Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Monday 24 August 2009

Sierpinski triangle ruby processing (context free DSL)

Making use of the newly implemented triangle terminal for ruby processing context free DSL (what a mouthful), I've had a go at drawing the Sierpinski triangle:-



# sierpinski.rb ruby-processing
load_library 'context_free'
Y_TOP = -1/Math.sqrt(3)
Y_BOT = Math.sqrt(3)/6


def setup_the_triangle
  @triangle = ContextFree.define do
    rule :start do
      unit :alpha => 0.5
    end
    rule :unit do
      triangle :size => 1.0
      split do
        unit :size => 0.5, :x => 0, :y => Y_TOP, :brightness => 0.8
        rewind
        unit :size => 0.5, :x => -0.5, :y => Y_BOT, :brightness => 0.8
        rewind
        unit :size => 0.5, :x => 0.5, :y => Y_BOT, :brightness => 0.8
      end
    end
  end
end
              
def setup
  size 600, 600
  setup_the_triangle
  no_stroke
  color_mode RGB, 1
  smooth
  draw_it
  save_frame("sierpinski.png")
end


def draw
        # Do nothing.
end


def draw_it
  background 225, 225, 0
  @triangle.render :start, :size => height, :stop_size => 3.0,
  :start_x => width/2, :start_y => height * 0.6
end


def mouse_clicked
  draw_it
end



I'm quite pleased at this attempt, though there are other ways of doing it.





The fun thing about context free is that even when it goes wrong, you often end up with something interesting like my 'y' here which was supposed to be the Sierpinski triangle, looks a bit like a window to me:



Somewhat inspired by the DSL version I have implemented a Sierspinski triangle using plain old java processing, and also repeated the exercise in ruby-processing.

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2