Circles 🌘

Simple turtle, inspired by twitter.com/paulrick…/1117819286695944192

Log in to post a comment.

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

const draw_size = 50;
const grid = 5; // min=1, max=90, step=1
const radius = 15; // min=1, max=90, step=1
const distance = 40; // min=1, max=50, step=1
const minLines = 3; // min=0, max=10, step=0.01

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

// The walk function will be called until it returns false.
function walk(i) {
    const x = i % grid;
    const y = i / grid | 0
    const startPos = [x * distance, y * distance];
    const startAngle = Math.random() * Math.PI*2;
    for (let z = 0; z < ((x + minLines) * (y + minLines)); z+=3) {
        const a1 = startAngle + getAngle1();
        const a2 = startAngle + getAngle2();
        line(
            startPos[0] + Math.cos(a1) * radius, 
            startPos[1] + Math.sin(a1) * radius,
            startPos[0] + Math.cos(a2) * radius, 
            startPos[1] + Math.sin(a2) * radius
        );
    }
    return i < grid * grid - 1;
}

const totalAngles = 100; //min=0, max=100, step=1
const getAngle1 = () => Math.round(Math.random()*totalAngles)/totalAngles * Math.PI;
const getAngle2 = () => Math.PI + Math.round(Math.random()*totalAngles)/totalAngles * Math.PI;

const line = (x1, y1, x2, y2) => {
    const s = (grid - 1) * distance / 2;
    x1 -= s;
    x2 -= s;
    y1 -= s;
    y2 -= s;
    turtle.jump(x1, y1);
    turtle.goto(x2, y2);
}

////////////////////////////////////////////////////////////////
// Slowpoke utility code. Created by Reinder Nijhoff 2019
// https://turtletoy.net/turtle/cfe9091ad8
////////////////////////////////////////////////////////////////

function Slowpoke(x, y) {
    const linesDrawn = {};
    class Slowpoke extends Turtle {
        goto(x, y) {
            const p = Array.isArray(x) ? [...x] : [x, y];
            if (this.isdown()) {
                const o = [this.x(), this.y()];
                const h1 = o[0].toFixed(2)+'_'+p[0].toFixed(2)+o[1].toFixed(2)+'_'+p[1].toFixed(2);
                const h2 = p[0].toFixed(2)+'_'+o[0].toFixed(2)+p[1].toFixed(2)+'_'+o[1].toFixed(2);
                if (linesDrawn[h1] || linesDrawn[h2]) {
                    super.up();
                    super.goto(p);
                    super.down();
                    return;
                }
                linesDrawn[h1] = linesDrawn[h2] = true;
            } 
            super.goto(p);
        }
    }
    return new Slowpoke(x,y);
}