Pyramid

Simple pyramid with centered squares.

Log in to post a comment.

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

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

// The walk function will be called until it returns false.
function walk(i) {
    //Draw a square with length the size of our current iteration.
    square(0,0,i);
    //If this is the final iteration, return false to terminate.
    return i < iterations;
}

function square(x,y,a) {
    let rad = a / 2;
    turtle.penup();
    //Postion the center of the square at given coordinates.
    turtle.goto(x,y);
    //Make sure our first move will take us 'north'.
    turtle.setheading(90);
    //Move north by half the length of the square to intersect the first edge.
    turtle.forward(rad);
    //Turn right and move another half length
    //to get to the first 'corner' point. (top right corner).
    turtle.right(90);
    turtle.forward(rad);
    //Get ready to draw!
    turtle.pendown();
    //Draw the square, leaving the pen in the top right corner when done.
    turtle.right(90);
    turtle.forward(a);
    turtle.right(90);
    turtle.forward(a);
    turtle.right(90);
    turtle.forward(a);
    turtle.right(90);
    turtle.forward(a);
}