Search code examples
javaawtpolygoncomputational-geometryconvex-hull

How to determine a point lies on a polygon?


I was solving a question which requires to find out if a point lies strictly inside a Polygon or not

Well I'm aware of java awt package so I could use this

polygon.contains(pointToCheck)

But the problem is according to the official documentation the definition of insideness is given as

A point is considered to lie inside a Shape if and only if:

  • it lies completely inside theShape boundary or
  • it lies exactly on the Shape boundary and the space immediately adjacent to the point in the increasing X direction is entirely inside the boundary or
  • it lies exactly on a horizontal boundary segment and the space immediately adjacent to the point in the increasing Y direction is inside the boundary

So how do I remove the count of points that lie on the Polygon?

If anyone could suggest a better algorithm to check if a point lies strictly inside the polygon it would also be helpful.


Solution

  • The Hao algorithm for point-in-polygon includes checking if the point lies on the boundary. The paper includes fairly understand psuedo-code if you want to write it yourself. I have written a javascript implementation here.

    In the java world another option might be to look at the Java Topological Suite. The PointLocator method probably does what you want.