0

I would like to calculate the distance between two geographical points (defined by latitude and longitude pair) using the solution described here.

Even though I understand the general concept the part with pre-converting all of your spherical (lat/long) coordinates into 3D unit-length cartesian coordinates first is problematic for me.

Could someone explain the algorithm that needs to we written to achieve the above? Java would be perfect, but pseudocode will do as well.

Update: I'm not interested in Haversine method.

Chanandler Bong
  • 403
  • 1
  • 11
  • 23
  • While not exactly a duplicate, I think you would be interested in this question and its answers: https://stackoverflow.com/questions/3694380/calculating-distance-between-two-points-using-latitude-longitude-what-am-i-doi – David Conrad Oct 26 '18 at 21:31

1 Answers1

2

Let's start with transformation to spherical coordinates. Observe that latitude/longitude is almost the same as angles in spherical coordinates. The only difference is that unlike phi latitude starts not from the North pole, but from the equator.

enter image description here

So, if you have latitude equal to +90 (90° N) corresponding phi angle is 0°, and when latitude equal to -90 (90° S) corresponding phi angle is 180°.

phi   = -latitude + 90°
theta =  longitude
rho   = 1

Now, you can go to Cartesian:

x=rho*sin(theta)*cos(phi); y = rho*sin(theta)*sin(phi); z = rho*cos(theta);

But in your case rho = 1, so

x = sin(theta)*cos(phi);
y = sin(theta)*sin(phi);
z = cos(theta);

where phi and theta are defined as above.

Yola
  • 18,496
  • 11
  • 65
  • 106