Class: VectorSalad::StandardShapes::Polygon
- Inherits:
-
BasicShape
- Object
- BasicShape
- VectorSalad::StandardShapes::Polygon
- Includes:
- Mixins::At
- Defined in:
- lib/vector_salad/standard_shapes/polygon.rb
Overview
Regular polygon shape.
Instance Attribute Summary collapse
-
#sides ⇒ Object
readonly
Returns the value of attribute sides.
Attributes inherited from BasicShape
Instance Method Summary collapse
-
#initialize(sides, radius, **options) ⇒ Polygon
constructor
A new instance of Polygon.
-
#to_path ⇒ Object
Convert the shape to a path.
Methods included from Mixins::At
Methods inherited from BasicShape
#flip, #flip_x, #flip_y, #jitter, #move, #rotate, #scale, #to_a, #to_bezier_path, #to_cubic_path, #to_multi_path, #to_simple_path, #to_svg
Constructor Details
#initialize(sides, radius, **options) ⇒ Polygon
17 18 19 20 21 22 |
# File 'lib/vector_salad/standard_shapes/polygon.rb', line 17 def initialize(sides, radius, **) @sides, @radius = sides, radius @options = @x, @y = 0, 0 self end |
Instance Attribute Details
#sides ⇒ Object (readonly)
Returns the value of attribute sides.
10 11 12 |
# File 'lib/vector_salad/standard_shapes/polygon.rb', line 10 def sides @sides end |
Instance Method Details
#to_path ⇒ Object
Convert the shape to a path
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/vector_salad/standard_shapes/polygon.rb', line 25 def to_path nodes = [] angle = 2 * Math::PI / @sides theta = angle / 2 + Math::PI / 2 @sides.times do |n| nodes[n] = [] nodes[n][0] = @radius * Math.cos(angle * n + theta) + @x nodes[n][1] = @radius * Math.sin(angle * n + theta) + @y end Path.new(*nodes, **@options) end |