Class: VectorSalad::StandardShapes::Polygon

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

Overview

Regular polygon shape.

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, #at=, #move

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, **options)
  @sides, @radius = sides, radius
  @options = options
  @x, @y = 0, 0
  self
end

Instance Attribute Details

#sidesObject (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_pathObject

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