Spirals

I was trying stuff with a stack (array.push, array.pop).

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.
var turtle = new Turtle();
turtle.penup();
turtle.goto(10,6);

let arr = [];

function push(){
    let pos = turtle.pos();
    arr.push([pos[0],pos[1],turtle.h()]);
}

function forward(x){
    turtle.forward(x);
    push();
}


function left(x){
    turtle.left(x);
    push();
}


function right(x){
    turtle.right(x);
    push();
}


// The walk function will be called until it returns false.
function walk(i) {

    turtle.pendown();
    
    for(let j = i * 1.4 + 1; j > 0; j--){
        left(i % 2 == 0? 60 : 88);
        forward(j/2);
    }
    
    while((pos = arr.pop()) != undefined){
        turtle.setheading(pos[2]);
        turtle.goto(pos[0], pos[1]);
    }
    
    turtle.penup();
    turtle.right(30);
    turtle.forward(2 + i * 2.3);
    
    return i < 40;
}