class GeometricPolygon

Public Instance Methods

area() click to toggle source
# 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_box() click to toggle source
# 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) click to toggle source
# 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
edges() click to toggle source
# 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