Sub GeoPointInPolygon(dPointLat As Double, dPointLon As Double, arrPolygonLatLon() As OSMDroid_GeoPoint) As Boolean
'http://www.mrexcel.com/forum/excel-questions/713005-does-point-fall-within-polygon-visual-basic-applications-function.html
'--------------------------------------------------------------------------------------------------------------------------
Dim i As Int
Dim j As Int
Dim PolySides As Int
Dim OddNodes As Boolean
PolySides = arrPolygonLatLon.Length - 1
j = PolySides
For i = 0 To PolySides
If (((arrPolygonLatLon(i).Longitude < dPointLon And arrPolygonLatLon(j).Longitude >= dPointLon) _
Or (arrPolygonLatLon(j).Longitude < dPointLon And arrPolygonLatLon(i).Longitude >= dPointLon)) _
And (arrPolygonLatLon(i).Latitude <= dPointLat Or arrPolygonLatLon(j).Latitude <= dPointLat)) Then
OddNodes = OddNodes <> (arrPolygonLatLon(i).Latitude + _
(dPointLon - arrPolygonLatLon(i).Longitude) / _
(arrPolygonLatLon(j).Longitude - arrPolygonLatLon(i).Longitude) * _
(arrPolygonLatLon(j).Latitude - arrPolygonLatLon(i).Latitude) < dPointLat)
End If
j = i
Next
Return OddNodes
End Sub
'------------------------------------------------------------------------
Sub WindingPointInPolygon(dLatitude As Double, dLongitude As Double, arrPolygonLatLon() As OSMDroid_GeoPoint) As Boolean
'http://geomalgorithms.com/a03-_inclusion.html
'2012 Dan Sunday
'--------------------------------------------
Dim i As Int
Dim wn As Int
Dim iUBound As Int
iUBound = arrPolygonLatLon.Length - 1
For i = 0 To iUBound - 1 '- 1 as we do i + 1
If arrPolygonLatLon(i).Longitude <= dLongitude Then
If arrPolygonLatLon(i + 1).Longitude > dLongitude Then
If IsLeft(arrPolygonLatLon(i), arrPolygonLatLon(i + 1), dLatitude, dLongitude) Then
wn = wn + 1
End If
End If
Else
If arrPolygonLatLon(i + 1).Longitude <= dLongitude Then
If IsLeft(arrPolygonLatLon(i), arrPolygonLatLon(i + 1), dLatitude, dLongitude) = False Then
wn = wn - 1
End If
End If
End If
Next
Return wn <> 0
End Sub
Sub IsLeft(P0 As OSMDroid_GeoPoint, P1 As OSMDroid_GeoPoint, dLatitude As Double, dLongitude As Double) As Boolean
Return (P1.Latitude - P0.Latitude) * (dLongitude - P0.Longitude) - _
(dLatitude - P0.Latitude) * (P1.Longitude - P0.Longitude) > 0
End Sub
'----------------------------------------------------------------------------------------------
Sub PtInPoly(dLatitude As Double, dLongitude As Double, arrPolygonLatLon() As OSMDroid_GeoPoint) As Boolean
Dim i As Int
Dim NumSidesCrossed As Int
Dim m As Double
Dim b As Double
Dim Boolean1 As Boolean
Dim Boolean2 As Boolean
Dim iUBound As Int
'Rick RothStein, http://www.excelfox.com/forum/showthread.php/1579-Test-Whether-A-Point-Is-In-A-Polygon-Or-Not
'-------------------------------------------------------------------------------------------------------------
iUBound = arrPolygonLatLon.Length - 1
'Xor in this case is true if expression1 and expression2 are: True False or False True
'-------------------------------------------------------------------------------------
For i = 0 To iUBound - 1
Boolean1 = arrPolygonLatLon(i).Latitude > dLatitude
Boolean2 = arrPolygonLatLon(i + 1).Latitude > dLatitude
If Boolean1 <> Boolean2 Then
m = (arrPolygonLatLon(i + 1).Longitude - arrPolygonLatLon(i).Longitude) / (arrPolygonLatLon(i + 1).Latitude - arrPolygonLatLon(i).Latitude)
b = (arrPolygonLatLon(i).Longitude * arrPolygonLatLon(i + 1).Latitude - arrPolygonLatLon(i).Latitude * arrPolygonLatLon(i + 1).Longitude) / (arrPolygonLatLon(i + 1).Latitude - arrPolygonLatLon(i).Latitude)
If m * dLatitude + b > dLongitude Then
NumSidesCrossed = NumSidesCrossed + 1
End If
End If
Next
Return (NumSidesCrossed = 1)
End Sub
'-------------------------------------------------------------------------------------------------
Sub CheckPointInPolygone(dPointLat As Double, dPointLon As Double, arrPolygonLatLon() As OSMDroid_GeoPoint) As Boolean
Dim x1 As Double
Dim y1 As Double
Dim x2 As Double
Dim y2 As Double
Dim D As Double
Dim i As Int
Dim ni As Int
Dim iPoints As Int
'Klaus
'-------------
ni = 0
x1 = arrPolygonLatLon(0).Latitude
y1 = arrPolygonLatLon(0).Longitude
iPoints = arrPolygonLatLon.Length
For i = 1 To iPoints
If i < iPoints Then
x2 = arrPolygonLatLon(i).Latitude
y2 = arrPolygonLatLon(i).Longitude
Else
x2 = arrPolygonLatLon(0).Latitude ' checks the last line
y2 = arrPolygonLatLon(0).Longitude
End If
If dPointLon >= Min(y1, y2) Then
If dPointLon <= Max(y1, y2) Then
If dPointLat <= Max(x1, x2) Then
If (dPointLat = x1 And dPointLon = y1) Or (dPointLat = x1 And dPointLat = x2) Then ' checks vertices and vertical lines
Return True
End If
If y1 <> y2 Then
D = (dPointLon - y1) * (x2 - x1) / (y2 - y1) + x1
If x1 = x2 Or dPointLat <= D Then
ni = ni + 1
End If
End If
End If
End If
End If
x1 = x2
y1 = y2
Next
If ni Mod 2 = 0 Then
Return False
Else
Return True
End If
End Sub