xxxxxxxxxx
93
function setup() {
createCanvas(800, 600, WEBGL);
angleMode(DEGREES);
// noLoop();
}
function draw() {
background(0);
orbitControl();
// box(100);
let stepSize = 2;
let turtleX = 300;
let turtleY = 200;
let turtleZ = 200;
let turtleAngle = 0;
// strokeWeight(1);
// stroke(0,255,0,100);
// fill(0,0,255,200);
normalMaterial();
let code = ["FRCLF"];
let nextGenCode = [];
let generations = 3;
translate(-400,-300,100);
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);
let newZ = turtleZ - stepSize;
push();
translate(turtleX, turtleY, turtleZ);
box(1, 10, 1);
pop();
// line(turtleX, turtleY, newX, newY);
turtleX = newX;
turtleY = newY;
turtleZ = newZ;
}
if (instruction == "L") {
// push();
rotateZ(10);
// pop();
// turtleAngle += 40;
}
if (instruction == "R") {
rotateX(-5);
// turtleAngle -= 20;
}
if (instruction == "[") {
notebook.push(turtleX);
notebook.push(turtleY);
notebook.push(turtleZ);
notebook.push(turtleAngle);
}
if (instruction == "]") {
turtleAngle = notebook.pop();
turtleZ = notebook.pop();
turtleY = notebook.pop();
turtleX = notebook.pop();
}
if ( instruction == "C") {
push();
translate(turtleX, turtleY, turtleZ);
sphere(2);
pop();
// ellipse(turtleX, turtleY, 7);
}
}
}