Divisions III

Yet another take on divisions

Log in to post a comment.

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

const iterations = 4;       //min = 0, max = 8, step = 1
const size = 80;            //min = 10, max = 100, step = 1

let width = 0.25;

width = 1 - width;

// Global code will be evaluated once.
const turtle = new Turtle();

function divide(x, y, s, l){
    
    if(l == 0){
        // Quadrant 1
        if(Math.random() < 0.5){
            turtle.jump(x+s/2, y-s/2);
            turtle.goto(x+s/2, y+s/2);

            turtle.jump(x+s, y-s/2);
            turtle.goto(x+width*s, y-s/2);
            turtle.goto(x+width*s, y+s/2);
            turtle.goto(x+s, y+s/2);
        } else {
            turtle.jump(x+s/2, y-s/2);
            turtle.goto(x+s, y-s/2);
            turtle.jump(x+s/2, y+s/2);
            turtle.goto(x+s, y+s/2);
        }
        // Quadrant 2
        if(Math.random() < 0.5){
            turtle.jump(x+s/2, y-s/2);
            turtle.goto(x-s/2, y-s/2);

            turtle.jump(x+s/2, y-s);
            turtle.goto(x+s/2, y-width*s);
            turtle.goto(x-s/2, y-width*s);
            turtle.goto(x-s/2, y-s);
        } else {
            turtle.jump(x+s/2, y-s/2);
            turtle.goto(x+s/2, y-s);
            turtle.jump(x-s/2, y-s/2);
            turtle.goto(x-s/2, y-s);
        }
        // Quadrant 3
        if(Math.random() < 0.5){
            turtle.jump(x-s/2, y-s/2);
            turtle.goto(x-s/2, y+s/2);

            turtle.jump(x-s, y-s/2);
            turtle.goto(x-width*s, y-s/2);
            turtle.goto(x-width*s, y+s/2);
            turtle.goto(x-s, y+s/2);

        } else {
            turtle.jump(x-s/2, y-s/2);
            turtle.goto(x-s, y-s/2);
            turtle.jump(x-s/2, y+s/2);
            turtle.goto(x-s, y+s/2);
        }
        // Quadrant 4
        if(Math.random() < 0.5){
            turtle.jump(x+s/2, y+s/2);
            turtle.goto(x-s/2, y+s/2);

            turtle.jump(x+s/2, y+s);
            turtle.goto(x+s/2, y+width*s);
            turtle.goto(x-s/2, y+width*s);
            turtle.goto(x-s/2, y+s);
        } else {
            turtle.jump(x+s/2, y+s/2);
            turtle.goto(x+s/2, y+s);
            turtle.jump(x-s/2, y+s/2);
            turtle.goto(x-s/2, y+s);
        }
        
    } else {
        l--;
        s /= 2;
        
        divide(x+s, y+s, s, l);
        divide(x+s, y-s, s, l);
        divide(x-s, y+s, s, l);
        divide(x-s, y-s, s, l);
    }
}

divide(0, 0, size, iterations)

// The walk function will be called until it returns false.
function walk(i) {
    
    return false;
}