Permalink
Browse files

fixing triangles to the unit circle, thanks to monkstone's new consta…

…nts, and blinging up Sierpinski with colors and a resolution slider
  • Loading branch information...
1 parent 16aed96 commit eda84af628d5c2b9ab2ec5ba10ab5ff3e8337c47 @jashkenas committed Aug 26, 2009
Showing with 18 additions and 22 deletions.
  1. +3 −3 context_free.rb
  2. +14 −19 samples/sierpinski.rb
  3. +1 −0 tests/triangle.rb
View
6 context_free.rb
@@ -11,8 +11,8 @@ class ContextFree
AVAILABLE_OPTIONS = [:x, :y, :rotation, :size, :flip, :color, :hue, :saturation, :brightness]
HSB_ORDER = {:hue => 0, :saturation => 1, :brightness => 2}
- Y_TRIANGLE_TOP = (4 - (5 * (Math.sqrt(3)))) / 8 # adjusted for processing coords
- Y_TRIANGLE_BOTTOM = (4 - Math.sqrt(3)) / 8 # adjusted for processing coords
+ TRIANGLE_TOP = -1 / Math.sqrt(3)
+ TRIANGLE_BOTTOM = Math.sqrt(3) / 6
# Define a context-free system. Use this method to create a ContextFree
# object. Call render() on it to make it draw.
@@ -216,7 +216,7 @@ 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.triangle(0, TRIANGLE_TOP * size, 0.5 * size, TRIANGLE_BOTTOM * size, -0.5 * size, TRIANGLE_BOTTOM * size)
@app.rotate(-rot) if rot
end
View
33 samples/sierpinski.rb
@@ -1,24 +1,24 @@
# TODO: Fix the triangle primitive to not have any points going out of the unit
# circle.
-load_library 'context_free'
+load_library 'context_free', 'control_panel'
-Y_TOP = (4 - (5 * (Math.sqrt(3))))/8
-Y_BOT = (4 - Math.sqrt(3))/8
+attr_accessor :resolution
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
+ triangle :size => 0.5, :rotation => PI
split do
- tri :size => 0.5, :y => -0.577, :x => 0
+ tri :size => 0.5, :y => -0.578, :x => 0,
+ :hue => 0.8, :saturation => 0.2, :brightness => 0.8
rewind
- tri :size => 0.5, :y => 0.285, :x => -0.51
+ tri :size => 0.5, :y => 0.289, :x => -0.5, :hue => 0.2,
+ :saturation => 0.2, :brightness => 0.8
rewind
- tri :size => 0.5, :y => 0.285, :x => 0.51
+ tri :size => 0.5, :y => 0.289, :x => 0.5, :hue => 0.2,
+ :saturation => 0.2, :brightness => 0.8
end
end
@@ -31,18 +31,13 @@ def setup
no_stroke
color_mode HSB, 1.0
smooth
- draw_it
+ @resolution = 5
+ control_panel do |p|
+ p.slider :resolution, (2..50), 5
+ end
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
+ @triangle.render :tri, :size => height/1.1, :color => [0, 0.5, 1], :stop_size => @resolution, :start_y => height/1.65
end
View
1 tests/triangle.rb
@@ -7,6 +7,7 @@ def setup
rule :tri do
circle
triangle :brightness => 0
+ circle :brightness => 10, :size => 0.02
end
end

0 comments on commit eda84af

Please sign in to comment.