Created
January 30, 2018 14:41
-
-
Save JirkaChadima/8ba687a1e9259b0d2cc1c17c618a55dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
// java code | |
//... | |
/** | |
* https://stackoverflow.com/a/38675842 | |
*/ | |
protected boolean isInside(Point p, Polygon polygon) { | |
if (polygon == null) { | |
return false; | |
} | |
int intersections = 0; | |
Point prev = polygon.getPoints().get(polygon.getPoints().size() - 1); | |
for (Point next : polygon.getPoints()) { | |
if ((prev.getY() <= p.getY() && p.getY() < next.getY()) || (prev.getY() >= p.getY() && p.getY() > next.getY())) { | |
double dy = next.getY() - prev.getY(); | |
double dx = next.getX() - prev.getX(); | |
double x = (p.getY() - prev.getY()) / dy * dx + prev.getX(); | |
if (x > p.getX()) { | |
intersections++; | |
} | |
} | |
prev = next; | |
} | |
return intersections % 2 == 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment