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

Saturday, 29 May 2010

Towards A Penrose Tiling Using context-free rules

Here is my penrosepp.cfdg script (which needs pre-processing see context free art forum to see how to do it) that creates some building blocks towards a penrose tiling.

   1 #define PHI ((sqrt(5) + 1)/2)
   2 #define IPHI (2/(sqrt(5) + 1))
   3 
   4 startshape tiling
   5 
   6 rule tiling{
   7   block{r 36}
   8   5 *{r 72}
   9   {sunstar{s IPHI x (1 + PHI)}}
  10 }  
  11 
  12 rule block{
  13 sunstar{s IPHI}
  14 5 *{r 72}
  15 {starsun{x PHI}}
  16 }
  17 
  18 rule starsun{
  19   sun{z -0.8}
  20   star{s IPHI z -0.7
  21   }
  22 }
  23 
  24 rule sunstar{
  25   star{z -1}
  26   sun{z -.9}
  27 }
  28 
  29 rule sun{
  30   5*{r 72}kite{}  
  31 }
  32 
  33 rule star{
  34   5*{r 72}dart{}  
  35 }
  36 
  37 rule kite {
  38   sharp{}
  39   sharp{flip 72}
  40 }
  41 
  42 rule dart {
  43   flat{}
  44   flat{flip 36}
  45 }
  46 
  47 path sharp {
  48   LINETO {x 1}
  49   LINETO {y sin(36) x cos(36)}
  50   CLOSEPOLY {}
  51   FILL {brightness 0.9 sat 1 hue 0}
  52   STROKE {width 0.01}
  53 }
  54 
  55 path flat {
  56   LINETO {x (cos(36) * PHI) y (sin(36) * PHI)}
  57   LINETO {x 1}
  58   CLOSEPOLY {}
  59   FILL {brightness 0.8 sat 1 hue 220}
  60   STROKE {width 0.01}
  61 }


Saturday, 22 May 2010

A Fern Fractal Using my LSystem Library Utilities

   1 // Fern.pde
   2 import lsystem.*;
   3 
   4 float REDUCE1 = 0.85;
   5 float REDUCE2 = 0.33;
   6 float DELTA = PI/180 * 5.4;
   7 float startLength = 165;
   8 float drawLength;
   9 int startColor = 255<<24|0<<16|255<<8|0; // a light green stem?
  10 int decrement = 10 << 8; // decrement green by 10
  11 
  12 Grammar grammar;
  13 String production;
  14 PenStack ps;
  15 
  16 void setup() {
  17   size(800, 800);
  18   background(0);
  19   drawLength = startLength;
  20   smooth();
  21   ps = new PenStack();
  22   grammar = new SimpleGrammar(this, "FD");
  23   grammar.addRule('D', "C+@FD");
  24   grammar.addRule('C', "B");
  25   grammar.addRule('B', "[6+#FD][7-#FD]"); // abbreviated grammar
  26   production = grammar.createGrammar(22);
  27   background(0);
  28   translateRules();
  29 }
  30 
  31 void translateRules() {
  32   int repeat = 1;
  33   PenStack penStack = new PenStack(this);
  34   float xpos = 20;
  35   float ypos = 720;
  36   float theta = -PI/2.8;
  37   float x_temp, y_temp;
  38   Pen pen= new Pen(this, xpos, ypos, theta, drawLength, startColor);
  39   CharacterIterator it = grammar.getIterator(production);  
  40   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  41     switch (ch) {
  42     case 'F':          
  43       drawLine(pen);
  44       break;
  45     case '+':
  46       pen.setTheta(pen.getTheta() + DELTA * repeat);
  47       repeat = 1;
  48       break;
  49     case '-':
  50       pen.setTheta(pen.getTheta() - DELTA * repeat);
  51       repeat = 1;
  52       break;
  53     case '[':          
  54       penStack.push(pen.clone());
  55       break;
  56     case ']':
  57       pen = penStack.pop();
  58       break;
  59     case '@':
  60       pen.setColor(pen.getColor() - decrement);
  61       pen.setLength(pen.getLength()*REDUCE1);  // reduce 0.9 length
  62       break;
  63     case '#': 
  64       pen.setColor(pen.getColor() - decrement * 2);  
  65       pen.setLength(pen.getLength()*REDUCE2);  // reduce 0.33 length 
  66       break; 
  67     case '6':  
  68     case '7':
  69       repeat = (int)ch - 48;
  70       break;   
  71     case 'B': 
  72     case 'C': 
  73     case 'D': 
  74       break;
  75     default:
  76       System.err.println("character " + ch + " not in grammar");
  77     }
  78   }
  79 }
  80 
  81 void drawLine(Pen pen) {
  82   float x_temp = pen.getX(); 
  83   float y_temp = pen.getY();
  84   pen.setX(x_temp + pen.getLength() * cos(pen.getTheta()));
  85   pen.setY(y_temp + pen.getLength() * sin(pen.getTheta())); 
  86   stroke(pen.getColor());
  87   line(x_temp, y_temp, pen.getX(), pen.getY());
  88 }


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 }


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