# Great arc distance between two points on a unit sphere

Suppose I have two points on a unit sphere whose spherical coordinates are $(\theta_1, \varphi_1)$ and $(\theta_2, \varphi_2)$. What is the great arc distance between these two points?

I found something from Wiki here but it is written in terms of latitude and longitude instead. I wonder latitude is $\pi/2 - \theta$ and longitude is $\varphi$. Am I right?

-
That is correct. – Robert Israel Nov 6 '12 at 6:34
@RobertIsrael So I can just plug these into the equation on Wiki and compute the great arc distance, right? – Patrick Li Nov 6 '12 at 6:36
latitude $\theta$ is measured from equator, not from the north pole as in your figure – Emanuele Paolini May 8 '13 at 13:44

Consider the two vectors to the points on the sphere, $${\bf v}_i=(r\sin\theta_i\cos\varphi_i,r\sin\theta_i\sin\varphi_i,r\cos\theta_i)$$ with $i=1,2$. Use the dot product to get the angle $\psi$ between them: $${\bf v}_1\cdot {\bf v}_2=r^2\left(\cos\theta_1\cos\theta_2+\sin\theta_1\sin\theta_2\cos\left(\varphi_1-\varphi_2\right)\right)=r^2\cos\psi.$$

Then the arclength is $$s=r\psi=r\cos^{-1}\left(\cos\theta_1\cos\theta_2+\sin\theta_1\sin\theta_2\cos\left(\varphi_1-\varphi_2\right)\right).$$

-
how exactly you got the dot product ? – curious Feb 28 '14 at 17:25
What you get is not the formula from here: en.wikipedia.org/wiki/Great-circle_distance#Formulas . I can't understand how the differences in cos are derived since you compute the inner product – curious Feb 28 '14 at 17:47
@curious the Wikipedia coordinates are defined differently than those in the question posted here. – Jonathan Feb 28 '14 at 19:47
Even if without the wikipedia definition i cant see how the formula of inner product between $v_1$ and $v_2$ gives the result you wrote – curious Mar 1 '14 at 3:36
@curious $\sin^2\alpha+\cos^2\alpha=1$ and $\cos\alpha\cos\beta+\sin\alpha\sin\beta=\cos(\alpha-\beta)$. – Jonathan Mar 1 '14 at 17:15

I compare two formulas in Matlab, in terms of precision and running time.

Consider the sphere, in Cartesian coordinates:

$x^2 + y^2 + z^2 = \rho^2$

or in spherical coordinates:

$x = \rho \sin(\theta) \cos(\varphi)\\ y = \rho \sin(\theta) \sin(\varphi)\\ z = \rho \cos(\theta)$

Choose a bunch of points on the sphere, for example:

N = 1e6 + 1;
rho_sph = rand;
phi_sph = rand(N, 1) * pi;
theta_sph = rand(N, 1) * pi;

xyz_sph = [rho_sph * sin(theta_sph) .* cos(phi_sph), ...
rho_sph * sin(theta_sph) .* sin(phi_sph), ...
rho_sph * cos(theta_sph)];


Now calculate the distance between each couple of points (1,2), (2,3), ..., (N-1, N).

1º formula

tic
aa = cross(xyz_sph(1:end-1, :), xyz_sph(2:end, :));
bb = dot(xyz_sph(1:end-1, :)', xyz_sph(2:end, :)');
aa = arrayfun(@(n) norm(aa(n, :)), 1:size(aa, 1));
d1 = rho_sph * atan2(aa, bb)';
toc
% Elapsed time is 12.376257 seconds.


2º formula

tic
d2 = rho_sph * acos(cos(theta_sph(1:end-1)) .* cos(theta_sph(2:end)) + ...
sin(theta_sph(1:end-1)) .* sin(theta_sph(2:end)) .* ...
cos(phi_sph(1:end-1) - phi_sph(2:end)));
toc
% Elapsed time is 0.264629 seconds.


The first formula is considered more precise, but it's slower; the second is less accurate at the poles, but it's much faster. Otherwise the two solutions are practically equivalent:

plot(d1 - d2, '.')

-