Given latitude/longitude of two points on the Moon (lat1, lon1)
and (lat2, lon2)
, compute the distance between the two points in kilometers, by using any formula that gives the same result as the haversine formula.
Input
- Four integer values
lat1, lon1, lat2, lon2
in degree (angle) or - four decimal values
ϕ1, λ1, ϕ2, λ2
in radians.
Output
Distance in kilometers between the two points (decimal with any precision or rounded integer).
Haversine formula
where
r
is the radius of the sphere (assume that the Moon's radius is 1737 km),ϕ1
latitude of point 1 in radiansϕ2
latitude of point 2 in radiansλ1
longitude of point 1 in radiansλ2
longitude of point 2 in radiansd
is the circular distance between the two points
(source: https://en.wikipedia.org/wiki/Haversine_formula)
Other possible formulas
d = r * acos(sin ϕ1 sin ϕ2 + cos ϕ1 cos ϕ2 cos(λ2 - λ1))
@miles' formula.d = r * acos(cos(ϕ1 - ϕ2) + cos ϕ1 cos ϕ2 (cos(λ2 - λ1) - 1))
@Neil's formula.
Example where inputs are degrees and output as rounded integer
42, 9, 50, 2 --> 284
50, 2, 42, 9 --> 284
4, -2, -2, 1 --> 203
77, 8, 77, 8 --> 0
10, 2, 88, 9 --> 2365
Rules
- The input and output can be given in any convenient format.
- Specify in the answer whether the inputs are in degrees or radians.
- No need to handle invalid latitude/longitude values
- Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
- If possible, please include a link to an online testing environment so other people can try out your code!
- Standard loopholes are forbidden.
- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
d = r * acos( sin ϕ1 sin ϕ2 + cos ϕ1 cos ϕ2 cos(λ2 - λ1) )
wherer = 1737
\$\endgroup\$