I need to calculate distance between two points, using their coordinates (x, y, z) on a unit sphere.
Both Haversine and Great Arc Distance use φ and λ (longitude and latitude). How do i write those Formulas for x, y, z?
I need to calculate distance between two points, using their coordinates (x, y, z) on a unit sphere.
Both Haversine and Great Arc Distance use φ and λ (longitude and latitude). How do i write those Formulas for x, y, z?
Generalizing to a sphere of radius R
, so that the physical dimensions of expressions and variables is always apparent, we draw the following figure where you can see, on the plane that contains the three points, the points A and B on the surface of the sphere and the point O, the centre of the sphere, and also the arc of minimum length that connects A and B.
With reference to the figure and the text in it, you can compute the distance between points A and B like this:
Δ = math.sqrt((x_B-x_A)**2+(y_B-y_A)**2+(z_B-z_A)**2)
φ = math.asin((Δ/2/R))
gc_dist = 2*phi*R
.(you may want to use R = 1
).
At the time I am writing, the Wikipedia page for Great-circle distance is an excellent resource on this topic.
As well as the method explained by gboffi, which is a formulation of the same method from chord length as seen on Wikipedia but in terms of the Cartesian coordinates of the two points, there are a few other useful methods on the Wikipedia page.
I would draw your attention to the vector version, which is a natural fit for calculating the central angle (and thereby the great circle distance) between two points on the unit sphere represented in Cartesian coordinates. Assuming the points lie on a unit sphere centered at the origin, the points can be interpreted as normal vectors, and the central angle can be computed "using the dot product, cross product, or a combination."
If the sphere is not a unit sphere at the origin, you could simply subtract the coordinates of the center of the sphere from the two points to center them, and then normalize the two points so their magnitude is one.