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

Sunday, 10 January 2010

Applying some contextual rules L-Systems

Here is a sketch to match my Kenai project image (I cheated before, as the image was produced using my ruby-processing implementation of L-Systems).

   1 import lsystem.*;
   2 
   3 // seasonal.pde
   4 final float DELTA = QUARTER_PI/2;
   5 final float REDUCE = 0.9;
   6 String production;
   7 float drawLength;
   8 float theta;
   9 float xpos;
  10 float ypos;
  11 ArrayList points;
  12 Grammar grammar;
  13 
  14 void setup() {
  15   size(400, 400);
  16   theta = -HALF_PI;
  17   setupLSystem();
  18   points = new ArrayList();
  19   fill(255, 0, 0); // red berries
  20   smooth();
  21   background(0);
  22   translateRules();
  23   noLoop();
  24 }
  25 
  26 void setupLSystem(){
  27   int generations = 3;                           // set no of recursions
  28   String axiom = "F";  
  29   grammar = new SimpleGrammar(this, axiom);      // initialize custom library
  30   grammar.addRule('F', "F[-Fb]F[+Fb][Fb]");         // add rule, with branches[]
  31   float startLength = 30;
  32   production = grammar.createGrammar(generations);
  33   // reduce drawLength with each generation
  34   drawLength = startLength * pow(REDUCE, generations);
  35 }
  36 
  37 void translateRules() {
  38   TurtleStack ts = new TurtleStack(this);
  39   xpos = width/2;
  40   ypos = height * 0.95;
  41   Turtle turtle;
  42   int branch = 0;
  43   float x_temp, y_temp;
  44   CharacterIterator it = new StringCharacterIterator(production);
  45   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  46     switch (ch) {
  47     case 'F':
  48       x_temp = xpos;
  49       y_temp = ypos;
  50       xpos -= drawLength * cos(theta);
  51       ypos += drawLength * sin(theta);
  52       stroke(0, 255, 0);
  53       strokeWeight(2);
  54       line(x_temp, y_temp, xpos, ypos);
  55       noStroke();
  56       break;
  57     case '+':
  58       theta += (DELTA);
  59       break;
  60     case '-':
  61       theta -= (DELTA);
  62       break;
  63     case '[':
  64       branch++;
  65       ts.push(new Turtle(xpos, ypos, theta));
  66       break;
  67     case ']':
  68       branch--;
  69       turtle = ts.pop();
  70       xpos = turtle.getX();
  71       ypos = turtle.getY();
  72       theta = turtle.getTheta();       
  73       break;
  74     case 'b':
  75       if (branch >= 3) // only latest branches bear fruit
  76       {ellipse(xpos, ypos, drawLength/3, drawLength/3);}
  77       break;
  78     default:
  79       System.err.println("character " + ch + " not in grammar");
  80     }
  81   }
  82 }


2 comments:

  1. Hello, I just discovered your blog and your LSystem library. I want to thank you for sharing all this awesome work.
    So... thank you!

    ReplyDelete
  2. aggaz thanks for the comment. I just checked out your blog, and I see you have an interest in context free art, you might be interested in some of my earlier posts. I spent quite a bit of time experimenting with it myself.

    ReplyDelete

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