xxxxxxxxxx
102
function setup() {
createCanvas(600, 600);
noLoop()
button = createButton("Regenerate!").mousePressed(buttonPressed)
}
function draw() {
background(30);
num_sections = int(random(3, 7))
num_sub_sections = int(random(3, 7))
w = width / num_sections
h = height / num_sections
points = []
for (let i = 0; i < num_sections; i++) {
for (let j = 0; j < num_sections; j++) {
x = j * w
y = i * h
points.push(generatePoints(2000, num_sub_sections, x, y, w, h))
}
}
for (let i = 0; i < points.length; i++) {
drawPoints(points[i])
}
}
function buttonPressed() {
print("!!!!")
redraw()
}
function drawPoints(points) {
let max_hue = 300
// noStroke()
push()
colorMode(HSB, 300, 100, 100)
for (let i = 0; i < points.length; i++) {
let p = points[i]
let hx = map(p.sx, 0, p.s, 0, (max_hue - 1) / 2)
let hy = map(p.sy, 0, p.s, 0, (max_hue - 1) / 2)
fill(hx + hy, 100, 100)
noFill()
stroke(hx + hy, 100, 100)
circle(p.x, p.y, 3)
}
pop()
}
function generatePoints(n, s, x, y, w, h) {
let rx = randomProbabilities(s)
let ry = randomProbabilities(s)
let points = []
for (let i = 0; i < n; i++) {
let sx = chooseSection(rx)
let sy = chooseSection(ry)
let min_x = x + (sx / s) * w
let max_x = x + ((sx + 1) / s) * w
let min_y = y + (sy / s) * h
let max_y = y + ((sy + 1) / s) * h
let cx = Math.random() * (max_x - min_x) + min_x
let cy = Math.random() * (max_y - min_y) + min_y
points.push({x: cx, y: cy, sx: sx, sy: sy, s: s})
}
return points
}
function chooseSection(probabilities) {
let r = Math.random()
for (let i = 0; i < probabilities.length; i++) {
if (r < probabilities[i]) {
return i
}
}
}
function randomProbabilities(n) {
let randoms = []
for (let i = 0; i < n; i++) {
randoms.push(Math.random())
}
return accumulate(normalize(randoms))
}
function accumulate(a) {
for (let i = 1; i < a.length; i++) {
a[i] += a[i-1]
}
return a
}
function sum(a) {
return a.reduce((b, c) => b + c)
}
function normalize(a) {
return a.map(n => n / sum(a))
}