Class: Sketch::Star

Inherits:
Element
  • Object
show all
Defined in:
lib/sketch/star.rb

Instance Method Summary collapse

Methods inherited from Element

central_point, #svg_node

Methods included from Base

#svg_attributes

Instance Method Details

#angle_between_pointsObject



20
21
22
# File 'lib/sketch/star.rb', line 20

def angle_between_points
  360 / number_of_points
end

#degrees_to_radians(degrees) ⇒ Object



16
17
18
# File 'lib/sketch/star.rb', line 16

def degrees_to_radians( degrees )
  degrees * Math::PI / 180
end

#draw(canvas) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sketch/star.rb', line 43

def draw(canvas)
  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

#drawing_order_indexesObject



24
25
26
27
28
# File 'lib/sketch/star.rb', line 24

def drawing_order_indexes
  self.number_of_points.times.map do |n|
    (n*2) % self.number_of_points
  end
end

#pointsObject



36
37
38
39
40
41
# File 'lib/sketch/star.rb', line 36

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

#points_in_drawing_orderObject



30
31
32
33
34
# File 'lib/sketch/star.rb', line 30

def points_in_drawing_order
  drawing_order_indexes.map do |index|
    points[index]
  end
end