Geo Fencing - point inside/outside polygon

后端 未结 16 1746
我在风中等你
我在风中等你 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:54

    Jan's answer is great.

    Here is the same code using the GeoCoordinate class instead.

    using System.Device.Location;
    

    ...

    public static bool IsPointInPolygon(List poly, GeoCoordinate point)
    {
        int i, j;
        bool c = false;
        for (i = 0, j = poly.Count - 1; i < poly.Count; j = i++)
        {
            if ((((poly[i].Latitude <= point.Latitude) && (point.Latitude < poly[j].Latitude))
                    || ((poly[j].Latitude <= point.Latitude) && (point.Latitude < poly[i].Latitude)))
                    && (point.Longitude < (poly[j].Longitude - poly[i].Longitude) * (point.Latitude - poly[i].Latitude)
                        / (poly[j].Latitude - poly[i].Latitude) + poly[i].Longitude))
                c = !c;
        }
    
        return c;
    }
    

提交回复
热议问题