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
    rule :unit do
      triangle :size => 1.0
      split do
        unit :size => 0.5, :x => 0, :y => Y_TOP, :brightness => 0.8
        unit :size => 0.5, :x => -0.5, :y => Y_BOT, :brightness => 0.8
        unit :size => 0.5, :x => 0.5, :y => Y_BOT, :brightness => 0.8
def setup
  size 600, 600
  color_mode RGB, 1

def draw
        # Do nothing.

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

def mouse_clicked

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


Blog Archive

About Me

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