Geo Fencing - point inside/outside polygon

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

    C# code

    bool IsPointInPolygon(List 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;
        }
    }
    

提交回复
热议问题