1

Given the coordinates of a polygon, I found many algorithms to detect if a point is in a polygon - for example: [1] and [2]. But none of these algorithms are able to detect if the point lies on the vertices of this polygon. For example I have a polygon:

|---------------------|
|                     |
|                     |
|---------------------|

My point is in the right upper corner. I want an algorithm that tells me whether the point is inside the polygon. How can I do this?

Community
  • 1
  • 1
Irgendw Pointer
  • 1,770
  • 3
  • 30
  • 67
  • 2
    How are your polygons defined? Presumably you don't have vertex coordinates; are you working from an image of the polygon? – beaker Jul 08 '13 at 14:54
  • No image. I have coordinates of the polygon and coordinates of my point. In the most cases this point is lying on the vertex of the polygon. And I have always simple polygons. – Irgendw Pointer Jul 09 '13 at 07:32
  • Check your terminology (vertex) and coherence of the question. "if the point lies on the vertices of this polygon" vs ""whether the point is inside the polygon vs "this point is lying on the vertex of the polygon". –  Sep 23 '15 at 14:51

2 Answers2

1

Except for problem of dealing with issues related to floating point not exact match but close enough, the same algorithm should work for both.
Just pick a point inside the polygon, create the test line segment from the test point to the inside point, and then, for each segment in the polygon, determine if a test line segment intersects that polygon segment, To count as intersect, count intersects open on one end of polygon segment, and closed on the other end, i.e., if the intersection is exactly the same as the start of the polygon, count it, but if it's exactly the same as the end point, do Not count it. You need to do this so that when the test segment intersects with a polygon vertex, you must only count it as intersecting one of the two segments on either side of the vertex.

Then If test point is inside polygon, the number of intersections will be an even number, if it is outside, it will be an odd number.

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
1

Just found a solution right here. It is very easy and the code from [1] has a and b already implemented. Additional information to the source code can be found here.

const float EPSILON = 0.001f;

bool IsPointOnLine(Point linePointA, Point linePointB, Point point) 
{
   float a = (linePointB.y - linePointA.y) / (linePointB.x - linePointB.x);
   float b = linePointA.y - a * linePointA.x;
   if ( fabs(point.y - (a*point.x+b)) < EPSILON)
   {
       return true;
   }

   return false;
}
Community
  • 1
  • 1
Irgendw Pointer
  • 1,770
  • 3
  • 30
  • 67
  • What is the relation with the question in the post ? –  Sep 23 '15 at 14:45
  • 1
    This code is rather poor as regards isotropy: it tests the vertical distance to the line, will return wrong results for nearly vertical lines and crash for verticals. –  Sep 23 '15 at 14:48