Parametric House of Cards

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

Log in to post a comment.

// Parameters.
const cardHeight = 2; // min=2, max=30, step=1
const cardAngle = 25; // min=0, max=90, step=1
// 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;
}