In my Java application I'm creating 2D polygons using an array of vertices. For example, I want to create a simple square using these 4 vertices
[-130, -74], [-125, -74], [-125, -70], [-130, -70]
Then I want to check if a point is inside the generated Polygon. But if I check, for example, this point
[-125, -73]
using polygon.contains(x, z)
it says is not inside the Polygon. Even if I check a corner, like [-125, -74]
is returns false. The strange part for me is that is I check this point [-126, -74]
is returns true, so some points are actually seen as inside the polygon, while others are not, and I can't understand why is it. This is a sample code I set up to test this, nothing special about it
public static void main(String[] args) {
Polygon polygon = new Polygon(new int[]{-130, -125, -125, -130}, new int[]{-74, -74, -70, -70}, 4);
System.out.println("" + polygon.contains(-125, -73));
System.out.println("" + polygon.contains(-125, -74));
System.out.println("" + polygon.contains(-126, -74));
}
And the output as well
false
false
true
I would also point out the fact that this is just a simple example, but the Polygon could be a really complex shape, for example something crazy like this