Random Coasters

Keep recompiling.

Log in to post a comment.

const randomInt = (min, max) => Math.floor(Math.random()*(max-min+1)+min);// min and max included

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

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

const resetTurtle = () => {
    const ranX = randomInt(-80, 80);
    const ranY = randomInt(-80, 80);
    turtle.penup();
    turtle.goto(ranX,ranY);
    turtle.seth(0);
    turtle.pendown();
}

resetTurtle();

const NumPatterns = 10;
const PatternIntensity = 20000;
const Total = NumPatterns * PatternIntensity;

const patternSchema = Array.apply(null, Array(NumPatterns)).map(val => ({
    forward: randomInt(3, 7),
    right: randomInt(80, 200),
}));


// The walk function will be called until it returns false.
function walk(i) {
    if (i % PatternIntensity === 0) {
        resetTurtle();
    }

    let index = Math.floor(i / PatternIntensity);
    if (index > (NumPatterns - 1)) { index = NumPatterns - 1 };
    const { forward, right } = patternSchema[index];
    pattern(i, forward, right);
    return i < Total;
}

const pattern = (i, forward, right) => {
    turtle.forward(forward);
    turtle.right(Math.sin(i)*right);
    if (i % 17 === 0) {
        turtle.right(90);
    }
}