Square to Circle ⬜️⚪️

Many lines from random position on edge from square to random position on edge of circle.

Log in to post a comment.

const turtle = new Turtle();

const rectSize = 75; // min=1, max=100, step=1
const rectLooseness = 3; // min=0, max=25, step=1
const circleSize = 50; // min=1, max=100, step=1
const circleLooseness = 3; // min=0, max=25, step=1
const totalLines = 1000; // min=1, max=2000, step=1

const lerp = (from,to, t) => from + (to - from) * t;
const or = (a, b) => Math.random() > 0.5 ? a : b;
const getLooseness = (amount) => lerp(-amount * 0.5, amount * 0.5, Math.random());

const getCircleEdge = radius => {
    const angle = Math.PI * 2 * Math.random();
    return [
        Math.sin(angle) * radius, 
        Math.cos(angle) * radius
    ]
}

function getSquareEdge(a, b) {
    const rect = [
        lerp(a, b, Math.random()),
        or(a, b)
    ];
    if (Math.random() > 0.5) rect.push(rect.shift());
    return rect;
}

function walk(step) {
    turtle.goto( getSquareEdge(-rectSize + getLooseness(rectLooseness), rectSize + getLooseness(rectLooseness)) );
    turtle.jump( getCircleEdge(circleSize + getLooseness(circleLooseness)) );
    return step < totalLines;
}