所以我从这里得到了这个算法。
http://www.ytechie.com/2009/08/determine-if-a-point-is-contained-within-a-polygon/
private List<Point> points;
@Override
public boolean contains(Point aPoint) {
int j = points.size() - 1;
boolean oddNodes = false;
for (int i = 0; i < points.size(); i++)
{
if (points.get(i).getY() < aPoint.getY() && points.get(j).getY() >= aPoint.getY() ||
points.get(j).getY() < aPoint.getY() && points.get(i).getY() >= aPoint.getY());
{
if (points.get(i).getX() +
(aPoint.getY() - points.get(i).getY())/(points.get(j).getY() - points.get(j).getY()) * (points.get(j).getX() - points.get(i).getX()) < aPoint.getX())
{
oddNodes = !oddNodes;
}
}
j = i;
}
return oddNodes;
}
算法不在java中,但是我转换了它,但它似乎没有用。它未能通过以下测试案例。
我的多边形由三个点组成。 (1,1),(99,99),(199,1),并且测试是(0,0)这应该返回false,因为它不在多边形中,但是尽管使用了该算法,它仍返回true。有什么想法错了吗?