11
2
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.
7Using that particular formula is an unobservable requirement. Isn't it enough to give the same result as that formula would give? – Adám – 2018-04-10T14:00:58.780
1May we take the input in radians? – Adám – 2018-04-10T14:02:51.657
Must we round, or may we return a result with decimals? – Adám – 2018-04-10T14:04:32.973
@Adám the inputs are in degree to avoid decimal inputs – mdahmoune – 2018-04-10T14:06:23.500
@Adám decimal with any precision or rounded integer – mdahmoune – 2018-04-10T14:09:04.297
@Adám yes of-course, you are allowed to use any formula that gives the same result. – mdahmoune – 2018-04-10T14:11:13.237
1@mdahmoune OK, so you listed in degrees for ease of writing, but may we require input to be in radians? Otherwise this challenge becomes a combo (which is bad) of angle conversion and of the main challenge. – Adám – 2018-04-10T14:11:37.680
@mdahmoune If any formula is allowed, you should state so. – Adám – 2018-04-10T14:11:54.563
ϕ1
latitude of point 1 in radians: since the only way the angles are used are as arguments tosin
, their unit doesn't matter. – Adám – 2018-04-10T14:14:55.4731@Adám you can use degrees or radian just specify in the answer whether the inputs are in degrees or radian. – mdahmoune – 2018-04-10T14:17:46.690
1I don't think this challenge will prompt varied or interesting answers. When the method of solving it is the same, you get a bunch of cookie-cutter solutions. – mbomb007 – 2018-04-10T14:34:24.977
@mbomb007 Who says the method must be the same... | {@}mdahmoune are you still going to keep that unobservable requirement? What if our language doesn't have sine and only cosine? Can we use cosine and shift the values instead? etc. – user202729 – 2018-04-10T15:05:04.273
1@user202729 it is mentioned in the question that you can use any formula that gives the same result as the haversine formula. – mdahmoune – 2018-04-10T15:24:11.190
5I've downvoted this question because it seems more to be 'Who's language can golf this formula the most', which, in my opinion, isn't particularly interesting. – caird coinheringaahing – 2018-04-10T15:27:51.483
@cairdcoinheringaahing Of course you can use another formula... – user202729 – 2018-04-10T15:38:32.303
2A shorter formula for most languages would be
d = r * acos( sin ϕ1 sin ϕ2 + cos ϕ1 cos ϕ2 cos(λ2 - λ1) )
wherer = 1737
– miles – 2018-04-10T17:35:51.123@miles thanks you can update the question by adding your formula if you want – mdahmoune – 2018-04-10T18:08:44.710
1@miles
r * acos(cos(ϕ1 - ϕ2) + cos ϕ1 cos ϕ2 (cos(λ2 - λ1) - 1))
is shorter still in some languages. – Neil – 2018-04-11T11:38:12.257I agree that Neil's and miles' formulas are mathematically equivalent, but the haversine formula was intended to give better precision in the cases the angles are close to each other. – jxh – 2018-04-11T18:51:52.910
Are we supposed to pretend that the Moon is spherical? It's less oblate than Earth, but still seems wrong to simplify that much... – Toby Speight – 2018-07-11T12:50:33.383
@TobySpeight yes, at least for this challenge – mdahmoune – 2018-07-11T13:37:12.770