hairy square

Some random walks away from a square's edge.

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();

function turtle_start() {
    turtle.penup();
    let side = Math.floor(Math.random()*4);
    let position = Math.random()*100-50;
    let angle = Math.random()*180;
    if (side == 0) {
        turtle.goto(-50,position);
        turtle.seth(angle+90);
    }
    if (side == 1) {
        turtle.goto(position, -50);
        turtle.seth(angle+180);
    }
    if (side == 2) {
        turtle.goto(50, position);
        turtle.seth(angle+270);
    }
    if (side == 3) {
        turtle.goto(position, 50);
        turtle.seth(angle);
    }
    turtle.pendown();
}

turtle_start();

function turtle2_start() {
    turtle.penup();
    let side = Math.floor(Math.random()*4);
    let position = Math.random()*80-40;
    let angle = Math.random()*180;
    if (side == 0) {
        turtle.goto(-40,position);
        turtle.seth(angle+270);
    }
    if (side == 1) {
        turtle.goto(position, -40);
        turtle.seth(angle);
    }
    if (side == 2) {
        turtle.goto(40, position);
        turtle.seth(angle+90);
    }
    if (side == 3) {
        turtle.goto(position, 40);
        turtle.seth(angle+180);
    }
    turtle.pendown();
}

let outer = true;

// The walk function will be called until it returns false.
function walk(i) {
    turtle.forward(1);
    turtle.lt((Math.random()-0.5)*50);
    if (outer) {
        if (Math.random() < 0.1 || (turtle.x() < 50 && turtle.x() > -50 && turtle.y() < 50 && turtle.y() > -50)) {
            if (i<5000) {
                turtle_start();
            }
            else {
                outer = false;
                turtle2_start();
            }
        }
    } else {
        if (Math.random() < 0.1 || (turtle.x() > 40 || turtle.x() < -40 || turtle.y() > 40 || turtle.y() < -40)) {
            turtle2_start();
        }
    }
    return i < 10000;
}