Class: VectorSalad::StandardShapes::Polygon

Inherits:
BasicShape
  • Object
show all
Includes:
Mixins::At
Defined in:
lib/vector_salad/standard_shapes/polygon.rb

Direct Known Subclasses

Hexagon, Pentagon, Triangle

Instance Attribute Summary collapse

Attributes inherited from BasicShape

#options

Instance Method Summary collapse

Methods included from Mixins::At

#[], #at, #move

Constructor Details

#initialize(sides, radius, **options) ⇒ Polygon

Returns a new instance of Polygon.



16
17
18
19
20
21
# File 'lib/vector_salad/standard_shapes/polygon.rb', line 16

def initialize(sides, radius, **options)
  @sides, @radius = sides, radius
  @options = options
  @x, @y = 0, 0
  self
end

Instance Attribute Details

#sidesObject (readonly)

Returns the value of attribute sides.



9
10
11
# File 'lib/vector_salad/standard_shapes/polygon.rb', line 9

def sides
  @sides
end

Instance Method Details

#to_pathObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vector_salad/standard_shapes/polygon.rb', line 23

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