SquareSpiral

My "Hello World!"

Log in to post a comment.

// Global code will be evaluated once.
const turtle = new Turtle();
const rect = { 
    w:200, h:200,    
    get tl(){ return {x:this.w/-2,y:this.h/-2}; }
};

// Helpers
function D2R(a){ return a / 180 * Math.PI; }

turtle.penup();
turtle.goto(rect.tl.x, rect.tl.y);
turtle.pendown();

var length = rect.w;
var angle = 0.5;
var sinAngle = Math.sin(D2R(angle));

// The walk function will be called until it returns false.
function walk(i) 
{
    turtle.forward(length);
    turtle.right(90+angle);
    
    length -= sinAngle * length;
    
    return length > 0.5 && i < 1000;
}