Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Tuesday, 1 December 2009

Experimenting with Lindenmayer System in java processing

Here is a processing sketch I originally created in netbeans, one thing that I've done here is to avoid affine transforms, it might be slightly easier to code one plant. However if you want to create more than one plant, then you might just as well work out the points. To preserve context (for branching) I have created a turtle to store position and direction and a custom turtle stack.
This and the grammar parser I have bundled into a custom library.
Just for the experience I have posted my library on kenai "http://kenai.com/projects/l-system-utilities" from where you will be able to check it out using either git or svn. If you want to try out my library from the processing ide, put the LSystem.jar in a folder in your sketchbook folder nested as follows:-
"sketchbook/libraries/LSystem/library/LSystem.jar", then you will find you can import the library into your sketch in the usual way. (sketch re-factored to run in processing using revised library 8 January 2010).

   1 // plant.pde
   2 import lsystem.*;
   3 
   4 ArrayList points;
   5 final float DELTA = PI/10; // 18 degrees
   6 Grammar grammar; 
   7 String axiom;
   8 String rule;
   9 String production;
  10 float startLength;
  11 float drawLength;
  12 float theta;
  13 float xpos;
  14 float ypos;
  15 TurtleStack ts;
  16 Turtle ttle;
  17 
  18 void setup() {
  19   size(500, 600);
  20   createLSystem();
  21   points = new ArrayList();
  22   ts = new TurtleStack();
  23   stroke(0, 255, 0);
  24   strokeWeight(4);
  25   noFill();
  26   smooth();
  27   translateRules();
  28   background(0);
  29   noLoop();
  30 }
  31 
  32 void createLSystem(){
  33   int generations = 5;                 // set no of recursions
  34   String axiom = "F";  // NB  '-' without prefix repeat = 1 (so 7 means 8 repeats or PI/2)
  35   grammar = new SimpleGrammar(this, axiom);  // initialize custom library
  36   grammar.addRule('F', "3F[2-F]2F[+F][F]"); // add rule
  37   float startLength = 18;
  38   production = grammar.createGrammar(generations);
  39   drawLength = startLength * pow(0.7, generations); 
  40 }
  41 
  42 void translateRules() {
  43   int repeats = 1;
  44   theta = HALF_PI;
  45   xpos = 0.4 * height;
  46   ypos = 0.98 * width;
  47   float x_temp, y_temp;
  48   CharacterIterator it = new StringCharacterIterator(production);
  49   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  50     switch (ch) {
  51     case 'F':
  52       x_temp = xpos;
  53       y_temp = ypos;
  54       xpos += drawLength * repeats * cos(theta);
  55       ypos -= drawLength * repeats * sin(theta);
  56       float[] point = {
  57         x_temp, y_temp, xpos, ypos};
  58       points.add(point);
  59       repeats = 1;
  60       break;
  61     case '+':
  62       theta += (DELTA * repeats);
  63       repeats = 1;
  64       break;
  65     case '-':
  66       theta -= (DELTA * repeats);
  67       repeats = 1;
  68       break;
  69     case '[':
  70       ts.push(new Turtle(xpos, ypos, theta));
  71       break;
  72     case ']':
  73       ttle = ts.pop();
  74       xpos = ttle.getX();
  75       ypos = ttle.getY();
  76       theta = ttle.getTheta();
  77       break;
  78     case '2':
  79       repeats = 2;
  80       break;
  81     case '3':
  82       repeats = 3;
  83       break;
  84     case '4':
  85       repeats = 4;
  86       break;
  87     case '5':
  88       repeats = 5;
  89       break;
  90     case 'X':
  91       break;
  92     default:
  93       System.err.println("character " + ch + " not in grammar");
  94     }
  95   }
  96 }
  97 
  98 void draw() {
  99   float[] point;
 100   for (int i = 0; i < points.size(); i++) {
 101     point = (float[])points.get(i);
 102     line(point[0], point[1], point[2], point[3]);
 103   }
 104 }
 

 

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2