xxxxxxxxxx
// By Roni Kaufman
// https://ronikaufman.github.io/
// https://twitter.com/KaufmanRoni
let r_base = 1;
let r_step = 70;
let circles = [];
let margin = 18;
let l = 10; // smol line length
let palette = ["#f47a9d", "#f4ea7a", "#72d8e5"];
function setup() {
createCanvas(600, 600);
strokeWeight(3);
background(250);
}
function draw() {
let origin = createVector(width/2, height/2);
for (let i = 0; i < sq(r_base); i++) {
let r0 = random(r_step) + r_base;
let theta = random(TWO_PI);
let pos = p5.Vector.fromAngle(theta, r0).add(origin);
let r = r_step+random(-1, 1);
let new_circle = {
r: r,
pos: pos
}
if (can_add(new_circle)) {
draw_circle(pos.x, pos.y, r);
circles.push(new_circle);
}
}
try_add_smol_line();
try_add_smol_circle();
r_base += r_step;
r_step *= 0.85;
if (r_step < 2*l) {
for (let i = 0; i < 300; i++) {
try_add_smol_line();
try_add_smol_circle();
}
noLoop();
}
}
function can_add(new_c) {
let x = new_c.pos.x;
let y = new_c.pos.y;
let r = new_c.r;
for (let c of circles) {
if (c.pos.dist(new_c.pos) < c.r + r + margin) {
return false;
}
}
return (x+r < width-margin) && (x-r > margin) && (y+r < height-margin) && (y-r > margin);
}
function draw_circle(x, y, r) {
noStroke();
fill(random(palette));
push();
translate(x, y);
rotate(random(TWO_PI));
circle(0, 0, 2*r);
stroke("#1c2249");
let n = 2*floor(sqrt(r))-2;
for (let theta = 0; theta < TWO_PI; theta += TWO_PI/n) {
line((r-l/2)*cos(theta), (r-l/2)*sin(theta), (r+l/2)*cos(theta), (r+l/2)*sin(theta));
}
pop();
}
function try_add_smol_line() {
let x = random(margin, width-margin);
let y = random(margin, height-margin);
let pos = createVector(x, y);
let new_circle = {
r: l,
pos: pos
}
if (can_add(new_circle)) {
draw_smol_line(x, y);
circles.push(new_circle);
}
}
function draw_smol_line(x, y) {
stroke("#1c2249");
push();
translate(x, y);
rotate(random(TWO_PI));
line(-l/2, 0, l/2, 0);
pop();
}
function try_add_smol_circle() {
let x = random(margin, width-margin);
let y = random(margin, height-margin);
let pos = createVector(x, y);
let new_circle = {
r: l/2,
pos: pos
}
if (can_add(new_circle)) {
noStroke();
fill(random(palette));
circle(x, y, l);
circles.push(new_circle);
}
}