Rationing

Generates art based on the golden ratio.

Log in to post a comment.

Canvas.setpenopacity(-1);

const turtle = new Turtle();
turtle.pendown();

const maxElems = 3+Math.floor(Math.random()*4);
const PHI = 0.618099;
const PHI1 = 1.618099;
const w = 200;
const h = 200;

const chance = 0.125;
const rand = (from) => {
    if (Math.random() < chance) {
        return from/2;
    }
    if (Math.random() < chance) {
        return from-from*PHI;
    }
    if (Math.random() < chance) {
        return from-from*PHI*PHI;
    }
    if (Math.random() < chance) {
        return from/PHI1;
    }
    if (Math.random() < chance) {
        return from*PHI*PHI;
    }
    if (Math.random() < chance) {
        return from*PHI;
    }
    if (Math.random() < chance) {
        return from*PHI1;
    }
    return from;
}

const randEdge = (edge, offset) => {
    if (Math.random() < 0.5) {
        return edge-offset;
    }
    if (Math.random() < 0.5) {
        return 0+offset;
    }
    return rand(edge)-offset;
}

const elems = (() => {
    let elems = [];
    while (elems.length < maxElems) {
        const ew = rand(w);
        const eh = rand(h);
        const xedge = randEdge(w, ew);
        const yedge = randEdge(h, eh);
        elems.push({
            type: Math.random() < 0.8 ? 'rect' : 'circle',
            x: xedge,
            y: yedge,
            w: ew,
            h: eh,
            fill: 0.5+Math.round(Math.random()*4)/4,
            fillAngle: 22.5+22.5*Math.floor(Math.random()*7)
        })
    }
    return elems;
})();

function walk(i) {
    for (const { type, x, y, w, h, fill, fillAngle } of elems) {
        if (type === "circle") {
            turtle.seth(0);
            for (let i = w-h; i > 0; i-=fill) {
                turtle.jump((-100+(x === 0 ? x+w-i : x-i)), (-100+y-i));
                turtle.circle(i);
            }        
        } else {
            turtle.seth(fillAngle);
            for (let i = 0; i < h; i+=fill) {
                turtle.jump(-100+x, -100+y+i);
                turtle.forward(w/Math.cos(Math.PI*fillAngle/180));
            }
            const step = (fill)/Math.sin(Math.PI*fillAngle/180);
            for (let i = step; i < w; i+=step) {
                turtle.jump(-100+x+i, -100+y);
                turtle.forward((w-i)/Math.cos(Math.PI*fillAngle/180));
            }        
        }
    }
    return false;
}