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

Sunday, 20 March 2011

Another (1L) context sensitive sketch in (vanilla) processing

Here is another sketch that uses my LSystem Library (> version 0.7.0) to explore (1L) context sensitive rules (NB: the ignore character list may be initialized with a either a string or character array). Certain combination of replacement rules and ignore lists may still lead to "out of range errors", although I am not sure real world examples would be affected?

/**
* cs_test2.pde by Martin Prout
* Demonstrates a simple (1L) context sensitive grammar with ignored
* symbols. 
*/

import lsystem.turtle.*;
import lsystem.collection.*;
import lsystem.CSGrammar;

CSGrammar grammar;
float distance = 30;
float THETA = radians(30);

void setup() {
  size(500, 200);
  createGrammar();  
  strokeWeight(4);
  translate(width * 0.4, height * 0.7);
  iterateGrammar();
}

void createGrammar() {
  String axiom = "G[+F]F[-F][+F]F";  
  grammar = new CSGrammar(this, axiom); // initialize library
  grammar.addRule("G<F", "G");          // add cs replacement rule
  grammar.setIgnoreList("-+[]");
}

void render(String production) {   
  CharacterIterator it = grammar.getIterator(production);
  rotate(-PI/2);
  for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
    switch (ch) {
    case 'F': 
      stroke(255, 0, 0);    
      line(0,0, distance, 0);
      translate(distance, 0);
      break;
    case 'G': 
      stroke(0, 255, 0);     
      line(0,0, distance, 0);
      translate(distance, 0);
      break;  
    case '-':
      rotate(THETA);
      break;
    case '+':
      rotate(-THETA);
     break;
     case '[':
      pushMatrix();
      break;
    case ']':
      popMatrix();
     break;     
    default:
      System.err.println("character " + ch + " not in grammar");
    }
  }
}

void iterateGrammar(){
  for (int i = 0; i < 6; i++){  
  String production = grammar.createGrammar(i);
  translate(40, 0);
  pushMatrix();
  render(production);
  popMatrix();
  }
}




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