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

Heighway Dragon Using L-Systems and processing

Another example demonstrating the usefulness of a custom library, you barely have to think to produce an interesting fractal graphic (well once you have a bit of a grasp of the rules or access to a crib sheet). Note I have posted my library on kenai "http://kenai.com/projects/l-system-utilities" from where you can check it out (example now works with revised library 8th January 2010).

   1 import lsystem.*;
   2 
   3 // dragon.pde
   4  
   5 
   6 final float DELTA = (PI/180) * 45; // 45 degrees
   7 final float REDUCE = 1/sqrt(2);
   8 String production;
   9 float drawLength;
  10 float theta;
  11 float xpos;
  12 float ypos;
  13 ArrayList points;
  14 Grammar grammar;
  15 
  16 void setup() {
  17   size(600, 500);
  18   createLSystem();
  19   points = new ArrayList();
  20   stroke(255, 0, 0);
  21   noFill();
  22   smooth();
  23   translateRules();
  24   background(0);
  25   noLoop();
  26 }
  27 
  28 void createLSystem(){
  29   int generations = 14;                 // set no of recursions
  30   String axiom = "FX";  // NB  '-' without prefix repeat = 1 (so 7 means 8 repeats or PI/2)
  31   grammar = new SimpleGrammar(this, axiom);  // initialize custom library
  32   grammar.addRule('X', "+FX--FY+");    // add rule
  33   grammar.addRule('Y', "-FX++FY-");
  34   float startLength = 100;
  35   production = grammar.createGrammar(generations);
  36   drawLength = startLength * pow(REDUCE, generations); 
  37 }
  38 
  39 void translateRules() {
  40   xpos = width*0.7;
  41   ypos = height/3;
  42   float x_temp, y_temp;
  43   CharacterIterator it = new StringCharacterIterator(production);
  44   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  45     switch (ch) {
  46     case 'F':
  47       x_temp = xpos;
  48       y_temp = ypos;
  49       xpos -= drawLength * cos(theta);
  50       ypos += drawLength * sin(theta);
  51       float[] point = {
  52         x_temp, y_temp, xpos, ypos};
  53       points.add(point);
  54       break;
  55     case '+':
  56       theta += (DELTA);
  57       break;
  58     case '-':
  59       theta -= (DELTA);
  60       break;
  61     case 'X':  // recognize grammar and do nothing
  62       break;
  63     case 'Y':  // recognize grammar and do nothing
  64       break;
  65     default:
  66       System.err.println("character " + ch + " not in grammar");
  67     }
  68   }
  69 }
  70 
  71 void draw() {
  72   float[] point;
  73   for (int i = 0; i < points.size(); i++) {
  74     point = (float[])points.get(i);
  75     line(point[0], point[1], point[2], point[3]);
  76   }
  77 }


 

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