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

Saturday, 22 May 2010

A Fern Fractal Using my LSystem Library Utilities

   1 // Fern.pde
   2 import lsystem.*;
   3 
   4 float REDUCE1 = 0.85;
   5 float REDUCE2 = 0.33;
   6 float DELTA = PI/180 * 5.4;
   7 float startLength = 165;
   8 float drawLength;
   9 int startColor = 255<<24|0<<16|255<<8|0; // a light green stem?
  10 int decrement = 10 << 8; // decrement green by 10
  11 
  12 Grammar grammar;
  13 String production;
  14 PenStack ps;
  15 
  16 void setup() {
  17   size(800, 800);
  18   background(0);
  19   drawLength = startLength;
  20   smooth();
  21   ps = new PenStack();
  22   grammar = new SimpleGrammar(this, "FD");
  23   grammar.addRule('D', "C+@FD");
  24   grammar.addRule('C', "B");
  25   grammar.addRule('B', "[6+#FD][7-#FD]"); // abbreviated grammar
  26   production = grammar.createGrammar(22);
  27   background(0);
  28   translateRules();
  29 }
  30 
  31 void translateRules() {
  32   int repeat = 1;
  33   PenStack penStack = new PenStack(this);
  34   float xpos = 20;
  35   float ypos = 720;
  36   float theta = -PI/2.8;
  37   float x_temp, y_temp;
  38   Pen pen= new Pen(this, xpos, ypos, theta, drawLength, startColor);
  39   CharacterIterator it = grammar.getIterator(production);  
  40   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  41     switch (ch) {
  42     case 'F':          
  43       drawLine(pen);
  44       break;
  45     case '+':
  46       pen.setTheta(pen.getTheta() + DELTA * repeat);
  47       repeat = 1;
  48       break;
  49     case '-':
  50       pen.setTheta(pen.getTheta() - DELTA * repeat);
  51       repeat = 1;
  52       break;
  53     case '[':          
  54       penStack.push(pen.clone());
  55       break;
  56     case ']':
  57       pen = penStack.pop();
  58       break;
  59     case '@':
  60       pen.setColor(pen.getColor() - decrement);
  61       pen.setLength(pen.getLength()*REDUCE1);  // reduce 0.9 length
  62       break;
  63     case '#': 
  64       pen.setColor(pen.getColor() - decrement * 2);  
  65       pen.setLength(pen.getLength()*REDUCE2);  // reduce 0.33 length 
  66       break; 
  67     case '6':  
  68     case '7':
  69       repeat = (int)ch - 48;
  70       break;   
  71     case 'B': 
  72     case 'C': 
  73     case 'D': 
  74       break;
  75     default:
  76       System.err.println("character " + ch + " not in grammar");
  77     }
  78   }
  79 }
  80 
  81 void drawLine(Pen pen) {
  82   float x_temp = pen.getX(); 
  83   float y_temp = pen.getY();
  84   pen.setX(x_temp + pen.getLength() * cos(pen.getTheta()));
  85   pen.setY(y_temp + pen.getLength() * sin(pen.getTheta())); 
  86   stroke(pen.getColor());
  87   line(x_temp, y_temp, pen.getX(), pen.getY());
  88 }


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