added Star element and example.
Johnathon E. Wright
committed Nov 11, 2011
commit 12b0c771a0108f8227f32fb14a19711f842deed8
Showing 3
changed files with
50 additions
and 9 deletions
examples/star.rb
+7
-0
| @@ | @@ -0,0 +1,7 @@ |
| + | class Star < Sketch |
| + | |
| + | def draw(canvas) |
| + | Sketch::Star.new(:radius => self.width.to_f / 2.1, :x => self.width.to_f / 2, :y => self.width.to_f / 2, :fill => 'yellow', :stroke => 'black', :stroke_width => 1, :orientation => 45).draw(canvas) |
| + | end |
| + | end |
| + | |
sketch.rb b/lib/sketch.rb
+1
-0
| @@ | @@ -48,4 +48,5 @@ require 'sketch/ellipse' |
| require 'sketch/rect' | |
| require 'sketch/rectangle' | |
| require 'sketch/path' | |
| + | require 'sketch/star' |
sketch/star.rb b/lib/sketch/star.rb
+42
-9
| @@ | @@ -5,17 +5,50 @@ class Sketch::Star < Sketch::Element |
| has_value :radius | |
| has_value :orientation, :default => 0 | |
| + | has_value :number_of_points, :default => 5 |
| + | |
| + | has_value :fill |
| + | has_value :stroke |
| + | has_value :stroke_width |
| + | |
| central_point :x, :y | |
| + | def degrees_to_radians( degrees ) |
| + | degrees * Math::PI / 180 |
| + | end |
| + | |
| + | def angle_between_points |
| + | 360 / number_of_points |
| + | end |
| + | |
| + | def drawing_order_indexes |
| + | self.number_of_points.times.map do |n| |
| + | (n*2) % self.number_of_points |
| + | end |
| + | end |
| + | |
| + | def points_in_drawing_order |
| + | drawing_order_indexes.map do |index| |
| + | points[index] |
| + | end |
| + | end |
| + | |
| + | def points |
| + | @points ||= list = number_of_points.times.map do |point_number| |
| + | current_angle = orientation + ( angle_between_points * point_number ) |
| + | [ x + (radius * Math.sin( degrees_to_radians( current_angle ))), y - (radius * Math.cos( degrees_to_radians( current_angle ))) ] |
| + | end |
| + | end |
| + | |
| def draw(canvas) | |
| - | top = [x, y - radius] |
| - | mid_right = [x + radius, y-(.3*radius)] |
| - | lower_right = [x + (0.6 * radius), y + radius] |
| - | lower_left = [x - (0.6 * radius), y + radius] |
| - | mid_left = [x - radius, y-(0.3*radius)] |
| - | points = [ top, lower_right, mid_left, mid_right, lower_left, top ] |
| - | points.each{|point| point.join(',')} |
| - | points = points.join(' ') |
| - | canvas.polygon( :points => points) |
| + | if x && y && radius |
| + | atts = {} |
| + | atts['points'] = points_in_drawing_order.each{|point| point.join(',')}.join(' ') |
| + | atts['fill'] = attributes[:fill] if attributes.has_key?(:fill) |
| + | atts['stroke'] = attributes[:stroke] if attributes.has_key?(:stroke) |
| + | atts['stroke-width'] = attributes[:stroke_width] if attributes.has_key?(:stroke_width) |
| + | |
| + | canvas.polygon( atts ) |
| + | end |
| end | |
| end | |