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, 19 May 2010

Circular Tiling using LSystem rules

Updated for LSystem-0.81 (21 July 2012)
import lsystem.turtle.*;
import lsystem.collection.*;
import lsystem.*;

/** 
 * circularTiling.pde
 * Based on FractInt rules 
 * by Adrian Mariano 
 *
 * My custom LSystem library available at Kenai version 0.8.1
 * http://kenai.com/projects/l-system-utilities/downloads
 */

import lsystem.*; 

final float DELTA = PI/12; // 15 degrees
Grammar grammar; 
String axiom;
String rule;
float startLength;
float drawLength;
float theta;
float xpos;
float ypos;
TurtleStack ts;

void setup() {
  size(600, 600);
  createLSystem();
  ts = new TurtleStack(this);
  strokeWeight(2);
  noFill();
  smooth();
  background(0);
  translateRules();
}

void createLSystem() {
  int generations = 6;                    // set no of recursions
  axiom = "X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X";  
  grammar = new SimpleGrammar(this, axiom);  // initialize custom library
  grammar.addRule('X', "[F+F+F+F[3-X-Y]5+F8+F-F-F-F]");
  grammar.addRule('Y', "[F+F+F+F[3-Y]5+F8+F-F-F-F]");
  startLength = 800;
  grammar.generateGrammar(generations);
  drawLength = startLength * pow(0.5, generations);
}

void translateRules() {
  float x_temp, y_temp;
  int repeat = 1;
  Turtle turtle = new Turtle(width/2, height/2, 0.0);
  CharacterIterator it = grammar.getIterator();
  for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
    switch (ch) {
    case 'F':
      x_temp = turtle.getX();
      y_temp = turtle.getY();
      turtle.setX(x_temp - drawLength * cos(turtle.getTheta()));
      turtle.setY(y_temp - drawLength * sin(turtle.getTheta()));      
      stroke(255);
      line(x_temp, y_temp, turtle.getX(), turtle.getY());
      break;
    case '+':
      turtle.setTheta(turtle.getTheta() + DELTA * repeat);
      repeat = 1;
      break;
    case '-':
      turtle.setTheta(turtle.getTheta() - DELTA * repeat);
      repeat = 1;
      break;
    case '[':
      ts.push(new Turtle(turtle)); 
      break;
    case ']':
      turtle = ts.pop();
      break;
    case 'X':   // confirm X&Y as valid grammar
    case 'Y':
      break;
    case '3':   // set repeat using char ascii code 
    case '5':   // 48 = ascii '0'
    case '8':
      repeat = (int)ch - 48;
      break;  
    default:
      System.err.println("character " + ch + " not in grammar");
      break;
    }
  }
}


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