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 September 2009

Koch flake, the hard way

   1 // hexaflake.pde
   2 float X1 = 0.5;
   3 float X2 = 0.25;
   4 float Y1 = sqrt(3)/4;
   5 float Y2 = 0;
   6 float REDUCE = 0.3333;
   7 void setup(){
   8   size(500, 500);
   9   smooth(); 
  10   background(0);
  11   fill(255);
  12   noStroke();
  13   drawHexaflake(width/2, height/2, height/1.5);
  14 }
  15 
  16 void drawHexaflake(float cx, float cy, float sz){
  17   if (sz < 5){
  18     noLoop();   
  19   }
  20   else{
  21      drawHexagon(cx, cy, sz);
  22     float x0 = cx - (X1 * sz);
  23     float y0 = cy;
  24     float x1 = cx - (X2 * sz);
  25     float y1 = cy  - (Y1 *sz);
  26     float x2 = cx + (X2 * sz);
  27     float y2 = cy  - (Y1 *sz);
  28     float x3 =  cx + (X1 * sz); 
  29     float y3 = cy;
  30     float x4 = cx + (X2 * sz);
  31     float y4 = cy + (Y1 * sz);
  32     float x5 = cx - (X2 * sz);
  33     float y5 = cy + (Y1 * sz);
  34     drawHexaflake(x0, y0, sz*REDUCE);
  35     drawHexaflake(x1, y1, sz*REDUCE);
  36     drawHexaflake(x2, y2, sz*REDUCE);
  37     drawHexaflake(x3, y3, sz*REDUCE);
  38     drawHexaflake(x4, y4, sz*REDUCE);
  39     drawHexaflake(x5, y5, sz*REDUCE);
  40   }
  41 }
  42 void drawHexagon(float cx, float cy, float sz){
  43   beginShape();
  44   vertex(cx - (X1 * sz), cy);
  45   vertex(cx - (X2 * sz), cy  - (Y1 *sz));
  46   vertex(cx + (X2 * sz), cy  - (Y1 *sz));
  47   vertex(cx + (X1 * sz), cy);
  48   vertex(cx + (X2 * sz), cy + (Y1 * sz));
  49   vertex(cx - (X2 * sz), cy + (Y1 * sz));
  50   endShape(CLOSE);
  51 }

Hexaflake based on hexagons (much more work than when implemented in context free when based on triangles)

However with a few changes to the code above it is easier to render the intermediate structure, using different colors/saturation/transparency (changing these with differing threshold sizes), best using HSB color mode.

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