Point in Polygon

Topics: Windows Forms
Aug 9, 2010 at 7:38 AM
Hi Radioman! Is there a algo in Windows.Forms for a Point in Poligon-Match?
Coordinator
Aug 9, 2010 at 8:02 AM

hey, check http://greatmaps.codeplex.com/Thread/View.aspx?ThreadId=85419

Aug 9, 2010 at 11:33 AM

This will do the trick (.NET 3.5)

Extension Methods are so amazing =D

 

public static class PolygonHelper
    {
        public static bool IsPointInPolygon(this GMapPolygon polygon, PointLatLng PointToDetermine)
        {
            return IsPointInPolygon(polygon.Points, PointToDetermine);
        }


        public static bool IsPointInPolygon(this List<PointLatLng> polygon, PointLatLng PointToDetermine)
        {
            if (polygon.Count < 3)
                return false;

            List<PointLatLng> localPointList = new List<PointLatLng>();
            localPointList.AddRange(polygon);
            localPointList.Add(polygon[0]);
            int counter = 0;
            int i;
            double xinters;
            PointLatLng p1, p2;

            p1 = polygon[0];
            for (i = 1; i < localPointList.Count; i++)
            {
                p2 = localPointList[i % localPointList.Count];

                if (PointToDetermine.Lat > Math.Min(p1.Lat, p2.Lat))
                {
                    if (PointToDetermine.Lat <= Math.Max(p1.Lat, p2.Lat))
                    {
                        if (PointToDetermine.Lng <= Math.Max(p1.Lng, p2.Lng))
                        {
                            if (p1.Lat != p2.Lat)
                            {
                                xinters = (PointToDetermine.Lat - p1.Lat) * (p2.Lng - p1.Lng) / (p2.Lat - p1.Lat) + p1.Lng;
                                if (p1.Lng == p2.Lng || PointToDetermine.Lng <= xinters)
                                    counter++;
                            }
                        }
                    }
                }
                p1 = p2;
            }

            if (counter % 2 == 0)
                return false;

            return true;
        }
    }