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, 8 December 2009

Islands in the processing ide using my lsystem library

NB: Revised version to fit my re-factored custom library 08 January 2010 which I have now extended to support weighted rules (note Grammar is now an interface the two implementing classes are SimpleGrammar and StochasticGrammar for weighted rules).
Here is an example island.pde which I created in the processing ide using my custom lsystem library (available to download from kenai see recent previous postings for the link).

   1 import lsystem.*;  // new library since 8 January 2008
   2 
   3 import java.text.CharacterIterator;
   4 import java.text.StringCharacterIterator;
   5 
   6 Grammar grammar; 
   7 float drawLength;
   8 float xpos;
   9 float ypos;
  10 float DELTA = PI/2; // 90 degrees
  11 float theta = 0;
  12 String production;
  13 ArrayList points;
  14 
  15 void setup() {
  16   size(500, 500);
  17   createLSystem();
  18   points = new ArrayList();
  19   stroke(255);
  20   noFill();
  21   smooth();
  22   xpos = width/4;
  23   ypos = height/4;
  24   translateRules();
  25   noLoop();
  26 }
  27 
  28 void createLSystem(){
  29   int generations = 2;
  30   String axiom = "F-F-F-F";
  31   grammar = new SimpleGrammar(this, axiom); // new since 8 January 2008
  32   grammar.addRule('F', "F+f-FF+F+FF+Ff+FF-f+FF-F-FF-Ff-FFF");
  33   grammar.addRule('f', "ffffff");
  34   float startLength = 1000;
  35   production = grammar.createGrammar(generations);
  36   drawLength = startLength * pow((1/12.0), generations); 
  37 }
  38 
  39 void translateRules() {
  40   CharacterIterator it = new StringCharacterIterator(production);
  41   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  42     switch(ch){
  43     case 'F':
  44       float[] temp = {
  45         xpos, ypos, (xpos += drawLength * cos(theta)), (ypos -= drawLength * sin(theta))
  46       };
  47       points.add(temp);
  48       break;
  49     case 'f':
  50       xpos += drawLength * cos(theta)/2;
  51       ypos -= drawLength * sin(theta)/2;
  52       break;
  53     case '+':
  54       theta += (DELTA);
  55       break;
  56     case '-':
  57       theta -= (DELTA);
  58       break;
  59     default:
  60       System.err.println("character " + ch + " not in grammar");
  61     }
  62   }
  63 }
  64 
  65 void draw() {
  66   background(0);
  67   float[] tmp; // placeholder for array of points that describe line 
  68   for (int i = 0; i < points.size(); i++)
  69   { 
  70     tmp = (float[])points.get(i);
  71     line(tmp[0], tmp[1], tmp[2], tmp[3]);
  72   }
  73 }

 

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