// Using base L-System implementation from reinder
// Global code will be evaluated once.
Canvas.setpenopacity(1);
const turtle = new Turtle(0,100);
turtle.setheading(270);
// l-system
function createLSystem(numIters, axiom) {
let s = axiom;
for (let i=0; i<numIters; i++) {
s = processString(s);
}
return s;
}
function processString(oldStr) {
let newstr = "";
for (let i=0; i<oldStr.length; i++) {
newstr += applyRules(oldStr[i]);
}
return newstr;
}
function applyRules(ch) {
switch (ch) {
case "F": return "FF"; break;
case "X": return "F-[[XX]+X]+F[+FXX]-X"; break;
default: return ch;
}
}
const inst = createLSystem(5, "X"); // number of iterations and axiom
let distance = 1.5;
let angle = 22.5;
let states = [];
// The walk function will be called until it returns false.
function walk(i) {
const cmd = inst[i];
switch (cmd) {
case "F": step = distance - (0.1) * Math.random();
turtle.forward(step);
break;
theta = angle * Math.random();
case "+": turtle.right(theta);
break;
case "-": theta = -angle * Math.random();
turtle.left(theta);
break;
case "[": states.push({
x: turtle.xcor(),
y: turtle.ycor(),
h: turtle.heading()
});
break;
case "]": const state = states.pop();
turtle.penup();
turtle.goto(state.x, state.y);
turtle.setheading(state.h);
turtle.pendown();
break;
default:
}
return i < inst.length - 1;
}