Oh christmas tree

oh christmas tree

Log in to post a comment.

Canvas.setpenopacity(1);

const turtle = new Turtle();
turtle.penup();
// put the grid centre in the bottom left (screen is 200x200)
const centre_x = -82;
const centre_y = 95;
turtle.goto(centre_x, centre_y);
turtle.pendown();

// size
const rows = 5
const cols = 8; 

// tree dims
const num_branches = 8;
const tree_height = 42;

const gap = tree_height +5;

// tessalate
turtle.right(90);
let idx = 0
        
for (let row = 0; row < rows; row++) {
    for (let col = 0; col < cols; col++) {

        const x = centre_x + col*gap/2;
        const y = centre_y - row*gap;
        
        turtle.goto(x, y);
        turtle.pendown();
        
        // Flip every other tree
        const flip = col % 2 === 0 ? 0 : 1;
        
        turtle.right(flip * 180);

        // Draw a tree
        for (let i = 0; i < num_branches; i++) {
            const branch_len = tree_height/4 * i / 4;
            branch(branch_len, 60);
            turtle.penup();
            turtle.forward(Math.floor(tree_height / num_branches));
            turtle.pendown();
        }
        
        // draw stem
        turtle.forward(Math.floor(tree_height / num_branches));
        turtle.backward(tree_height);
        
        // move to next
        turtle.penup();
        turtle.goto(x, y);
        
        // rotate back to normal
        turtle.right(flip * 180);
        idx ++;

    }
}


function branch(length, angle){
    turtle.penup();
    turtle.right(angle);
    turtle.forward(length);
    turtle.pendown();
    turtle.backward(length);
    turtle.left(2*angle);
    turtle.forward(length);
    turtle.penup();
    turtle.backward(length);
    turtle.pendown();
    turtle.right(angle);

}