Imperfect poly packing

Based on Circle packing #1 but an imperfect imperfect-poly-draw-function is used to draw the 'circles'. The result is very imperfect.

#packing #handdrawing

Log in to post a comment.

// Imperfect poly packing. Created by Reinder Nijhoff 2018
// @reindernijhoff
//
// https://turtletoy.net/turtle/b0ac9de176
//

const min_number_of_sides = 3; // try 30
const canvas_size = 95;
const max_radius = 10;
const min_radius = 1;
const radius_decr = .995;
const max_tries = 7500;

let radius = max_radius;

const circles = [];

const turtle = new Turtle();

function add_circle(t, r) {
    let coord_found = false;
    let tries = 0;
    
    while (!coord_found && tries < max_tries) {
        tries ++;
        const x = Math.random() * (canvas_size-r)*2 -canvas_size + r;
        const y = Math.random() * (canvas_size-r)*2 -canvas_size + r;
        let possible = true;
        for (let i=0; i<circles.length; i++) {
            const dx = circles[i][0] - x;
            const dy = circles[i][1] - y;
            const dr = circles[i][2] + r;
            
            if ( dx*dx + dy*dy < dr * dr) {
                possible = false;
                break;
            }
        }
        if (possible) {
            coord_found = true;
            draw_circle(x,y,t, r);
            circles.push([x,y,r]);
            return true;
        }
    }
    return false;
}

function draw_circle(x,y,t, r) {
    draw_imperfect_poly(x,y,t,min_number_of_sides+Math.floor(Math.random()*4),r);
}

function draw_imperfect_poly(x,y,t, c, r) {
    const side = 2*Math.sin(Math.PI/c) * r - 3/c;
    const side_div = 10;
    const rand_angle = Math.max(0.03,0.1/c);

    t.radians();   
    t.penup();
    t.goto(x,y-r);
    t.setheading(0);
    t.circle(r,Math.random()*Math.PI);
    t.pendown();
    
    t.right(Math.PI/c);
    for (let i=0; i<c; i++) {
        for(let j=0; j<Math.ceil(side*side_div);j++) {
            t.forward(side/Math.ceil(side*side_div));
            t.right((Math.random()-.5)*rand_angle);
        }
        t.circle(3/c,Math.PI*2/c);
    }
    t.forward(Math.random());
}

function walk(i) {
    if (!add_circle(turtle, radius)) {
        radius *= radius_decr;
    }
    return radius >= min_radius;
}