Geo Fencing - point inside/outside polygon

后端 未结 16 1654
我在风中等你
我在风中等你 2020-11-28 02:15

I would like to determine a polygon and implement an algorithm which would check if a point is inside or outside the polygon.

Does anyone know if there is any exampl

相关标签:
16条回答
  • 2020-11-28 02:42

    Check if a point is inside a polygon or not -

    Consider the polygon which has vertices a1,a2,a3,a4,a5. The following set of steps should help in ascertaining whether point P lies inside the polygon or outside.

    Compute the vector area of the triangle formed by edge a1->a2 and the vectors connecting a2 to P and P to a1. Similarly, compute the vector area of the each of the possible triangles having one side as the side of the polygon and the other two connecting P to that side.

    For a point to be inside a polygon, each of the triangles need to have positive area. Even if one of the triangles have a negative area then the point P stands out of the polygon.

    In order to compute the area of a triangle given vectors representing its 3 edges, refer to http://www.jtaylor1142001.net/calcjat/Solutions/VCrossProduct/VCPATriangle.htm

    0 讨论(0)
  • 2020-11-28 02:43

    If you have a simple polygon (none of the lines cross) and you don't have holes you can also triangulate the polygon, which you are probably going to do anyway in a GIS app to draw a TIN, then test for points in each triangle. If you have a small number of edges to the polygon but a large number of points this is fast.

    For an interesting point in triangle see link text

    Otherwise definately use the winding rule rather than edge crossing, edge crossing has real problems with points on edges, which if your data is generated form a GPS with limited precision is very likely.

    0 讨论(0)
  • 2020-11-28 02:44

    The problem is easier if your polygon is convex. If so, you can do a simple test for each line to see if the point is on the inside or outside of that line (extending to infinity in both directions). Otherwise, for concave polygons, draw an imaginary ray from your point out to infinity (in any direction). Count how many times it crosses a boundary line. Odd means the point is inside, even means the point is outside.

    This last algorithm is trickier than it looks. You will have to be very careful about what happens when your imaginary ray exactly hits one of the polygon's vertices.

    If your imaginary ray goes in the -x direction, you can choose only to count lines that include at least one point whose y coordinate is strictly less than the y coordinate of your point. This is how you get most of the weird edge cases to work correctly.

    0 讨论(0)
  • 2020-11-28 02:47

    I add one detail to help people who live in the... south of earth!! If you're in Brazil (that's my case), our GPS coord are all negatives. And all these algo give wrong results.

    The easiest way if to use the absolute values of the Lat and Long of all point. And in that case Jan Kobersky's algo is perfect.

    0 讨论(0)
  • 2020-11-28 02:51

    If i remember correctly, the algorithm is to draw a horizontal line through your test point. Count how many lines of of the polygon you intersect to reach your point.

    If the answer is odd, you're inside. If the answer is even, you're outside.

    Edit: Yeah, what he said (Wikipedia):

    alt text

    0 讨论(0)
  • 2020-11-28 02:51

    C# code

    bool IsPointInPolygon(List<Loc> poly, Loc point)
    {
        int i, j;
        bool c = false;
        for (i = 0, j = poly.Count - 1; i < poly.Count; j = i++)
        {
            if ((((poly[i].Lt <= point.Lt) && (point.Lt < poly[j].Lt)) 
                    || ((poly[j].Lt <= point.Lt) && (point.Lt < poly[i].Lt))) 
                    && (point.Lg < (poly[j].Lg - poly[i].Lg) * (point.Lt - poly[i].Lt) 
                        / (poly[j].Lt - poly[i].Lt) + poly[i].Lg))
            {
    
                c = !c;
            }
        }
    
        return c;
    }
    

    Location class

    public class Loc
    {
        private double lt;
        private double lg;
    
        public double Lg
        {
            get { return lg; }
            set { lg = value; }
        }
    
        public double Lt
        {
            get { return lt; }
            set { lt = value; }
        }
    
        public Loc(double lt, double lg)
        {
            this.lt = lt;
            this.lg = lg;
        }
    }
    
    0 讨论(0)
提交回复
热议问题