Parametric House of Cards

Iterates top-down, spawning a new triangle at each bottom corner.

Log in to post a comment.

// Parameters.
const cardHeight = 15;
const cardAngle = 20;
// Start at tip position.
const positions = [{x: 0, y: -85}];

const height = cardHeight*2;
const turtle = new Turtle();
const cardBase = Math.sin(cardAngle*Math.PI/180)*height;
const savePos = (x, y) => {
    const xr = Math.round(x);
    const yr = Math.round(y);
    if (!positions.find(p => p.x==xr && p.y==yr)) {
        positions.splice(0, 0, {x: xr, y: yr});
    }
}
const getLastPos = () => positions.pop();

Canvas.setpenopacity(1);

function walk(i) {
    const lastPos = getLastPos();
    turtle.penup();
    turtle.goto(lastPos.x, lastPos.y);
    if (turtle.y() > 80 || turtle.x() > 95 || turtle.x() < -95) return false;
    turtle.pendown();
    turtle.seth(90+cardAngle);
    turtle.forward(height);
    savePos(turtle.x(), turtle.y());
    turtle.seth(0);
    turtle.forward(cardBase*2);
    savePos(turtle.x(), turtle.y());
    turtle.seth(270-cardAngle);
    turtle.forward(height);
    return true;
}