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.
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)
Labels:
context free,
ruby processing,
Sierpinski triangle
Subscribe to:
Post Comments (Atom)
Followers
Blog Archive
-
▼
2009
(50)
-
▼
August
(8)
- Adjusting your context free DSL variables
- Sierpinski triangle ruby processing (context free ...
- Understanding terminal shapes Context Free
- Triangle now included in Ruby Processing cfdg DSL
- Introducing weighted rules (and randomness)
- Rewind in Ruby Processing cfdg DSL
- Context Free Ruby Processing DSL
- Recursive call with probabalistic endpoint
-
▼
August
(8)
About Me
- monkstone
- I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2
No comments:
Post a Comment