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

Thursday, 3 December 2009

Here is a penrose snowflake I have created with my custom library

Here is another application of my lsystem library. The library has custom rule class to store the string replacement rule. The main feature is a parser that can cope with multiple rules (see plant of my previous post). The Turtle and TurtleStack are lightweight replacements for the processing matrix functions. Just for the experience I have posted my library on kenai "http://kenai.com/projects/l-system-utilities" from where you will be able to check it out (this is the revised version as of 8 January 2010).
   1 import lsystem.*;
   2 
   3 // penrose.pde
   4 
   5 
   6 float drawLength;
   7 float xpos;
   8 float ypos;
   9 float DELTA = PI/10; // 18 degrees
  10 ArrayList points;
  11 float theta = 0;
  12 Grammar grammar;
  13 TurtleStack ts;
  14 String production;
  15 
  16 void setup() {
  17   size(1000, 1000);
  18   createLSystem();
  19   points = new ArrayList();
  20   stroke(255, 0, 0);
  21   noFill();
  22   smooth();
  23   translateRules();
  24   noLoop();
  25 }
  26 
  27 void createLSystem(){
  28   int generations = 3;                 // set no of recursions
  29   String axiom = "F3-F3-F3-F3-F3-";  // NB  '-' without prefix repeat = 1 (so 3 means 4 repeats or 72 degrees)
  30   grammar = new SimpleGrammar(this, axiom);  // initialize custom library SimpleGrammar (non-stochastic)
  31   grammar.addRule('F', "F3-F3-F45-F++F3-F"); // add rule
  32   float startLength = 480;
  33   production = grammar.createGrammar(generations);
  34   drawLength = startLength * pow(0.4, generations); 
  35 }
  36 
  37 void translateRules() {
  38   TurtleStack ts = new TurtleStack(this);
  39   xpos = width/4;
  40   ypos = height * 0.9;
  41   int repeats = 1;
  42   CharacterIterator it = new StringCharacterIterator(production);
  43   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  44     Turtle turtle;
  45     switch(ch){
  46     case 'F':
  47       float[] temp = {
  48         xpos, ypos, (xpos += drawLength * repeats * cos(theta)), (ypos += drawLength * repeats * sin(theta))
  49       };
  50       points.add(temp);
  51       repeats = 1;
  52       break;
  53     case '+':
  54       theta += (DELTA * repeats);
  55       repeats = 1;
  56       break;
  57     case '-':
  58       theta -= (DELTA * repeats);
  59       repeats = 1;
  60       break;
  61     case '[':
  62       ts.push(new Turtle(xpos, ypos, theta));
  63       break;
  64     case ']':
  65       turtle = ts.pop();
  66       xpos = turtle.getX();
  67       ypos = turtle.getY();
  68       theta = turtle.getTheta();
  69       break;
  70     case '3':
  71       repeats += 3;
  72       break;
  73     case '4':
  74       repeats += 4;
  75       break;
  76     case '5':
  77       repeats += 5;
  78       break;
  79     default:
  80       System.err.println("character " + ch + " not in grammar");
  81     }
  82   }
  83 }
  84 
  85 void draw() {
  86   background(0);
  87   float[] tmp;
  88   for (int i =0; i < points.size(); i++)
  89   { 
  90     tmp = (float[])points.get(i);
  91     line(tmp[0], tmp[1], tmp[2], tmp[3]);
  92   }
  93 }
  94 
  95 


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