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

Tuesday, 9 February 2010

A Hilbert from multiple rules

Thanks to Jonathan Feinberg here is a Hilbert built from a four rule LSystem (Jonathan is also the creator of the peasycam library). Here I've used abbreviated grammar, replacing repeated instructions with the number of repeats (1 in this case means repeat one more 'rotation'). What I'd really like to do is replace the rotate and translate calls with a call to a 3D line(x, y, z, x1, x1, y1), so far I've managed this for single generation with the previous single rule, perhaps this multiple substitution rule will enable me to do it for multiple generations!!!!

   1 import processing.opengl.*;
   2 import lsystem.*;
   3 import peasy.*;
   4 
   5 String axiom = "A";
   6 String production;
   7 float THETA = HALF_PI;
   8 float PHI = HALF_PI;
   9 PeasyCam cam;
  10 Grammar grammar;
  11 
  12 void setup()
  13 {
  14   size(600, 600, OPENGL);
  15   configureOpenGL();
  16   cam = new PeasyCam(this, -70,70,-70,250);
  17   cam.rotateX(PI/5);
  18   cam.rotateY(PI/5);
  19   noStroke();
  20   grammar = new SimpleGrammar(this, axiom);
  21   grammar.addRule('A', "B>F<CFC<F>D+F-D>F<1+CFC<F<B1^");
  22   grammar.addRule('B', "A+F-CFB-F-D1->F>D-1>F-B1>FC-F-A1^");
  23   grammar.addRule('C', "1>D-1>F-B>F<C-F-A1+FA+F-C<F<B-F-D1^");
  24   grammar.addRule('D', "1>CFB>F<B1>FA+F-A1+FB>F<B1>FC1^");
  25   production = grammar.createGrammar(3);
  26 }
  27 
  28 void configureOpenGL(){
  29   hint(ENABLE_OPENGL_4X_SMOOTH);
  30   hint(DISABLE_OPENGL_ERROR_REPORT);
  31 }
  32  
  33 public void draw()
  34 {
  35   int repeats = 1;
  36   int col = color(0, 225, 0);
  37   lights();
  38   directionalLight(128, 128, 128, 0, 0, 1);
  39   background(0);
  40   CharacterIterator it = new StringCharacterIterator(production);
  41   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  42     switch (ch) {
  43     case 'F':
  44       fill(col);
  45       float len = 20;
  46       translate(0, 0, -len / 2);
  47       box(3, 3, len - 1.6);      
  48       translate(0, 0, -len / 2);
  49       box(3, 3, 3);
  50       break;
  51     case '+':
  52       rotateX(THETA * repeats);
  53       repeats = 1;
  54       break;
  55     case '-':
  56       rotateX(-THETA * repeats);
  57       repeats = 1;
  58       break;  
  59     case '>':
  60       rotateY(THETA * repeats);
  61       repeats = 1;
  62       break;
  63     case '<':
  64       rotateY(-THETA * repeats);
  65       repeats = 1;
  66       break;
  67     case '^':
  68       rotateZ(PHI * repeats);
  69       repeats = 1;
  70       break;
  71     case '1':
  72       repeats += 1;  
  73     case 'A':
  74     case 'B':
  75     case 'C':
  76     case 'D':
  77       break;
  78     default:
  79       System.err.println("character " + ch + " not in grammar");  
  80     }
  81   }
  82 }

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