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, 12 January 2010

A 3D tree using my lsystem library for processing

   1 /**
   2 * Three D Tree using my lsystem library
   3 * using some ideas from 
   4 * http://web.mit.edu/~eric_r/Public/lsystems/
   5 */
   6 
   7 import processing.opengl.*;
   8 import java.text.*;
   9 import lsystem.*;
  10 
  11 String axiom = "F";
  12 Grammar grammar;
  13 String rule = "F[z+F]F[-aF][-aF][zF]"; 
  14 float distance = 5;
  15 int depth = 5;
  16 float theta = radians(28);
  17 float phi = radians(45);
  18 float scaleFactor = 0.8;
  19 float xrot = 0;
  20 float yrot = 0;
  21 String production = "";
  22 char premis = 'F'; 
  23 
  24 void setup() {
  25   size(800, 800, OPENGL);
  26   grammar = new SimpleGrammar(axiom);
  27   grammar.addRule(premis, rule);
  28   production = grammar.createGrammar(depth);
  29   noStroke();
  30 }
  31 
  32 void draw() {
  33   background(0);
  34   translate(width / 2, height * 0.9);
  35   rotateY(xrot);
  36   rotateX(yrot);
  37   lights();
  38   camera(mouseX, 30, 230.0, // eyeX, eyeY, eyeZ
  39   0.0, 0.8, 0.0,            // centerX, centerY, centerZ
  40   0.0, 1.0, 0.0);
  41   render();
  42 }
  43 
  44 void render() {
  45   CharacterIterator it = new StringCharacterIterator(production);
  46   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  47     switch (ch) {
  48     case 'F':      
  49       fill(0, 255, 0);
  50       translate(0, distance/-2, 0);
  51       box(distance/4 , distance, distance/4);
  52       translate(0, distance/-2, 0);
  53       break;
  54     case '-':
  55       rotateX(-theta);
  56       break;
  57     case '+':
  58       rotateX(theta);
  59       break;
  60     case 'z':
  61       rotateZ(phi);
  62       break;
  63     case 'a':
  64       rotateZ(-phi);
  65       break;
  66     case '[':
  67       pushMatrix();
  68       distance = distance * scaleFactor;
  69       break;
  70     case ']':
  71       popMatrix();
  72       distance = distance / scaleFactor;
  73       break;
  74     default:
  75       System.err.println("character " + ch + " not in grammar");
  76     }
  77   }
  78 }

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