Tree

a recursive tree

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.
const turtle = new Turtle();
turtle.penup();
turtle.goto(0,100);
turtle.setheading(270);
turtle.pendown();


const stem = 35; // min=1 max=100 step=1 
turtle.forward(stem);


const length = 15; // min=1 max=20 step=1 
const radius = 15; // min=1 max=360 step=1

leaf(turtle.pos(), turtle.h(), length, radius, 0);

function leaf(position, heading, length, radius, generation) {
    
    if(length==0) {
        return;
    }
    turtle.jump(position);
    turtle.setheading(heading);
    turtle.right(radius);
    turtle.forward(length);
    leaf(turtle.pos(), turtle.h(), length-1, radius, generation+1);
    
    turtle.jump(position);
    turtle.setheading(heading);
    turtle.left(radius);
    turtle.forward(length);
    leaf(turtle.pos(), turtle.h(), length-1, radius, generation+1);
}