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

Friday, 17 August 2012

Creating 2-D PShape with my LSystems library

  1 /**
  2  * Copyright (c) 2012 Martin Prout
  3  * 
  4  * This demo & library is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2.1 of the License, or (at your option) any later version.
  8  * 
  9  * http://creativecommons.org/licenses/LGPL/2.1/
 10  * 
 11  * This library is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14  * Lesser General Public License for more details.
 15  * 
 16  * You should have received a copy of the GNU Lesser General Public
 17  * License along with this library; if not, write to the Free Software
 18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 19  */
 20 
 21 import lsystem.turtle.Turtle;
 22 import lsystem.Grammar;
 23 import lsystem.SimpleGrammar;
 24 import lsystem.util.LUT;
 25 
 26 /**
 27  * levy.pde  
 28  * (a levy carpet, created by two levy curves use axiom 'FX' for a single curve)
 29  * Demonstrates the use of a Simple Grammar with two 'substitution' rules.
 30  * A CharacterIterator instance used to create the grammar, is re-used to 
 31  * parse the hidden 'production' string.
 32  * lines are 'stored' as a PShape object
 33  * Also features use of my restricted lookup table for sin/cos which takes
 34  * degree input rather than radians (fixed precision of 1 degree), this 
 35  * example however is not improved by use of the lookup table.
 36  */
 37 
 38 final int DELTA = 45; // float only used to match turtle interface
 39 final float REDUCE = 1/sqrt(2);
 40 float drawLength;
 41 float theta;
 42 Grammar grammar;
 43 PShape levy;
 44 
 45 void setup() {
 46   size(600, 600, P2D);
 47   LUT.initialize();  // needed to initialize lookup table
 48   createLSystem();
 49   smooth(8);
 50   translateRules();
 51   background(0);
 52   noLoop();
 53 }
 54 
 55 void draw() {
 56   translate(width/4, height/2);
 57   shape(levy);
 58 }
 59 
 60 void createLSystem() {
 61   int generations = 16;                      // set no of recursions
 62   String axiom = "FX4-FX";  
 63   grammar = new SimpleGrammar(this, axiom);  // initialize library
 64   grammar.addRule('X', "+FX2-FX+");          // add rule
 65   float startLength = 80;
 66   grammar.generateGrammar(generations);
 67   drawLength = startLength * pow(REDUCE, generations);
 68 }
 69 
 70 /**
 71  * Abbreviated form and repeat saves some multiple loops
 72  */
 73 void translateRules() {
 74   int repeat = 1;
 75   Turtle turtle = new Turtle(0, 0, 0);
 76   CharacterIterator it = grammar.getIterator();
 77   levy = createShape(PShape.POLYGON);
 78   levy.noFill();
 79   levy.strokeWeight(2);
 80   levy.stroke(255, 0, 0);
 81   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
 82     switch (ch) {
 83     case 'F':
 84       drawLine(levy, turtle);
 85       break;
 86     case'+':
 87       turnRight(turtle, repeat);
 88       repeat = 1;
 89       break;
 90     case '-':
 91       turnLeft(turtle, repeat);
 92       repeat = 1;
 93       break;
 94     case 'X': // do nothing
 95       break;
 96     case '2':   // set repeat using char ascii code 
 97     case '4':   // 48 = ascii '0'
 98       repeat = (int)ch - 48;
 99       break;  
100     default:
101       System.err.println("character " + ch + " not in grammar");
102       break;
103     }
104   }
105     levy.end(); // yes we don't want close
106   }
107 
108   void drawLine(PShape levy, Turtle turtle) {
109     levy.vertex(turtle.getX(), turtle.getY());
110     turtle.setX(turtle.getX() + drawLength * LUT.cos(turtle.getTheta()));
111     turtle.setY(turtle.getY() - drawLength * LUT.sin(turtle.getTheta()));
112     levy.vertex(turtle.getX(), turtle.getY());
113   }
114 
115   void turnLeft(Turtle turtle, int rep) {
116     turtle.setTheta(turtle.getTheta() + DELTA * rep);
117   }
118 
119   void turnRight(Turtle turtle, int rep) {
120     turtle.setTheta(turtle.getTheta() - DELTA * rep);
121   }
122 

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