# 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
• @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'm a bit confused, are \varphi_1 and \varphi_2 angles? If I use 0 for both \theta_1 and \theta_2, I get a 0 degree difference no matter what \varphi_1 and \varphi_2 are. That doesn't seem right to me. – B T Mar 8 '16 at 2:42

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, '.')

• Does the expense of the first formula decrease if you store xyz_sph(2:end, :) as an intermediate value instead of computing it twice? – Brandon Rhodes Jun 16 '18 at 17:59

well, i see Wikipedia says there is serious rounding error in the formula of top answer. i found on google a new formula with the derivation. It seems slightly complicated but it is correct one. you can calculate values of distance & compare with percentage errors in both the formula.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 1, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.8171511888504028, "perplexity": 1511.4544371954053}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-34/segments/1596439739104.67/warc/CC-MAIN-20200813220643-20200814010643-00298.warc.gz"}