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

Peano curve fractal using Lindenmayer Systems

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



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