xxxxxxxxxx
68
function setup() {
createCanvas(800, 600);
background(0);
angleMode(DEGREES);
noLoop();
}
function draw() {
let stepSize = 15;
let turtleX = 300;
let turtleY = 200;
let turtleAngle = 0;
strokeWeight(1);
stroke(0,255,0,100);
fill(0,0,255,200);
let code = ["FRFCLF"];
let nextGenCode = [];
let generations = 4;
for (let genNum = 1; genNum <= generations; genNum++) {
nextGenCode = [];
for (let instruction of code) {
if (instruction == "F") {
nextGenCode.push("RFC[RFLFL]F");
} if (instruction == "R") {
nextGenCode.push("RF[RF]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();
}
if ( instruction == "C") {
ellipse(turtleX, turtleY, 7);
}
}
}