xxxxxxxxxx
// Lindenmayer systems
var thestring = '';
var therules = [];
var numproductions, t, d;
// curves
// thestring = 'FY'; // the starting string ("axiom")
// therules[0] = ['X', 'YF+XF+Y'];
// therules[1] = ['Y', 'XF-YF-X'];
// numproductions = 6;
// t = 60; // turn amount
// d = 5;
thestring = 'XF+F+XF+F+XF+'; // the starting string ("axiom")
therules[0] = ['X', 'XF+F+XF-F-F-XF-F+F+F-F+F+F-X'];
therules[1] = ['F', 'B'];
numproductions = 3;
t = 60; // turn amount
d = 10;
var ptr = 0;
var osc, rev;
var thefreq;
// graphics stuff:
// a simple version of Papert's turtle
var x = [];
var y = [];
var a = [];
var lpf = 1; // letters per frame
function preload()
{
}
function setup() {
createCanvas(windowWidth, windowHeight);
init();
// triple loop:
var outstring = thestring;
for(let i = 0;i<numproductions;i++)
{
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);
}
console.log(thestring);
//frameRate(12);
strokeWeight(1);
// sound:
osc = new p5.Oscillator('sine');
osc.freq(thefreq);
osc.amp(0.3);
osc.start();
rev = new p5.Reverb();
rev.process(osc, 3, 2);
}
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/2;
a[0] = PI*3/2;
thefreq = 300;
}
function doTurtle(k) {
let theamp = 0;
if(k=='c') init();
if(k=='F') // draw forward
{
stroke(0);
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;
theamp = 0.3;
}
if(k=='B') // draw circle
{
stroke(255, 0, 255);
ellipse(x[x.length-1], y[y.length-1], d, d);
theamp = 0.1;
}
if(k=='+') // turn right
{
a[a.length-1]+=radians(t);
thefreq*=1.75;
}
if(k=='-') // turn left
{
a[a.length-1]-=radians(t);
thefreq/=1.75;
}
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();
}
osc.freq(thefreq);
osc.amp(theamp, 0.04);
}