Divisions VI

More, more, more divisions

Log in to post a comment.

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

const iterations = 6;       //min = 0, max = 8, step = 1
const size = 90;            //min = 10, max = 100, step = 1
const oblique = 0.5;        //min = 0, max = 1, step = 0.01
const delay = 0;            //min = 0, max = 1, step = 1, (no, yes)
const random = 0;           //min = 0, max = 5, step = 1

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

let lines = [];

function divide(x, y, s, l){
    
    if(l == 0){
        const rand = Math.random();
        
        if(rand < oblique){
            lines.push([[x, y-s], [x+s, y]]);
//            turtle.jump(x, y-s);
//            turtle.goto(x+s, y);
            lines.push([[x-s, y], [x, y+s]]);
//            turtle.jump(x-s, y);
//            turtle.goto(x, y+s);
        } else if(rand){
            lines.push([[x, y+s], [x+s, y]]);
//            turtle.jump(x, y+s);
//            turtle.goto(x+s, y);

            lines.push([[x-s, y], [x, y-s]]);
//            turtle.jump(x-s, y);
//            turtle.goto(x, 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)

for(let i = 0; i < random; i++){
    lines.sort(() => Math.random() > 0.5 ? -1 : 1);
}

let lastTime = Date.now();
// The walk function will be called until it returns false.
function walk(i) {
    if(i >= lines.length) return false;

    turtle.jump(lines[i][0]);
    turtle.goto(lines[i][1]);
    
    if(delay){
        while(lastTime >= Date.now());
        lastTime = Date.now();
    }

    return true;
}