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)

Instance Method Summary (collapse)

Constructor Details

- (Polygon) initialize(sides, radius, **options)

Create a regular polygon with a specified number of sides.

Examples:

new(6, 100)

Parameters:

  • sides (PolySides)

    +Int; sides of the polygon, minimum of 3+

  • radius (Pos)
  • options ({})

    +{}+



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

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

Instance Attribute Details

- (Object) options Originally defined in class BasicShape

Returns the value of attribute options

- (Object) sides (readonly)

Returns the value of attribute sides



11
12
13
# File 'lib/vector_salad/standard_shapes/polygon.rb', line 11

def sides
  @sides
end

Instance Method Details

- (Any) [](x, y) Originally defined in module Mixins::At

Set the x, y coordinates of the shape.

Parameters:

  • x (Coord)

    +Num; a coordinate+

  • y (Coord)

    +Num; a coordinate+

Returns:

  • (Any)

- (Coords) at Originally defined in module Mixins::At

Get the x, y coordinates of the shape.

Returns:

  • (Coords)

    +[Num, Num] an x,y coordinate array+

- (Any) at=(at) Originally defined in module Mixins::At

Set the x, y coordinates of the shape directly.

Parameters:

  • at (Coords)

    +[Num, Num] an x,y coordinate array+

Returns:

  • (Any)

- (Any) move(x, y) Originally defined in module Mixins::At

Move the shape relatively.

Parameters:

  • x (Coord)

    +Num; a coordinate+

  • y (Coord)

    +Num; a coordinate+

Returns:

  • (Any)

- (Object) to_path

Convert the shape to a path



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vector_salad/standard_shapes/polygon.rb', line 26

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