Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

sierpinski in the house

  • Loading branch information...
commit 16aed9642fc6e4619dbabfccb42ea889f58d9515 1 parent 65c93b2
@jashkenas authored
Showing with 51 additions and 0 deletions.
  1. +3 −0  context_free.rb
  2. +48 −0 samples/sierpinski.rb
View
3  context_free.rb
@@ -213,8 +213,11 @@ def circle(some_options=nil)
end
def triangle(some_options=nil)
+ rot = some_options[:rotation]
+ @app.rotate(rot) if rot
size, options = *get_shape_values(some_options)
@app.triangle(0, Y_TRIANGLE_TOP * size, 0.5 * size, Y_TRIANGLE_BOTTOM * size, -0.5 * size, Y_TRIANGLE_BOTTOM * size)
+ @app.rotate(-rot) if rot
end
View
48 samples/sierpinski.rb
@@ -0,0 +1,48 @@
+# TODO: Fix the triangle primitive to not have any points going out of the unit
+# circle.
+
+load_library 'context_free'
+
+Y_TOP = (4 - (5 * (Math.sqrt(3))))/8
+Y_BOT = (4 - Math.sqrt(3))/8
+
+def setup_the_triangle
+
+ @triangle = ContextFree.define do
+
+ rule :tri do
+ triangle :size => 1.0, :set_brightness => 1
+ triangle :size => 0.51, :set_brightness => 0, :rotation => PI
+ split do
+ tri :size => 0.5, :y => -0.577, :x => 0
+ rewind
+ tri :size => 0.5, :y => 0.285, :x => -0.51
+ rewind
+ tri :size => 0.5, :y => 0.285, :x => 0.51
+ end
+ end
+
+ end
+end
+
+def setup
+ size 600, 600
+ setup_the_triangle
+ no_stroke
+ color_mode HSB, 1.0
+ smooth
+ draw_it
+end
+
+def draw
+ # Do nothing.
+end
+
+def draw_it
+ background 0.1
+ @triangle.render :tri, :size => height/1.2, :color => [0, 0, 0], :stop_size => 5, :start_y => height/1.65
+end
+
+def mouse_clicked
+ draw_it
+end
Please sign in to comment.
Something went wrong with that request. Please try again.