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, 11 February 2010

A David Tour Fractal (weird LSystem rules)

I got these fractal rules from a paper by A. Mariano, P. Mascato & M. G. Norman, who were investigating the Euclidean traveling salesman problem. The loops can be seen as cities...
My library holds up, note the use of the ternary operator to save several lines of code, almost certainly more efficient (it was when I did the ruby-processing version that it was an obvious use, which suggests to me that it is the language you program in influences the way you think!!!). To download the code follow the link below:-

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 }

Wednesday, 3 February 2010

Improved loading for Open GL Applets

The latest version of processing-1.1 has a much improved method of loading opengl applets, checkout my 3D Sierpinski to see if you agree:-
my 3D Sierpinski

Tuesday, 2 February 2010

An improved (stochastic) 3D tree using LSystems and PeasyCam

My library project is currently still available at Kenai, but I've just been officially warned that Oracle (new owners in case you hadn't heard) are pulling the plug on Kenai apart from their private use. A pity because it was well very well integrated with NetBeans ide. I will probably transfer my project to GitHub because I know it very well from collaborating on ruby-processing projects (it is unquestionably the place for ruby projects).

   1 import processing.opengl.*;
   2 import java.text.*;
   3 import lsystem.*;  // My custom LSystem library available at Kenai version 0.5.0
   4                    // http://kenai.com/projects/l-system-utilities/downloads
   5 import peasy.*;    // PeasyCam custom library v0.8.1 available at 
   6                    // http://mrfeinberg.com/peasycam/#download
   7 PeasyCam cam;
   8 Grammar grammar;
   9 
  10 float distance = 72;
  11 int depth = 3;
  12 float theta = radians(23);
  13 float phi = radians(27);
  14 String production = "";
  15 
  16 void setup() {
  17   size(800, 800, OPENGL);
  18   configureOpenGL();  
  19   cam = new PeasyCam(this, 100);  
  20   cam.setMinimumDistance(100);
  21   cam.setMaximumDistance(500);
  22   setupGrammar();
  23   noStroke();  
  24 }
  25 
  26 void configureOpenGL(){
  27   hint(ENABLE_OPENGL_4X_SMOOTH);
  28   hint(DISABLE_OPENGL_ERROR_REPORT);
  29 }
  30 
  31 void setupGrammar(){
  32   grammar = new StochasticGrammar(this, "F");   // this only required to allow applet to call dispose()
  33   grammar.addRule('F', "F[&+F]F>>[->F]>>[->F]>>[&F]");
  34   grammar.addRule('F', "F>[&+F]F[->F]>[->F]>[&F]");
  35   grammar.addRule('F', "F>>[&+F]>F[->F][->F][&F]");
  36   production = grammar.createGrammar(depth);
  37   distance *= pow(0.5, depth);
  38 }
  39 
  40 void draw() {
  41   background(0);
  42   lights();
  43   render();
  44 }
  45 
  46 void render() {
  47   translate(0, 40, 0);  // center the plant empirically set looks OK to me
  48   fill(0, 175, 0); 
  49   lightSpecular(0, 204, 0); 
  50   specular(0, 255, 0); 
  51   shininess(1.0); 
  52   CharacterIterator it = new StringCharacterIterator(production);
  53   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  54     switch (ch) {
  55     case 'F': 
  56       translate(0, distance/-2, 0);
  57       box(distance/9, distance, distance/9);
  58       translate(0, distance/-2, 0);
  59       break;
  60     case '-':
  61       rotateX(theta);
  62       break;
  63     case '+':
  64       rotateX(-theta);
  65       break;
  66     case '>':
  67       rotateY(theta);
  68       break;
  69     case '<':
  70       rotateY(-theta);
  71       break;
  72     case '&':
  73       rotateZ(-phi);
  74       break;
  75     case '^':
  76       rotateZ(phi);
  77       break;
  78     case '[':
  79       pushMatrix();
  80       break;
  81     case ']':
  82       popMatrix();
  83       break;   
  84     default:
  85       System.err.println("character " + ch + " not in grammar");
  86     }
  87   }
  88 }



Follow this link to see an animated version.

Monday, 1 February 2010

Animated LSystems (A Mark Meyer classic)

Adapted for processing from lsystem-animated.py uses my custom lsystem library. I should check whether I could use my Pen or Turtle classes to hold state.

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