xxxxxxxxxx
// Lindenmayer systems
var thestring = '';
var therules = [];
var numproductions, t, d;
// plant 1
// thestring = 'F'; // the starting string ("axiom")
// therules[0] = ['F', 'F[+F]F[-F][F]'];
// numproductions = 5;
// t = 30; // turn amount
// d = 20;
// plant 2
// thestring = 'F'; // the starting string ("axiom")
// therules[0] = ['F', 'FF-[-F+F+F]+[+F-F-F]'];
// numproductions = 4;
// t = 22.5; // turn amount
// d = 15;
// plant 3
thestring = 'X'; // the starting string ("axiom")
therules[0] = ['X', 'F[+X][-X]FX'];
therules[1] = ['F', 'FF'];
numproductions = 7;
t = 25.7; // turn amount
d = 3;
var ptr = 0;
var Asnd, Bsnd, Csnd;
// graphics stuff:
// a simple version of Papert's turtle
var x = [];
var y = [];
var a = [];
var lpf = 20; // letters per frame
function preload()
{
Asnd = loadSound('A.mp3');
Bsnd = loadSound('B.mp3');
Csnd = loadSound('C.mp3');
}
function setup() {
createCanvas(windowWidth, windowHeight);
init();
// triple loop:
for(let i = 0;i<numproductions;i++)
{
var outstring = '';
for(let j = 0;j<thestring.length;j++)
{
for(let k = 0;k<therules.length;k++)
{
var ismatch = 0;
if(thestring.charAt(j)==therules[k][0]) // MATCH
{
outstring+=therules[k][1]; // copy of the second half
ismatch=1;
break;
}
}
if(ismatch==0) outstring+=thestring.charAt(j);
}
thestring=outstring;
console.log(thestring);
}
//frameRate(12);
strokeWeight(3);
}
function draw() {
var c;
for(let i = 0;i<lpf;i++)
{
c = thestring.charAt(ptr);
if(ptr<thestring.length) doTurtle(c);
ptr = ptr+1;
}
}
function init() {
background(255);
x = [];
y = [];
a = [];
x[0] = width/2;
y[0] = height;
a[0] = PI*3/2;
}
function doTurtle(k) {
if(k=='c') init();
if(k=='F') // draw forward
{
let x1 = x[x.length-1] + d*cos(a[a.length-1]);
let y1 = y[y.length-1] + d*sin(a[a.length-1]);
line(x[x.length-1], y[y.length-1], x1, y1);
x[x.length-1] = x1;
y[y.length-1] = y1;
redraw();
}
if(k=='+') // turn right
{
a[a.length-1]+=radians(t);
}
if(k=='-') // turn left
{
a[a.length-1]-=radians(t);
}
if(k=='[') // start branch
{
x.push(x[x.length-1]);
y.push(y[y.length-1]);
a.push(a[a.length-1]);
}
if(k==']') // end branch
{
x.pop();
y.pop();
a.pop();
}
}