Lat/long Coördinates in selected area

Oct 28, 2009 at 11:33 AM

Hello,

Is there a way to determine if a lat/long is in a given area. I have some odd shaped (No rectangle, triangle, circle...) overlays and i

want to check when i receive a gps coördinate if that one is within the boundaries of that overlay.

 

Thanx.

Hein

Coordinator
Oct 28, 2009 at 12:51 PM

http://en.wikipedia.org/wiki/Point_in_polygon

http://msdn.microsoft.com/en-us/library/cc451895.aspx

Oct 28, 2009 at 1:31 PM

Here are 2 ways of doing it.

Cheers.

/// <summary> /// Determines if a Point is inside a polygon. /// </summary> /// <returns>Return True if the point is inside Polygon.</returns> public static bool IsInsidePolygon(PointF[] polygon, PointF point) { using (GraphicsPath gp = new GraphicsPath()) { gp.AddPolygon(polygon); return gp.IsVisible(point.X, point.Y); } }

/// <summary> /// Determines if a Point is inside a polygon. /// </summary> /// <returns>Return True if the point is inside the Polygon.</returns> public static bool InsidePolygon(Core.RX4Point[] polygon, Core.RX4Point point) { int polyPoints = polygon.GetUpperBound(0) + 1; int i = 0; int j = polyPoints - 1; while (i < polyPoints) { if ((((polygon[i].Y <= point.Y) && (point.Y < polygon[j].Y)) || ((polygon[j].Y <= point.Y) && (point.Y < polygon[i].Y))) && (point.X < (polygon[j].X - polygon[i].X) * (point.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) + polygon[i].X)) { return true; } j = i; i += 1; } return false; }

Oct 28, 2009 at 4:30 PM

Azlan,

 

Thanx for the quick reply...

I am not a C programmer. I will try to translate to VB.NET.....

 

Hein

Oct 28, 2009 at 5:15 PM
VB is not that much different for the C# code.

Cheers

''' <summary> ''' Determines if a Point is inside a polygon. ''' </summary> ''' <returns>Return True if the point is inside Polygon.</returns> Public Shared Function IsInsidePolygon(ByVal polygon() As PointF, ByVal point As PointF) As Boolean Using gp As GraphicsPath = New GraphicsPath() gp.AddPolygon(polygon) Return gp.IsVisible(point.X, point.Y) End Using End Function ''' <summary> ''' Determines if a Point is inside a polygon. ''' </summary> ''' <returns>Return True if the point is inside the Polygon.</returns> Public Shared Function InsidePolygon(ByVal polygon() As Core.RX4Point, ByVal point As Core.RX4Point) As Boolean Dim polyPoints As Integer = polygon.GetUpperBound(0) + 1 Dim i As Integer = 0 Dim j As Integer = polyPoints - 1 Do While i < polyPoints If (((polygon(i).Y <= point.Y) AndAlso (point.Y < polygon(j).Y)) OrElse ((polygon(j).Y <= point.Y) AndAlso (point.Y < polygon(i).Y))) AndAlso (point.X < (polygon(j).X - polygon(i).X) * (point.Y - polygon(i).Y) / (polygon(j).Y - polygon(i).Y) + polygon(i).X) Then Return True End If j = i i += 1 Loop Return False End Function