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

Friday, 8 January 2010

Stochastic Plant rules Lindenmayer Systems (using custom library)

Here is a processing sketch using my custom lsystem library.

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). This example demonstrates using the StochasticGrammar class.
You can enter different rules for a given premise (character) and you should enter a weighting.
If you don't enter a weighing each rule is given a default weighting of 1.0 therefore each rule will be equally weighted. For non-stochastic rules use the SimpleGrammar test (see revised Isaland fractal for an example).

PS: for those of you who would prefer just to try out my library I have added a simple download to my project on kenai.

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

 

4 comments:

  1. how to add the function of zoom in and zoom out on this script?

    ReplyDelete
    Replies
    1. You could use the scale function from processing. Adjusting the scale factor with mouse wheel (you will need to implement a MouseWheelListener) or mouse drag left/right or up down, or even keyboard entry '^' 'v' '+' '-'.

      Delete
  2. Can I add this script peasycam on? because the output is not on the center if I change startlength. or maybe you can solve with another way. thanks for help my :D

    ReplyDelete
    Replies
    1. I've left it to the user to find out (the hard way) that LSystems quite often do not self centre, you could write some bounding box function if you had to.

      Delete

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