In: Computer Science
find the distance between two latitudes and longitudes while also dealing with unit conversions
We first derive the relevant formula, then give the algorithmic explanation for said formula and then lastly do a desk-check of the values.
The formula for the haversine of an angle is given as:
.
Now, let the central angle between the radius of the earth and the distance between any two given points be d. Then,
Where: r is the radius of the Earth (6371 km), d is the physical distance between the two points under consideration, is latitude of the two points and is longitude of the two points respectively.
Now, we take h = haversine(d/r). Then:
(we take haversine = hsine for brevity in the derivation below)
Then,
Which gives us:
Now, algorithmically (we assume that the internal sin() and cos() functions take radians in arguments):
Desk-check:
The actual distance between the points is:
Thus our final result is very close to the actual distance between these two points, with some error induced due to rounding.
(If you liked this answer, feel free to give it a thumbs up. Thank you so much!)