class Pip::Polygon

Attributes

points[RW]

Public Class Methods

coord_at(y, p1, p2) click to toggle source
# File lib/pip/polygon.rb, line 30
def self.coord_at(y, p1, p2)
  x1, y1 = p1.map(&:to_f)
  x2, y2 = p2.map(&:to_f)
  (y - y1) / (y2 - y1) * (x2 - x1) + x1
end
new(points) click to toggle source
# File lib/pip/polygon.rb, line 7
def initialize(points)
  @points = points
end

Public Instance Methods

contains?(x,y) click to toggle source
# File lib/pip/polygon.rb, line 11
def contains?(x,y)
  (vertical_boundaries_on(y).map{|k| [k,nil]} << [x,x]).sort_by{|one, two| one}.uniq.find_index([x,x]).odd?
end
lines() click to toggle source
# File lib/pip/polygon.rb, line 24
def lines
  perms = []
  points.each_with_index {|n, i| perms << [n, points[i+1] || points[0]] }
  perms
end
vertical_boundaries_on(target_y) click to toggle source
# File lib/pip/polygon.rb, line 15
def vertical_boundaries_on(target_y)
  lines.map do |p1, p2| 
    x = Polygon.coord_at(target_y, p1, p2)
    comparable = [p1[1], p2[1]].sort
    next if comparable.uniq.size == 1
    x if target_y.between?(comparable[0], comparable[1])
  end.compact.sort
end