Find coordinates between 2 locations ($30)

Beja

Expert
Licensed User
Longtime User
Hi all,
Please see the attached image.
I want to know the latitude and longitude of the nearest point between 2 known points (X4, Y4. In other words, my vehicle (X1, Y1) is outside the route of the two points and wants to travel the shortest way to reach the nearest point. any formula or solution appreciated and will pay $30 for your successful efforts.
Note: Y is not necessarily parallel to equator

11xyz.png
 

Roycefer

Well-Known Member
Licensed User
Longtime User
This should work so long as the points are close enough for the curvature of the Earth not to be significant (less than 100 miles should do).
B4X:
'Returns x4,y4 as an Array of Doubles
Sub ClosestPointOnLine(x1 As Double, y1 As Double, x2 As Double, y2 As Double, x3 As Double, y3 As Double) As Double()
    Dim ax, ay, bx, by As Double
    ax = x1-x2   'a is the vector from point 2 to point 1
    ay = y1-y2
    bx = x3-x2   'b is the vector from point 2 to point 3
    by = y3-y2
    Dim aDOTb As Double = ax*bx + ay*by  'the dot product of a and b
    Dim bMag As Double = bx*bx + by*by    'the magnitude of b
    Return Array As Double(x2 + aDOTb*bx/bMag, y2 + aDOTb*by/bMag)
    'returns point 2 + (a DOT b)/bMag * b , namely, point 4
    'in geometry terms, this is the a vector projected onto the b vector and added to point 2
End Sub
 
Last edited:

derez

Expert
Licensed User
Longtime User
Hi all,
Please see the attached image.
I want to know the latitude and longitude of the nearest point between 2 known points (X4, Y4. In other words, my vehicle (X1, Y1) is outside the route of the two points and wants to travel the shortest way to reach the nearest point. any formula or solution appreciated and will pay $30 for your successful efforts.
Note: Y is not necessarily parallel to equator

View attachment 50246
IMG_20161121_102019.jpg


I don't need payment.

Note:
If you work with azimuth then the lines azimuth is 90 - alfa and the azimuth to go fro point 0 is 180-alfa
 

derez

Expert
Licensed User
Longtime User
Forgot to mention, to find the coordinates of the point, after you calculate alfa and d :
x = x0 + d * cos(90+alfa)
y = y0 + d * sin(90+alfa)
 
Top