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, 17 June 2010

Yet Another Sierpinski (in context free Scheme)

Get the Scheme context free library here.

   1 ;A Sierpinski on Ypsilon Scheme
   2 ;By Martin Prout
   3 ;
   4 
   5 (import (psilab cfdg core)
   6         (psilab cfdg rule))
   7 
   8 
   9 (define Y_TOP (/ 1.0 (sqrt 3)))
  10 (define Y_BOT (/ (sqrt 3) -6))
  11 
  12 (rule unit
  13    ( 1 (triangle
  14       (hue 0)
  15       (brightness 0.2)
  16       (saturation 0.2)
  17   )
  18   (unit 
  19     (scale 0.5)
  20     (brightness 0.2)
  21     (saturation 0.2)
  22     (x 0.5)
  23     (y Y_BOT)
  24     )
  25 
  26   (unit
  27     (scale 0.5)
  28     (brightness 0.2)
  29     (saturation 0.2)
  30     (x -0.5)
  31     (y Y_BOT)
  32   )
  33   
  34   (unit 
  35     (scale 0.5)
  36     (brightness 0.2)
  37     (saturation 0.2)
  38     (x 0)
  39     (y Y_TOP)
  40     )
  41   )
  42 )
  43 
  44 (init-cfdg)
  45 
  46 (background (brightness -1)) 
  47 
  48 (bounds -0.5 0.5 -0.4 0.6)
  49 
  50 (start-shape unit) 
 
 

A Window on Ypsilon Scheme (context free)

Get the Scheme context free library here.

   1 ;A Window on Ypsilon Scheme
   2 ;By Martin Prout
   3 ;
   4 
   5 (import (psilab cfdg core)
   6         (psilab cfdg rule))
   7 
   8 (define Y_TOP (/ 0.5 (sqrt 3)))
   9 (define Y_BOT (/ (sqrt 3) -24))
  10 
  11 (rule unit
  12    (1 (triangle (brightness 0.5) (hue 180) (saturation 0.5))
  13       (unit       
  14       (scale 0.5) 
  15       (y Y_TOP)
  16       (brightness 0.2)
  17       (saturation 0.1)
  18       )
  19       (unit       
  20       (scale 0.5) 
  21       (x -0.25)
  22       (y Y_BOT)
  23       (brightness 0.2)
  24       (saturation 0.1)
  25       )
  26       (unit       
  27       (scale 0.5) 
  28       (x 0.25)
  29       (y Y_BOT)
  30       (brightness 0.2)
  31       (saturation 0.1)
  32       )
  33    )
  34 )   
  35 
  36 (init-cfdg)
  37 
  38 (background (hue 60) (brightness 1) (saturation 1)) 
  39 
  40 (bounds -0.5 0.5 -0.4 0.6)
  41 
  42 (start-shape unit) 


Sunday, 13 June 2010

Levy Carpet Fractal (using ypsilon Scheme context free library)

Here is another set of context free rules in scheme, note I've created a highlighting mode for the scheme DSL for jEdit (but I'm not releasing it yet/if ever) get the Scheme context free library here.

   1 ;Levy Carpet fractal
   2 ;By Martin Prout
   3 ;
   4 
   5 (import (psilab cfdg core)
   6         (psilab cfdg rule))
   7 
   8 (define ISQRT2 (/ (sqrt 2)))
   9 
  10 (rule tapestry
  11   (1 (levy)
  12      (levy (flip 90))
  13   )
  14 )
  15 
  16 (rule levy
  17    (1 (square (alpha -0.8))
  18       (levy       
  19       (rotate 45) 
  20       (scale ISQRT2)
  21       (hue 0)
  22       (saturation 0.1)
  23       (brightness 0.1)
  24       (alpha 0.6)
  25       (x 0.5) 
  26       )
  27       (levy       
  28       (rotate -45) 
  29       (scale ISQRT2)
  30       (hue 0)
  31       (saturation 0.1)
  32       (brightness 0.1)
  33       (alpha 0.6)
  34       (x 0.5) 
  35       )
  36    )
  37 )   
  38 
  39 (init-cfdg)
  40 
  41 (background (brightness -1)) 
  42 
  43 (bounds -1.2 1.2 -1.2 1.2)
  44 
  45 (start-shape tapestry) 


Saturday, 12 June 2010

A Scheme Library For Context Free Art (I had to give it a go)

Ed Cavazos has produced a couple of Scheme implementations of cfdg here is the one I tried http://github.com/dharmatech/psilab/.

   1 ;Levy fractal
   2 ;By Martin Prout
   3 ;
   4 
   5 (import (psilab cfdg core)
   6         (psilab cfdg rule))
   7 
   8 (rule levy
   9   (1 (square (hue 0))
  10      (levy 
  11       (rotate 45) 
  12       (scale (/ 1 (sqrt 2)))
  13       (brightness 0.1)
  14       (saturation 0.1)
  15       (x 0.5)
  16      )
  17      (levy 
  18       (rotate -45) 
  19       (scale (/ 1 (sqrt 2)))
  20       (brightness 0.1)
  21       (saturation 0.1)
  22       (x 0.5)
  23      )      
  24    )
  25 )
  26 
  27 
  28 (init-cfdg)
  29 
  30 (background (brightness -1))
  31 
  32 (bounds -1.2 1.2 -1.2 1.2)
  33 
  34 (start-shape levy) 



I reckon the reason for the missing bits on the levy curve is that the probability of the recursive levy are shared with the square. So far my experiments to sort it out, have caused a syntax error. Update 14 June, Ed Cavazos let me know, via the cfdg discourse, that the problem is that first recursive levy dominates. Which is interesting as it highlights a difference between the Scheme expansion and the C++ implementation of the rules. Makes me wonder what happens with ruby processing? I should have remembered it worked OK see my ruby-processing blog.

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 }

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