' Búsqueda binaria para el punto más cercano en la línea geodésica
Public Sub PuntoMasCercano(latA As Double, lonA As Double, latB As Double, lonB As Double, latP As Double, lonP As Double) As Double()
Dim invAB As Map = VincentyInverse(latA, lonA, latB, lonB)
If invAB = Null Then Return Null
Dim distAB As Double = invAB.Get("distance")
Dim azimutAB As Double = invAB.Get("initialBearing")
Dim low As Double = 0
Dim high As Double = distAB
Dim tol As Double = 0.1
Dim bestPoint() As Double = Null
Dim minDist As Double = 1E9
Do While (high - low) > tol
Dim mid As Double = (low + high) / 2
Dim Q() As Double = VincentyDirect(latA, lonA, azimutAB, mid)
Dim invQ As Map = VincentyInverse(Q(0), Q(1), latP, lonP)
Dim distQ As Double = invQ.Get("distance")
If distQ < minDist Then
minDist = distQ
bestPoint = Q
End If
Dim Qleft() As Double = VincentyDirect(latA, lonA, azimutAB, (low + mid) / 2)
Dim invLeft As Map = VincentyInverse(Qleft(0), Qleft(1), latP, lonP)
Dim distLeft As Double = invLeft.Get("distance")
Dim Qright() As Double = VincentyDirect(latA, lonA, azimutAB, (mid + high) / 2)
Dim invRight As Map = VincentyInverse(Qright(0), Qright(1), latP, lonP)
Dim distRight As Double = invRight.Get("distance")
If distLeft < distRight Then
high = mid
Else
low = mid
End If
Loop
Return bestPoint
End Sub