Write a program that computes the spherical distance between two points on the surface of the Earth, given their latitudes and longitudes. This is a useful operation because it tells you how far apart two cities are if you multiply the distance by the radius of the Earth, which is roughly 6372.795 km.
Let φ1, λ1, and φ2, λ2 be the latitude and longitude of two points, respectively. Δ λ, the longitudinal difference, and Δσ, the angular difference/distance in radians, can be determined as follows from the spherical law of cosines:
Δσ = arcos(sin φ1 sin φ2 + cos φ1 cos φ2 cosΔλ)
For example, consider the latitude and longitude of two major cities:
• Nashville, TN: N 36°7.2′, W 86°40.2′
• Los Angeles, CA: N 33°56.4′, W 118°24.0′
You must convert these coordinates to radians before you can use them effectively in the formula. After conversion, the coordinates become
• Nashville: φ1 = 36.12° = 0.6304 rad, λ1 = −86.67° = −1.5127 rad
• Los Angeles: φ2 = 33.94° = 0.5924 rad, λ2 = −118.40° = −2.0665 rad
Using these values in the angular distance equation, you get
rΔσ = 6372.795 × 0.45306 = 2887.259 km
Thus, the distance between these cities is about 2887 km, or 1794 miles. (Note: To solve this problem, you will need to use the Math.acos method, which returns an arccosine angle in radians.)
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.