xxxxxxxxxx
let x, y; // the current position of the turtle
let currentangle = 60; // which way the turtle is pointing
let step = 1.2; // how much the turtle moves with each 'F'
let angle = -1; // how much the turtle turns with a '-' or '+'
let thestring = 'F+F-F-F'; // "axiom" or start of the string
let numloops = 7; // how many iterations to pre-compute
let therules = []; // array for rules
therules[0] = ['F', 'F+X-F']; // first rule
therules[1] = ['X', 'F-X-F']; // first rule
let whereinstring = 0; // where in the L-system are we?
function setup() {
angle = 180*(PI/2.2)/PI
let hh = 1000;
createCanvas(hh, hh);
background(250);
stroke(0, 7);
strokeWeight(2)
// start the x and y position at lower-left corner
x = width/2;
y = height/2;
// COMPUTE THE L-SYSTEM
for (let i = 0; i < numloops; i++) {
thestring = lindenmayer(thestring);
}
//console.log(thestring);
}
function draw() {
for (let j = 0; j < 7000; j++) {
// draw the current character in the string:
drawIt(thestring[whereinstring]);
whereinstring++;
if (whereinstring > thestring.length-1) whereinstring = 0;
}
}
// interpret an L-system
function lindenmayer(s) {
let outputstring = ''; // start a blank output string
// iterate through 'therules' looking for symbol matches:
for (let i = 0; i < s.length; i++) {
let ismatch = 0; // by default, no match
for (let j = 0; j < therules.length; j++) {
if (s[i] == therules[j][0]) {
outputstring += therules[j][1]; // write substitution
ismatch = 1; // we have a match, so don't copy over symbol
break; // get outta this for() loop
}
}
// if nothing matches, just copy the symbol over.
if (ismatch == 0) outputstring+= s[i];
}
return outputstring; // send out the modified string
}
// this is a custom function that draws turtle commands
function drawIt(k) {
if (k=='F') { // draw forward
// polar to cartesian based on step and currentangle:
let x1 = x + step*cos(radians(currentangle));
let y1 = y + step*sin(radians(currentangle));
line(x, y, x1, y1); // connect the old and the new
// update the turtle's position:
x = x1;
y = y1;
} else if (k == '+') {
currentangle += angle; // turn left
} else if (k == '-') {
currentangle -= angle; // turn right
}
}