sketch = function( p ) {
let x, y;
let currentangle = 0;
let step = 53.3;
let angle = 90;
let thestring = 'A';
let numloops = 5;
let therules = [];
therules[0] = ['A', '-BF+AFA+FB-'];
therules[1] = ['B', '+AF-BFB-FA+'];
let whereinstring = 0;
p.setup = function() {
p.createCanvas(800, 800);
p.background(0);
p.stroke(255);
p.rectMode(p.CENTER);
x = 0;
y = p.height-1;
for (let i = 0; i < numloops; i++) {
thestring = lindenmayer(thestring);
}
}
p.draw = function() {
// draw the current character in the string:
drawIt(thestring[whereinstring]);
// increment the point for where we're reading the string.
// wrap around at the end.
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 * p.cos(p.radians(currentangle));
let y1 = y + step * p.sin(p.radians(currentangle));
p.line(x, y, x1, y1); // connect the old and the new
// update the turtle's position:
x = x1;
y = y1;
} else if (k == '+') { //turn left
currentangle += angle;
} else if (k == '-') { //turn right
currentangle -= angle;
} else {
// pick a gaussian (D&D) distribution for the radius:
let radius = 0;
let minR = 0;
let maxR = 30;
radius += p.random(minR, maxR);
radius += p.random(minR, maxR);
radius += p.random(minR, maxR);
radius = radius / 3 * 2;
p.fill(255);
//p.rect(x, y, radius, radius);
p.ellipse(x, y, radius, radius);
}
}
}