Circle grid 🌕

Draw from grid positions to some random position at large circle.
It's not perfect, but who like perfection anyway.

twitter.com/mknol

Log in to post a comment.

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(1);

// Global code will be evaluated once.
const turtle = new Turtle();

const DENSITY = 25; // min=1 max=100, step=1
const RADIUS = 45; // min=1 max=100, step=1
const DOTS = 0.38; // min=0 max=1, step=0.001

const CENTER_X = 0; // min=-75 max=75, step=5
const CENTER_Y = 0; // min=-75 max=75, step=5


const center = [CENTER_X,CENTER_Y];
const r = RADIUS;
const grid = DENSITY;
const size = 90;

// The walk function will be called until it returns false.
function walk(i) {
    const x = i % grid;
    const y = i / grid | 0;
    const offset = (size * 2 / grid);
    
    if (i != 0 && i != grid - 1 && i != grid * grid - grid && i != grid * grid - 1) { // "rounded corner"
        const pos = [-size + x * offset, -size + y * offset];
        const randomPos = add2(pos, [(Math.random() - 0.5) * r, (Math.random() - 0.5) * r]);
        const len = dist(randomPos, center);
        const endPos = add2(center, scale2(sub2(randomPos, center), 1 / len * r));
        if (dist(center, pos) > r)  {
            if (Math.random() < DOTS) {
                const circle = Math.random() + 0.5;
                turtle.jump(pos[0] , pos[1] - circle);
                turtle.circle(circle);
            }
            turtle.jump(pos[0], pos[1]);
            turtle.goto(endPos[0], endPos[1]);
        }
    
    }

    return i < grid * grid - 1;
}

// vec2 functions
const scale2=(a,b)=>[a[0]*b,a[1]*b];
const add2=(a,b)=>[a[0]+b[0],a[1]+b[1]];
const sub2=(a,b)=>[a[0]-b[0],a[1]-b[1]];
const dot2=(a,b)=>a[0]*b[0]+a[1]*b[1];
const dist_sqr2=(a,b)=>(a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1]);
const dist=(a,b)=>Math.sqrt(dist_sqr2(a,b));
const angle=(a,b)=>Math.atan2(b[1]-a[1], b[0]-a[0]);