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, 5 November 2009

Lévy curve implemented in processing

levy.pde
   1 Element turtle;
   2 
   3 int ITERATIONS = 18; 
   4 float elementSize = 0.5;
   5 
   6 void setup(){
   7   size(600, 400);
   8   background(0);
   9   turtle = new Element(width/3, height*0.77, 0);  
  10   levy(ITERATIONS);
  11 }
  12 
  13 class Element{
  14   float x, y, theta;
  15   Element(float x0, float y0, float angle){
  16     x =x0;
  17     y = y0;
  18     theta = angle;
  19   }
  20 
  21   void turnLeft(float angle){
  22     theta += angle;
  23   }
  24 
  25   void drawElement(float step){
  26     float x1 = x;
  27     float y1 = y;
  28     x += step * cos(theta);
  29     y += step * sin(theta);    
  30     vertex(x, y);
  31     vertex(x1, y1);
  32   }
  33 }
  34 
  35 
  36 public void levy(int n) {
  37   stroke(255, 0, 0);
  38   smooth();
  39   noFill();
  40   if (n == 0){
  41     beginShape();
  42     turtle.drawElement(elementSize);
  43     endShape();
  44   }
  45   else {
  46     turtle.turnLeft(-QUARTER_PI);
  47     levy(n-1);
  48     turtle.turnLeft(PI/2);
  49     levy(n-1);
  50     turtle.turnLeft(-QUARTER_PI);
  51   }
  52 }

 

2 comments:

  1. Looks great. CF excels at recursive stuff. The image looks like a sumo wrestler covered in tattoos to me!

    b e n

    ReplyDelete
  2. Yeah this processing sketch lacks the sharpness of the vanilla context free.

    ReplyDelete

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