Previously, several of us were riffing on Sierpinski variants, this dragged me to OpenProcessing and set me on task to update some of my older contributions. Also @neilcsmith set me thinking about poor old Ben Tilbert, and it was about time he saw the light of day again.
My LSystem library is still available on github, but I never got on with Prisoner Johns template so it needs a manual install, here’s the sketch code:-
/**
* A 3D LSystem example with a SimpleGrammar
* This LSystem library is available at Github
* https://github.com/monkstone/LSystems
*/
/*
* Copyright (c) 2011-20 Martin Prout
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.text.CharacterIterator;
import lsystem.Grammar;
import lsystem.SimpleGrammar;
import lsystem.turtle.RodTurtle;
import lsystem.util.ArcBall;
import lsystem.util.LUT;
import peasy.PeasyCam;
float HALF = PI/360; // half a degree error
String axiom = "A";
String production;
float THETA = HALF_PI + HALF;
float PHI = HALF_PI - HALF;
PeasyCam cam;
Grammar grammar;
void setup()
{
size(600, 600, P3D);
cam = new PeasyCam(this, -70,70,-70,250);
cam.rotateX(PI/5);
cam.rotateY(PI/5);
noStroke();
grammar = new SimpleGrammar(this, axiom);
grammar.addRule('A', "B>F<CFC<F>D+F-D>F<1+CFC<F<B1^");
grammar.addRule('B', "A+F-CFB-F-D1->F>D-1>F-B1>FC-F-A1^");
grammar.addRule('C', "1>D-1>F-B>F<C-F-A1+FA+F-C<F<B-F-D1^");
grammar.addRule('D', "1>CFB>F<B1>FA+F-A1+FB>F<B1>FC1^");
grammar.generateGrammar(3);
}
public void draw()
{
int repeats = 1;
int col = color(0, 225, 0);
lights();
directionalLight(128, 128, 128, 0, 0, 1);
background(0);
CharacterIterator it = grammar.getIterator();
for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
switch (ch) {
case 'F':
fill(col);
float len = 20;
translate(0, 0, -len / 2);
box(3, 3, len - 1.6);
translate(0, 0, -len / 2);
box(3, 3, 3);
break;
case '+':
rotateX(THETA * repeats);
repeats = 1;
break;
case '-':
rotateX(-THETA * repeats);
repeats = 1;
break;
case '>':
rotateY(THETA * repeats);
repeats = 1;
break;
case '<':
rotateY(-THETA * repeats);
repeats = 1;
break;
case '^':
rotateZ(PHI * repeats);
repeats = 1;
break;
case '1':
repeats += 1;
case 'A':
case 'B':
case 'C':
case 'D':
break;
default:
System.err.println("character " + ch + " not in grammar");
}
}
}
The character iterator is no longer needed with recent java since we can now switch on a String.