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

Turning Lozenges into Penrose Tiling

Here are some alternative rules to generate Penrose tilings from lozenges (they are taken from Fractint rules). These rules are supposed to make the coloring in easier, with X one color and Y another, but it didn't seem to fall out that easily for me. If you have been following the development of my LSystem library utilities, here I have introduced a singleton StringCharacterIterator instance in my grammar class that is re-used within the grammar class, and accessed here using the getIterator(String myString) function (see line 43).

   1 import lsystem.*;
   2 
   3 final float DELTA = (PI/5); // 36 degrees
   4 final float REDUCE = (sqrt(5) - 1)/2;
   5 int RED = color(200, 0, 0, 80);
   6 int YELLOW = color(200, 200, 0, 80);
   7 boolean tile = true;
   8 String production;
   9 float drawLength;
  10 float theta;  
  11 Grammar grammar;
  12 
  13 void setup() {
  14   size(600, 600);
  15   theta = 0;
  16   createLSystem();
  17   smooth();
  18   background(150, 0, 0);
  19   strokeWeight(3);
  20   translateRules();
  21   noLoop();
  22  }
  23 
  24 void createLSystem() {
  25   int generations = 8;  // set no of recursions
  26   String axiom = "bX";  
  27   grammar = new SimpleGrammar(this, axiom);  // initialize custom library
  28   grammar.addRule('X', "@+bF[|Y]2-F[|X][|+@GIX]3-[X]bF2-[Y]bF");
  29   grammar.addRule('Y', "@2+[X]rF|+rF[|Y]-[Y]rF|+rF[|X]");
  30   grammar.addRule('F', "G");
  31   float startLength = 1000;
  32   production = grammar.createGrammar(generations);
  33   drawLength = startLength;
  34 }
  35 
  36 void translateRules() {
  37   int repeat = 1;
  38   PenStack penStack = new PenStack(this);
  39   float xpos = -50;
  40   float ypos = height/2;
  41   float x_temp, y_temp;
  42   Pen pen= new Pen(this, xpos, ypos, theta, drawLength, YELLOW);
  43   CharacterIterator it = grammar.getIterator(production);  
  44   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  45     switch (ch) {
  46     case 'F':
  47       tile = true;
  48       stroke(pen.getColor());    
  49       drawLine(pen);
  50       break;
  51     case 'G': 
  52       pen.setColor(YELLOW);
  53       tile = false; 
  54       drawLine(pen);      
  55       break;
  56     case '+':
  57       pen.setTheta(pen.getTheta() + DELTA * repeat);
  58       repeat = 1;
  59       break;
  60     case '-':
  61       pen.setTheta(pen.getTheta() - DELTA * repeat);
  62       repeat = 1;
  63       break;
  64     case '[':      
  65       penStack.push(pen.clone());
  66       break;
  67     case ']':      
  68       pen = penStack.pop();
  69       break;
  70     case '|':
  71       pen.setTheta(pen.getTheta() + PI);  // turn 180 degrees  
  72       break;  
  73     case '@':
  74       pen.setLength(pen.getLength()*REDUCE);  // reduce length
  75       break;
  76     case 'I':     
  77       pen.setLength(pen.getLength()/REDUCE);  // restore length
  78       break; 
  79     case 'r':
  80       pen.setColor(YELLOW); 
  81       break; 
  82     case 'b':
  83       pen.setColor(RED); 
  84       break;
  85     case '2':
  86     case '3':
  87       repeat = (int)ch - 48;
  88       break;   
  89     case 'X': 
  90     case 'Y':      
  91       break;
  92     default:
  93       System.err.println("character " + ch + " not in grammar");
  94     }
  95   }
  96 }
  97 
  98 void drawLine(Pen pen) {
  99   float x_temp = pen.getX(); 
 100   float y_temp = pen.getY();
 101   pen.setX(x_temp + pen.getLength() * cos(pen.getTheta()));
 102   pen.setY(y_temp + pen.getLength() * sin(pen.getTheta())); 
 103   if (tile) {   // only draw line on F = forward    
 104     line(x_temp, y_temp, pen.getX(), pen.getY());
 105     tile = false;
 106   }
 107 }


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