Golden Spiral

Golden Spiral approximation

Log in to post a comment.

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

const STEPS = 100;
const STEP_LENGTH = 0.1;
const GOLDEN_RATIO = 1.61803;
const SLEEP_TIME = 30;


function Spiral(rotation) {
    const turtle = new Turtle();
    turtle.penup();
    turtle.goto(0,0);
    turtle.pendown();
    turtle.left(rotation);
    
    let currentLength = STEP_LENGTH;
    
    function sleep(ms = SLEEP_TIME) {
      var date = new Date();
      var curDate = null;
    
      do { curDate = new Date(); }
      while (curDate-date < ms);
    }
    
    function draw() {
        turtle.circle(currentLength, -90);
        currentLength *= GOLDEN_RATIO;
        //sleep();
    }
    
    return {draw};

}

let spirals = [];

for (let i = 0; i < 360; i += 45) {
    spirals.push(Spiral(i));
}

// The walk function will be called until it returns false.
function walk(i) {
    // turtle.forward(currentLength);
    // turtle.left(140);
    
    
    spirals.forEach(s => s.draw());
    
    //currentLength *= GOLDEN_RATIO;
    
    return i < STEPS - 1;
}