Determine if point is in set of coordinates in java

风流意气都作罢 提交于 2021-02-19 12:46:21

问题


I have searched for an answer to this question, but I cannot seem to find a solution. What I need is I have a set of coordinates (of unknown length in the beginning) and I need to determine if a point resides inside of the polygon determined by this coordinate. However, I cannot use the AWT (as I originally thought I could), as the coordinates can range anywhere in the US.

I think I need to use a for-loop, with a series of less than, greater than checks. Am I on the right path, or is there an easier solution that I am not thinking of? Any help would be greatly appreciated!

EDIT: Here is a sample of the data I am working with:

49.006452278468664, 115.10363050431958
48.993314385809654, 115.04009921106841
49.03510754434096, 115.02009771650923
49.04825271759021, 115.08366905978859

回答1:


Eric Haines wrote a great article on this: http://erich.realtimerendering.com/ptinpoly/

There are a few methods at your disposal, the Jordan Curve Theorem is fairly good but has some issues if your data creates a doughnut type polygon and your point is in the middle.

Essentially, it says that a point is inside a polygon if, for any ray from this point, there is an odd number of crossings of the ray with the polygon's edges

You could also use the Angle Summation Test which is pretty slow but a surefire way to test if a point is inside a polygon.

sum the signed angles formed at the point by each edge's endpoints. If the sum is near zero, the point is outside; if not, it's inside (Figure 2)

A third method is Triangle Test which will work well if your polygon is not too pointy (which I suspect it might be):

The polygon is treated as a fan of triangles emanating from one vertex and the point is tested against each triangle by computing its barycentric coordinates.

If you can check that the shape is concave there are other methods at your disposal, I would recommend reading the article as it discusses many more ways to solve your problem than the ones listed here.




回答2:


Wikipedia writes:

One simple way of finding whether the point is inside or outside a simple polygon is to test how many times a ray, starting from the point and going in any fixed direction, intersects the edges of the polygon. If the point is on the outside of the polygon the ray will intersect its edge an even number of times. If the point is on the inside of the polygon then it will intersect the edge an odd number of times. Unfortunately, this method won't work if the point is on the edge of the polygon.

For simplicity, it's probably easiest to cast the ray horizontally, so edges crossing the ray can be identified by the sign changing:

boolean inside(Point p, Point[] polygon) {
    int intersections = 0;
    Point prev = polygon[polygon.length - 1];
    for (Point next : polygon) {
        if ((prev.y <= p.y && p.y < next.y) || (prev.y >= p.y && p.y > next.y)) {
            double dy = next.y - prev.y;
            double dx = next.x - prev.x;
            double x = (p.y - prev.y) / dy * dx + prev.x;
            if (x > p.x) {
                intersections++;
            }
        }
        prev = next;
    }
    return intersections % 2 == 1;
}


来源:https://stackoverflow.com/questions/38675611/determine-if-point-is-in-set-of-coordinates-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!