Class: GeometricPolygon

Inherits:
Struct
  • Object
show all
Defined in:
lib/flash_math/modules/geometry/geometric_polygon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#verticesObject

Returns the value of attribute vertices

Returns:

  • (Object)

    the current value of vertices



1
2
3
# File 'lib/flash_math/modules/geometry/geometric_polygon.rb', line 1

def vertices
  @vertices
end

Instance Method Details

#areaObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/flash_math/modules/geometry/geometric_polygon.rb', line 10

def area
  sum = 0.0
  (0..vertices.length-1).each do |i|
    a = vertices[i-1]
    b = vertices[i]

    sum = sum + ((a.x * b.y) - (a.y * b.x))
  end
  (sum / 2).abs
end

#bounding_boxObject



3
4
5
6
7
8
# File 'lib/flash_math/modules/geometry/geometric_polygon.rb', line 3

def bounding_box
  leftbottom = GeometricPoint.new(vertices.map(&:x).min, vertices.map(&:y).min)
  righttop = GeometricPoint.new(vertices.map(&:x).max, vertices.map(&:y).max)

  GeometricBoundingBox.new(leftbottom, righttop)
end

#contains?(point) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/flash_math/modules/geometry/geometric_polygon.rb', line 21

def contains?(point)
  point_in_polygon = GeometricPointInPolygon.new(point, self)
  point_in_polygon.inside? || point_in_polygon.on_the_boundary?
end

#edgesObject



26
27
28
29
30
31
32
33
34
# File 'lib/flash_math/modules/geometry/geometric_polygon.rb', line 26

def edges
  edges = []

  1.upto(vertices.length - 1) do |vertex_index|
    edges << GeometricSegment.new(vertices[vertex_index - 1], vertices[vertex_index])
  end

  edges << GeometricSegment.new(vertices.last, vertices.first)
end