topical media & game development
graphic-processing-site-examples-Topics-Fractals-and-L-Systems-PenroseTile-PenroseLSystem.pde / pde
class PenroseLSystem extends LSystem {
int steps = 0;
float somestep = 0.1;
String ruleW;
String ruleX;
String ruleY;
String ruleZ;
PenroseLSystem() {
axiom = "[X]++[X]++[X]++[X]++[X]";
ruleW = "YF++ZF4-XF[-YF4-WF]++";
ruleX = "+YF--ZF[3-WF--XF]+";
ruleY = "-WF++XF[+++YF++ZF]-";
ruleZ = "--YF++++WF[+ZF++++XF]--XF";
startLength = 460.0;
theta = radians(36);
reset();
}
void useRule(String r_) {
rule = r_;
}
void useAxiom(String a_) {
axiom = a_;
}
void useLength(float l_) {
startLength = l_;
}
void useTheta(float t_) {
theta = radians(t_);
}
void reset() {
production = axiom;
drawLength = startLength;
generations = 0;
}
int getAge() {
return generations;
}
void render() {
translate(width/2, height/2);
int pushes = 0;
int repeats = 1;
steps += 12;
if (steps > production.length()) {
steps = production.length();
}
for (int i = 0; i < steps; i++) {
char step = production.charAt(i);
if (step == 'F') {
stroke(255, 60);
for (int j = 0; j < repeats; j++) {
line(0, 0, 0, -drawLength);
noFill();
translate(0, -drawLength);
}
repeats = 1;
}
else if (step == '+') {
for (int j = 0; j < repeats; j++) {
rotate(theta);
}
repeats = 1;
}
else if (step == '-') {
for (int j =0; j < repeats; j++) {
rotate(-theta);
}
repeats = 1;
}
else if (step == '[') {
pushes++;
pushMatrix();
}
else if (step == ']') {
popMatrix();
pushes--;
}
else if ( (step >= 48) && (step <= 57) ) {
repeats = (int)step - 48;
}
}
// Unpush if we need too
while (pushes > 0) {
popMatrix();
pushes--;
}
}
String iterate(String prod_, String rule_) {
String newProduction = "";
for (int i = 0; i < prod_.length(); i++) {
char step = production.charAt(i);
if (step == 'W') {
newProduction = newProduction + ruleW;
}
else if (step == 'X') {
newProduction = newProduction + ruleX;
}
else if (step == 'Y') {
newProduction = newProduction + ruleY;
}
else if (step == 'Z') {
newProduction = newProduction + ruleZ;
}
else {
if (step != 'F') {
newProduction = newProduction + step;
}
}
}
drawLength = drawLength * 0.5;
generations++;
return newProduction;
}
}
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.