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

Wednesday, 5 May 2010

Penrose Tiling in Processing using my LSystems Utilities

   1 /** 
   2 * penroseTiling.pde
   3 * Loosely based on a processing PenroseTiling
   4 * processing sketch by Geraldine Sarmiento 
   5 */
   6 
   7 import lsystem.*; // My custom LSystem library available at Kenai version 0.6.0
   8                   // http://kenai.com/projects/l-system-utilities/downloads
   9 
  10 ArrayList points;
  11 final float DELTA = PI/5; // 36 degrees
  12 Grammar grammar; 
  13 String axiom;
  14 String rule;
  15 String production;
  16 float startLength;
  17 float drawLength;
  18 float theta;
  19 float xpos;
  20 float ypos;
  21 TurtleStack ts;
  22 
  23 
  24 void setup() {
  25   size(500, 600);
  26   createLSystem();
  27   points = new ArrayList();
  28   ts = new TurtleStack(this);
  29   stroke(0, 255, 0, 60);
  30   strokeWeight(3);
  31   noFill();
  32   smooth();
  33   translateRules();
  34   background(0);
  35   noLoop();
  36 }
  37 
  38 void createLSystem(){
  39   int generations = 4;                 // set no of recursions
  40   String axiom = "[X]2+[X]2+[X]2+[X]2+[X]";  
  41   grammar = new SimpleGrammar(this, axiom);  // initialize custom library
  42   grammar.addRule('F', "");                 // add char substitution rules
  43   grammar.addRule('W', "YF2+ZF4-XF[-YF4-WF]2+");
  44   grammar.addRule('X', "+YF2-ZF[3-WF2-XF]+");
  45   grammar.addRule('Y', "-WF2+XF[3+YF2+ZF]-");
  46   grammar.addRule('Z', "2-YF4+WF[+ZF4+XF]2-XF");
  47 
  48   float startLength = 500;
  49   production = grammar.createGrammar(generations);
  50   drawLength = startLength * pow(0.5, generations); 
  51 }
  52 
  53 void translateRules() {
  54   float x_temp, y_temp;
  55   int repeat = 1;
  56   Turtle turtle = new Turtle(width/2, height/2, 0);
  57   CharacterIterator it = new StringCharacterIterator(production);
  58   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  59     switch (ch) {
  60     case 'F':
  61       x_temp = turtle.getX();
  62       y_temp = turtle.getY();
  63       turtle.setX(x_temp - drawLength * cos(turtle.getTheta()));
  64       turtle.setY(y_temp - drawLength * sin(turtle.getTheta()));
  65       float[] temp = {
  66         x_temp, y_temp, turtle.getX(), turtle.getY()      };
  67       points.add(temp);
  68       break;
  69     case '+':
  70       turtle.setTheta(turtle.getTheta() + DELTA * repeat);
  71       repeat = 1;
  72       break;
  73     case '-':
  74       turtle.setTheta(turtle.getTheta() - DELTA * repeat);
  75       repeat = 1;
  76       break;
  77     case '[':
  78       ts.push(turtle.clone()); 
  79       break;
  80     case ']':
  81       turtle = ts.pop();
  82       break;
  83     case 'W':   // do nothing other than
  84     case 'X':   // confirm W,X,Y&Z as valid grammar
  85     case 'Y':
  86     case 'Z':
  87       break;
  88     case '2':   // set repeat using char ascii code 
  89     case '3':   // 48 = ascii '0'
  90     case '4':
  91       repeat = (int)ch - 48;
  92       break;  
  93     default:
  94       System.err.println("character " + ch + " not in grammar");
  95       break;
  96     }
  97   }
  98 }
  99 
 100 void draw() {
 101   float[] point;
 102   for (int i = 0; i < points.size(); i++) {
 103     point = (float[])points.get(i);
 104     line(point[0], point[1], point[2], point[3]);
 105   }
 106 }
 107 


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