xxxxxxxxxx
82
function setup() {
createCanvas(800, 600);
background(230);
angleMode(DEGREES);
colorMode(HSB);
noLoop();
}
function draw() {
let stepSize = 12;
let turtleX = 325;
let turtleY = 475;
let turtleAngle = 0;
let hueStep = 5;
let hueMax = 330;
let hueMin = 180;
let turtleHue = hueMin;
strokeWeight(1);
stroke(0, 100, 100, 0.5);
fill(turtleHue, 100, 100, 0.2);
let code = ["FRFRFRFR"];
let nextGenCode = [];
let generations = 5;
for (let genNum = 1; genNum <= generations; genNum++) {
nextGenCode = [];
for (let instruction of code) {
if (instruction == "F") {
nextGenCode.push("F[HLFx]FC");
} else if (instruction == "x") {
nextGenCode.push("FxF");
} 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);
stroke(0, 100, 100, 0.5);
line(turtleX, turtleY, newX, newY);
stroke(turtleHue, 100, 100, 0.5);
turtleX = newX;
turtleY = newY;
}
if (instruction == "L") {
turtleAngle += 120;
}
if (instruction == "R") {
turtleAngle -= 120;
}
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, 6);
}
if (instruction == "H") {
turtleHue += hueStep;
if (turtleHue > hueMax) {
turtleHue = turtleHue - (hueMax - hueMin);
}
fill(turtleHue, 100, 100, 0.2);
}
stroke(turtleHue, 100, 100, 0.5);
}
}