Geo Fencing - point inside/outside polygon

后端 未结 16 1775
我在风中等你
我在风中等你 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条回答
  •  猫巷女王i
    2020-11-28 03:02

    The complete solution in asp.Net C#, you can see the complete detail here, you can see how to find point(lat,lon) whether its inside or Outside the Polygon using the latitude and longitudes ? Article Reference Link

    private static bool checkPointExistsInGeofencePolygon(string latlnglist, string lat, string lng) {

        List objList = new List();
        // sample string should be like this strlatlng = "39.11495,-76.873259|39.114588,-76.872808|39.112921,-76.870373|";
        string[] arr = latlnglist.Split('|');
        for (int i = 0; i <= arr.Length - 1; i++)
        {
            string latlng = arr[i];
            string[] arrlatlng = latlng.Split(',');
    
            Loc er = new Loc(Convert.ToDouble(arrlatlng[0]), Convert.ToDouble(arrlatlng[1]));
            objList.Add(er);
        }
        Loc pt = new Loc(Convert.ToDouble(lat), Convert.ToDouble(lng));
    
        if (IsPointInPolygon(objList, pt) == true)
        {
              return true;
        }
        else
        {
               return false;
        }
    }
    private static 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;
    }
    

提交回复
热议问题