Skip to content

Commit

Permalink
added Star element and example.
Browse files Browse the repository at this point in the history
  • Loading branch information
mustmodify committed Nov 11, 2011
1 parent bb9fe0b commit 12b0c77
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
7 changes: 7 additions & 0 deletions examples/star.rb
Original file line number Diff line number Diff line change
@@ -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

1 change: 1 addition & 0 deletions lib/sketch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ def svg_builder
require 'sketch/rect'
require 'sketch/rectangle'
require 'sketch/path'
require 'sketch/star'

51 changes: 42 additions & 9 deletions lib/sketch/star.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 12b0c77

Please sign in to comment.