xxxxxxxxxx
// By Roni Kaufman
// https://ronikaufman.github.io/
// https://twitter.com/KaufmanRoni
let gap = 8;
let margin = 50;
let squiggles = [];
let palette = ["#0077e1", "#f5d216", "#fc3503"];
let blackLines;
function setup() {
createCanvas(500, 500);
//noLoop();
noStroke();
background(250);
blackLines = random() < 1/2;
let circles = [];
for (let i = 0; i < 100; i++) {
let x = random(margin, width-margin);
let y = random(margin, height-margin);
let d = gap;
let canAdd = true;
for (let c of circles) {
if (dist(x, y, c.x, c.y) < gap*2) {
canAdd = false;
break;
}
}
if (canAdd) {
if (blackLines) fill(random(palette));
else fill(5);
circle(x, y, d);
circles.push(createVector(x, y));
}
}
squiggles.push({
circles: circles
});
}
function draw() {
let noAddition = true;
for (let i = 0; i < 150; i++) {
if (makeSquiggle()) {
noAddition = false;
}
}
if (noAddition) noLoop();
}
function makeSquiggle() {
let squi = new Squiggle(random(margin, width-margin), random(margin, height-margin));
while (squi.move());
if (squi.circles.length > 5) {
squiggles.push(squi);
squi.draw();
return true;
}
return false;
}
function Squiggle(x0, y0) {
this.x = x0;
this.y = y0;
this.circles = [createVector(x0, y0)];
this.d = 4;
this.amp = random(2, 10);
this.move = function() {
let theta = atan2(this.y-height/2, this.x-width/2) + sin(dist(this.x, this.y, width/2, height/2)/this.amp);
let r = 1;
this.x += r * cos(theta);
this.y += r * sin(theta);
if (this.x < margin || this.x > width-margin || this.y < margin || this.y > height-margin) {
return false;
}
let circ = createVector(this.x, this.y);
for (let squi of squiggles) {
for (let c of squi.circles) {
if (circ.dist(c) < gap) return false;
}
}
this.circles.push(circ);
return true;
}
this.draw = function() {
if (blackLines) fill(5);
else fill(random(palette));
let n = this.circles.length;
for (let i = 0; i < n; i++) {
let c = this.circles[i];
circle(c.x, c.y, this.d);
}
}
}