Sierpinski for printers

Sierpinski triangle made up of straight lines

Log in to post a comment.

const recursion_depth = 7.; // min=1, max=10, step=1
const angle = 45.; // min=0.0, max=360, step=0.1
const start_len = 100; // min=0, max=200, step=0.1
const start_x = 0; // min=-200, max=200, step=0.1
const start_y = 0; // min=-200, max=200, step=0.1
// How do I get the size of the Canvas?

Canvas.setpenopacity(1);

const turtle = new Turtle();
turtle.setheading(angle);

function walk(i) {
    current_len = start_len;
    current_x_y = [start_x,start_y];
    
    stack = [[current_len,current_x_y]];
    
    for(let i=0; i<recursion_depth; i++){
        new_stack = [];
        console.log(i, stack);
        for(let j=0; j<stack.length; j++){
            [drawlen, x_y] = stack[j];
            turtle.penup();
            turtle.goto(x_y[0], x_y[1]);
            turtle.backward(drawlen);
            
            turtle.pendown();
            
            turtle.forward(drawlen/2.);
            new_stack.push([drawlen/2.,turtle.pos()]);
            turtle.forward(drawlen);
            new_stack.push([drawlen/2.,turtle.pos()]);
            turtle.forward(drawlen/2.);
            
            [x,y] = turtle.pos();
            
            turtle.penup();
            turtle.goto(x-drawlen*Math.cos(Math.PI/4.),y);
            
            turtle.pendown();
            
            turtle.backward(drawlen/2.);
            new_stack.push([drawlen/2.,turtle.pos()]);
            turtle.backward(drawlen/2.);
            
        }
        stack = new_stack;
    }
    
    return false;
}