Tiling exercise
Log in to post a comment.
// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(1);
const turtle = new Turtle();
const iterations = 5; // min=0 max=8 step=1
const choice = 0; // min=0 max=1 step=1 (straight, diagonal)
function tiling(x, y, s, l, mode) {
if ((l === 0)) {
if ((mode === 0)) {
if ((Math.random() < 0.5)) {
turtle.penup();
turtle.goto(x, (y - s));
turtle.pendown();
turtle.goto(x, (y + s));
} else {
turtle.penup();
turtle.goto((x + s), y);
turtle.pendown();
turtle.goto((x - s), y);
}
} else {
if ((mode === 1)) {
if ((Math.random() < 0.5)) {
turtle.penup();
turtle.goto((x + s), (y - s));
turtle.pendown();
turtle.goto((x - s), (y + s));
} else {
turtle.penup();
turtle.goto((x + s), (y + s));
turtle.pendown();
turtle.goto((x - s), (y - s));
}
}
}
} else {
s /= 2;
l -= 1;
tiling((x - s), (y + s), s, l, mode);
tiling((x + s), (y + s), s, l, mode);
tiling((x - s), (y - s), s, l, mode);
tiling((x + s), (y - s), s, l, mode);
}
}
// set the function variables
tiling(0, 0, 88, iterations, choice);