xxxxxxxxxx
61
function setup() {
createCanvas(800, 600);
background(220);
angleMode(DEGREES);
noLoop();
}
function draw() {
let stepSize = 25;
let turtleX = 400;
let turtleY = 200;
let turtleAngle = 0;
strokeWeight(1);
stroke(255, 0, 0, 100);
let code = ["FRFLF"];
let nextGenCode = [];
let generations = 5;
for (let genNum = 1; genNum <= generations; genNum++) {
nextGenCode = [];
for (let instruction of code) {
if (instruction == "F") {
nextGenCode.push("RF[RFLFL]F");
} else {
nextGenCode.push(instruction);
}
}
code = nextGenCode;
}
let notebook = [];
for (let instruction of code) {
if (instruction == "F") {
let newX = turtleX - stepSize * sin(turtleAngle);
let newY = turtleY - stepSize * cos(turtleAngle);
line(turtleX, turtleY, newX, newY);
turtleX = newX;
turtleY = newY;
}
if (instruction == "L") {
turtleAngle += 40;
}
if (instruction == "R") {
turtleAngle -= 20;
}
if (instruction == "[") {
notebook.push(turtleX);
notebook.push(turtleY);
notebook.push(turtleAngle);
}
if (instruction == "]") {
turtleAngle = notebook.pop();
turtleY = notebook.pop();
turtleX = notebook.pop();
}
}
}