Method: Geometry::Polygon#<=>
- Defined in:
- lib/geometry/polygon.rb
#<=>(point) ⇒ Number
Test a Geometry::Point for inclusion in the receiver using a simplified winding number algorithm
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/geometry/polygon.rb', line 70 def <=>(point) sum = edges.reduce(0) do |sum, e| direction = e.last.y <=> e.first.y # Ignore edges that don't cross the point's x coordinate next sum unless ((point.y <=> e.last.y) + (point.y <=> e.first.y)).abs <= 1 if 0 == direction # Special case horizontal edges return 0 if ((point.x <=> e.last.x) + (point.x <=> e.first.x)).abs <= 1 next sum # Doesn't intersect else is_left = e <=> point return 0 if 0 == is_left next sum unless is_left sum += 0 <=> (direction + is_left) end end (0 == sum) ? -1 : 1 end |