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

Tuesday 1 September 2009

Non cfdg DSL Sierpinski triangle using ruby processing

A genuine Sierpinski triangle using ruby-processing no DSL required:-


# Sierpinski.rb by Martin Prout
require 'ruby-processing'

T_HEIGHT = Math.sqrt(3)/2 # use ruby Math
TOP_Y = 1/Math.sqrt(3)
BOT_Y = Math.sqrt(3)/6
TRIANGLE_SIZE = 800

def setup()
  size(TRIANGLE_SIZE, (T_HEIGHT *  TRIANGLE_SIZE))
  smooth() 
  fill(255)
  background(0)
  no_stroke()
  draw_sierpinski(width/2, height/1.5, TRIANGLE_SIZE)
  save_frame("sierpinski.png")
end

def draw_sierpinski(cx, cy, sz)
  if (sz < 5)  then # Limit no of recursions on size
    draw_triangle(cx, cy, sz) # Only draw terminals
    no_loop()
  else
    cx0 = cx
    cy0 = cy - BOT_Y * sz
    cx1 = cx - sz/4
    cy1 = cy + (BOT_Y/2) * sz
    cx2 = cx + sz/4
    cy2 = cy + (BOT_Y/2) * sz
    draw_sierpinski(cx0, cy0, sz/2)
    draw_sierpinski(cx1, cy1, sz/2)
    draw_sierpinski(cx2, cy2, sz/2)
  end
end

def draw_triangle(cx, cy, sz)
  cx0 = cx
  cy0 = cy - TOP_Y * sz
  cx1 = cx - sz/2
  cy1 = cy + BOT_Y * sz
  cx2 = cx + sz/2
  cy2 = cy + BOT_Y * sz
  triangle(cx0, cy0, cx1, cy1, cx2, cy2)
end

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