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

Monday, 7 June 2010

Towards an Escher Bird Tiling (in regular processing)

   1 /**
   2 * escher.pde
   3 */
   4 
   5 Bird[] birds = new Bird[64]; // Array of birds to tile
   6 
   7 void setup(){
   8   size(600, 600);
   9   translate(10, 10);
  10   smooth();
  11   
  12   // create a grid of Bird instances and draw them to screen  
  13   for (int i = 0; i < 8; i++){
  14     for (int j = 0; j < 8; j++){
  15       birds[i+j] = new Bird((i * 100) - 50 * (j%2), j * 85, 100);
  16       birds[i+j].drawBird((i+j)% 4);
  17     }
  18   }
  19 }
  20 
  21 /**
  22 * Bird.pde  [Bird class, knows how to draw 4 bird shape variations]
  23 */
  24 class Bird {
  25   PVector[] pts;
  26   float sz;
  27   float x0;
  28   float y0;
  29   float disp = 0.32;
  30   Bird(float x, float y, float sz) {
  31     x0 = x;
  32     y0 = y;
  33     this.sz = sz;
  34     pts = new PVector[16];
  35     calculatePoints();
  36   }
  37   void calculatePoints() {
  38     pts[0] = new PVector(x0 - 0.25 * sz, y0 - sz/sqrt(3));     // A
  39     pts[2] = new PVector(x0 + 0.75 * sz, y0 - sz/sqrt(3));     // B
  40     pts[4] = new PVector(x0 + 0.25 * sz, y0 + (sqrt(3)*sz)/6); // C
  41     pts[6] = new PVector(x0 - 0.75 * sz, y0 + (sqrt(3)*sz)/6); // D
  42     pts[1] = getMidPoint(pts[0], pts[2]); //Ab
  43     pts[3] = getMidPoint(pts[2], pts[4]); //Bc
  44     pts[5] = getMidPoint(pts[4], pts[6]); //Cd
  45     pts[7] = getMidPoint(pts[6], pts[0]); //Da
  46     pts[8] = getMidPoint(pts[0], pts[1]); //Aba
  47     adjustBezier(pts[8], HALF_PI, -disp * sz); //Aba
  48     pts[9] = getMidPoint(pts[1], pts[2]);
  49     adjustBezier(pts[9], HALF_PI, disp * sz);
  50     pts[10] = getMidPoint(pts[2], pts[3]);
  51     adjustBezier(pts[10], THIRD_PI, disp * sz);
  52     pts[11] = getMidPoint(pts[3], pts[4]);
  53     adjustBezier(pts[11], THIRD_PI, -disp * sz);
  54     pts[12] = getMidPoint(pts[4], pts[5]);
  55     adjustBezier(pts[12], HALF_PI, disp * sz);
  56     pts[13] = getMidPoint(pts[5], pts[6]);
  57     adjustBezier(pts[13], HALF_PI, -disp * sz);
  58     pts[14] = getMidPoint(pts[6], pts[7]);
  59     adjustBezier(pts[14], THIRD_PI, -disp * sz);
  60     pts[15] = getMidPoint(pts[7], pts[0]);
  61     adjustBezier(pts[15], THIRD_PI, disp * sz);
  62   } 
  63 
  64   void drawShape() {
  65     beginShape();
  66     vertex(pts[0].x, pts[0].y);
  67     bezierVertex(pts[0].x, pts[0].y, pts[8].x, pts[8].y, pts[1].x, pts[1].y);
  68     bezierVertex(pts[1].x, pts[1].y, pts[9].x, pts[9].y, pts[2].x, pts[2].y);
  69     bezierVertex(pts[2].x, pts[2].y, pts[10].x, pts[10].y, pts[3].x, pts[3].y);
  70     bezierVertex(pts[3].x, pts[3].y, pts[11].x, pts[11].y, pts[4].x, pts[4].y);
  71     bezierVertex(pts[4].x, pts[4].y, pts[12].x, pts[12].y, pts[5].x, pts[5].y);
  72     bezierVertex(pts[5].x, pts[5].y, pts[13].x, pts[13].y, pts[6].x, pts[6].y);
  73     bezierVertex(pts[6].x, pts[6].y, pts[14].x, pts[14].y, pts[7].x, pts[7].y);
  74     bezierVertex(pts[7].x, pts[7].y, pts[15].x, pts[15].y, pts[0].x, pts[0].y);
  75     vertex(pts[0].x, pts[0].y);
  76     endShape(CLOSE);
  77   }
  78 
  79   void drawBird(int type) {
  80     switch(type) {
  81     case 0:
  82       fill(255);
  83       stroke(0);
  84       drawShape();
  85       fill(0);
  86       ellipse(pts[6].x + 0.16  * sz, pts[6].y - 0.24 * sz, 0.04 * sz, 0.04 * sz);
  87       break;
  88     case 1:
  89       fill(100);
  90       stroke(0);
  91       drawShape();
  92       fill(255);
  93       ellipse(pts[2].x - 0.16 * sz, pts[2].y + 0.24 * sz, 0.04 * sz, 0.04 * sz);
  94       break;    
  95     case 2:    
  96       fill(155);
  97       drawShape();
  98       fill(0);
  99       noStroke();
 100       ellipse(pts[2].x - 0.16 * sz, pts[2].y + 0.24 * sz, 0.04 * sz, 0.04 * sz);
 101       break;
 102     case 3:
 103       fill(0);
 104       stroke(255);      
 105       drawShape();
 106       noStroke();
 107       fill(255);
 108       ellipse(pts[6].x + 0.16  * sz, pts[6].y - 0.24 * sz, 0.04 * sz, 0.04 * sz);
 109       break;
 110     }
 111   }
 112 
 113   void adjustBezier(PVector base, float theta, float disp) {
 114     base.add(new PVector(cos(theta)*disp, sin(theta)*disp));
 115   }
 116 
 117   PVector getMidPoint(PVector a,PVector b) {
 118     PVector mid = PVector.add(a, b);
 119     mid.div(2);
 120     return mid;
 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