### Fractal plant #2

Fractal plant by Paul Bourke: paulbourke.net/fractals/lsys/

#fractal #lsystem

Created by reinder on 2018/11/2
461
0

```// Fractal plant #2. Created by Reinder Nijhoff 2018
// @reindernijhoff
//
// https://turtletoy.net/turtle/a507092f1f
//
Canvas.setpenopacity(.5);

const turtle = new Turtle(-80,80);

// 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+[+F-F-F]-[-F+F+F]"; //   # Rule 1
//   case "F": return "FF"; //   # Rule 2
default: return ch;
}
}

const inst = createLSystem(5, "F"); // number of iterations and axiom
const distance = 1.8;
const angle = 22.5;
const states = [];

turtle.left(60);

// The walk function will be called until it returns false.
function walk(i) {
const cmd = inst[i];

switch (cmd) {
case "F":   turtle.forward(distance);
break;
case "B":   turtle.backward(distance);
break;
case "+":   turtle.right(angle);
break;
case "-":   turtle.left(angle);
break;
case "[":   states.push({
x: turtle.xcor(),
y: turtle.ycor(),