LSystem Code

by buechley

General L System Code:

/*
F = forward
+ = left turn by angle
- = right turn by angle
[ = remember current state
] = return to last remembered state

generate a pattern using LSystem, which takes 3 parameters
a STARTING STRING, a RULE, and an ANLGE:
myLSystem = new LSystem(STARTING STRING, RULE, ANLGE);

here's an example, starting with starting string = F, rule= F+F--F+F, angle=60:
myLSystem = new LSystem("F", "F+F--F+F", 60);
*/

import processing.pdf.*;

LSystem myLSystem;
float scaleFactor=.5;
int iterations =3;
int startingSize=200;
int i;

void setup() {
size(500, 500); //set the size of the window (& the pdf document)
smooth();
noLoop(); //only draw the design once
stroke(100,100,50); //use a yellow-green color

//scales the pattern down as iterations increase
scaleFactor = 1/pow(3,iterations);
}

void draw() {
//define the design
//myLSystem = new LSystem(STARTING STRING, RULE, ANLGE);
myLSystem = new LSystem("F", "F+F--F+F", 60); //Koch curve
//myLSystem = new LSystem("F-F-F-F-F-F", "F+F--F+F", 60); //Koch snowflake
//myLSystem = new LSystem("F+F+F+F", "F+F-F-F+F", 90); //square variation on Koch
//myLSystem = new LSystem("F", "F+[F]--[F]+", 30); //tree
//myLSystem = new LSystem("F-F-F-F-F", "FF+F+F+F+F+F", 72); //pentagonal pattern
//myLSystem = new LSystem("F", "F+[F]--[F]+", 30); //tree
//generate the design
myLSystem.applyRuleToAxiom(iterations,startingSize,scaleFactor);
//set a white background
background(255);
//move to an appropriate location on the page
translate(width/2, height/2);
//draw the design to the screen
myLSystem.render();
}

void keyPressed() {
//if the 'q' key is pressed, generate a PDF & quit
if (key == 'q') {
String fileName= "LSystem"+millis()+".pdf"; //create a unique name for your file
beginRecord(PDF, fileName);
draw();
endRecord();
exit();
}
else
{
redraw();
}
}

class LSystem
{
int steps = 0;
int randomness;

String axiom;
String rule;
String production;
float drawLength;
float theta;
float scaleFactor;
int x=0;

int generations;

//default system is a koch curve
LSystem() {
axiom = "F";
rule = "F+F--F+F";
theta = radians(60.0);
reset();
}

LSystem (String axiomIn, String ruleIn, float thetaIn) {
axiom = axiomIn;
rule = ruleIn;
theta = radians(thetaIn);
reset();
}

void reset() {
production = axiom;
generations = 0;
}

int getAge() {
return generations;
}

//turns the final rule into a drawing on the screen
void render() {

steps = production.length();

for (int i = 0; i < steps; i++) {
char step = production.charAt(i);
if (step == 'F') {
line(0, 0, 0, -drawLength);
translate(0, -drawLength);
}
else if (step == 'L') {
line(0, 0, 0, -(drawLength*2));
translate(0, -(drawLength*2));
}
else if (step == '+') {
rotate(theta);
}
else if (step == '-') {
rotate(-theta);
}
else if (step == '[') {
pushMatrix();
}
else if (step == ']') {
popMatrix();
}
}
}

void applyRuleToAxiom(int gen, int startLength, float scaleFactor) {
drawLength=startLength*scaleFactor;
while (getAge() < gen) {
production = iterate(production, rule);
}
print (axiom);
print (" --> ");
println(production);
}

String iterate(String prod_, String rule_) {
generations++;
String newProduction = prod_;
newProduction = newProduction.replaceAll("F", rule_);
return newProduction;
}
}