A pretty naive way of circle packing. Circles with a radius > hide_radius are not drawn.
#packing
Log in to post a comment.
// Circle packing #2. Created by Reinder Nijhoff 2021 // @reindernijhoff // // https://turtletoy.net/turtle/2b822bb094 // const canvas_size = 95; const shape = 1; // min=0, max=1, step=1 (Box, Circle) const pattern = 4; // min=0, max=5, step=1 (Uniform, Gradient X, Gradient Y, Radial, Noise, Noise Wave) const pattern_inverse = 0; // min=0, max=1, step=1 (No, Yes) const hide_radius = 5; // min=0, max=20, step=1 const max_radius = 10; // min=0.01, max=20, step=0.01 const min_radius = .3; // min=0.01, max=2, step=0.01 const noise_seed = 1; // min=0, max=100, step=1 const noise_freq = 0.5; // min=0, max=2, step=0.01 const radius_decr = .99; const max_tries = 5000; const noise = new SimplexNoise(noise_seed); let radius = max_radius; const circles = []; const turtle = new Turtle(); function add_circle(t, rt) { let coord_found = false; let tries = 0; while (!coord_found && tries < max_tries) { tries ++; let x = Math.random() * (canvas_size)*2 -canvas_size; let y = Math.random() * (canvas_size)*2 -canvas_size; let factor = 1; switch(pattern) { case 1: factor = (x+canvas_size) / (2*canvas_size); break; case 2: factor = (y+canvas_size) / (2*canvas_size); break; case 3: factor = Math.hypot(x,y)/(Math.sqrt(2)*canvas_size); break; case 4: factor = noise.noise2D([x * noise_freq / canvas_size, y * noise_freq / canvas_size])*.5 + .5; break; case 5: factor = noise.noise2D([Math.floor(x * 12 * noise_freq / canvas_size + noise.noise2D([x * noise_freq / canvas_size, y * noise_freq / canvas_size])),.5])*.5 + .5; break; } if (pattern_inverse) { factor = 1 - factor; } const r = Math.max(min_radius, factor * rt); x *= (canvas_size - r) / canvas_size; y *= (canvas_size - r) / canvas_size; const possible = circles.every(c => { const dx = c[0] - x; const dy = c[1] - y; const dr = c[2] + r; return dx**2 + dy**2 > dr**2 && (!shape || x**2 + y**2 < (canvas_size-r)**2); }); if (possible && (!shape || x*x+y*y < (canvas_size-r)*(canvas_size-r))) { coord_found = true; if (r < hide_radius) { draw_circle(x,y,t, r); } circles.push([x,y,r]); return true; } } return false; } function draw_circle(x,y,t, r) { turtle.jump(x,y-r); turtle.circle(r); } function walk(i) { if (!add_circle(turtle, radius)) { radius *= radius_decr; } return radius >= min_radius; } //////////////////////////////////////////////////////////////// // Simplex Noise utility code. Created by Reinder Nijhoff 2020 // https://turtletoy.net/turtle/6e4e06d42e // Based on: http://webstaff.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf //////////////////////////////////////////////////////////////// function SimplexNoise(seed = 1) { const grad = [ [1, 1, 0], [-1, 1, 0], [1, -1, 0], [-1, -1, 0], [1, 0, 1], [-1, 0, 1], [1, 0, -1], [-1, 0, -1], [0, 1, 1], [0, -1, 1], [0, 1, -1], [0, -1, -1] ]; const perm = new Uint8Array(512); const F2 = (Math.sqrt(3) - 1) / 2, F3 = 1/3; const G2 = (3 - Math.sqrt(3)) / 6, G3 = 1/6; const dot2 = (a, b) => a[0] * b[0] + a[1] * b[1]; const sub2 = (a, b) => [a[0] - b[0], a[1] - b[1]]; const dot3 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; const sub3 = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]]; class SimplexNoise { constructor(seed = 1) { for (let i = 0; i < 512; i++) { perm[i] = i & 255; } for (let i = 0; i < 255; i++) { const r = (seed = this.hash(i+seed)) % (256 - i) + i; const swp = perm[i]; perm[i + 256] = perm[i] = perm[r]; perm[r + 256] = perm[r] = swp; } } noise2D(p) { const s = dot2(p, [F2, F2]); const c = [Math.floor(p[0] + s), Math.floor(p[1] + s)]; const i = c[0] & 255, j = c[1] & 255; const t = dot2(c, [G2, G2]); const p0 = sub2(p, sub2(c, [t, t])); const o = p0[0] > p0[1] ? [1, 0] : [0, 1]; const p1 = sub2(sub2(p0, o), [-G2, -G2]); const p2 = sub2(p0, [1-2*G2, 1-2*G2]); let n = Math.max(0, 0.5-dot2(p0, p0))**4 * dot2(grad[perm[i+perm[j]] % 12], p0); n += Math.max(0, 0.5-dot2(p1, p1))**4 * dot2(grad[perm[i+o[0]+perm[j+o[1]]] % 12], p1); n += Math.max(0, 0.5-dot2(p2, p2))**4 * dot2(grad[perm[i+1+perm[j+1]] % 12], p2); return 70 * n; } hash(i) { i = 1103515245 * ((i >> 1) ^ i); const h32 = 1103515245 * (i ^ (i>>3)); return h32 ^ (h32 >> 16); } } return new SimplexNoise(seed); }