### Distance between points on a sphere

If you try and find out how you compute the distance between points on a sphere, you’ll get a bunch of sites which offer to calculate it for you if you enter the coordinates. If you search harder, you can even get the formula. But no one seems to be offering a derivation. So here you go.

First, some preliminaries. The half angle formulas give us 2 sin² ½*ψ* = 1−cos *ψ*. (Easily derivable from cos 2*α* = cos² *α* − sin² *α*.) The straight-line distance (cutting through the inside of the sphere) between two points on a unit sphere with an angle *ψ* between them is 2 sin ½*ψ*. (Bisect the triangle formed by the two points and the center of the sphere.) Together, these mean that the square of the straight-line distance is 2 − 2 cos *ψ*.

Consider A and B to be our two points we want to get the distance between, with latitudes *φ*_{A} and *φ*_{B}, and longitudes which differ by *Θ*. We’ll operate mostly on the disk formed by the parallel through B. The point where it intersects the pole is D, and the projection of A onto it is C. Let EB be perpendicular to DC.

The radius of the parallel through A is *r*_{A} = cos *φ*_{A} = DC; the one through B is *r*_{B} = cos *φ*_{B} = DB. Angle CDB is *Θ*, so EB = *r*_{B} sin *Θ* and ED = *r*_{B} cos *Θ*.

EC = CD − ED = *r*_{A} − *r*_{B} cos *Θ*.

BC² = EC² − EB² = *r*_{A}² − 2 *r*_{A} *r*_{B} cos *Θ* + *r*_{B}² cos² *Θ* + *r*_{B}² sin² *Θ* = *r*_{A}² + *r*_{B}² − 2 *r*_{A} *r*_{B} cos *Θ*.

AC = |sin *φ*_{A} − sin *φ*_{B}|.

AB² = BC² + AC² = *r*_{A}² + *r*_{B}² − 2 *r*_{A} *r*_{B} cos *Θ* + sin² *φ*_{B} − 2 sin *φ*_{A} sin *φ*_{B}

= cos² *φ*_{A} + sin² *φ*_{A} + cos² *φ*_{B} + sin² *φ*_{A} + sin² *φ*_{B} − 2 *r*_{A} *r*_{B} cos *Θ* − 2 sin *φ*_{A} sin *φ*_{B}

= 2 − 2 *r*_{A} *r*_{B} cos *Θ* − 2 sin *φ*_{A} sin *φ*_{B}.

If *ψ* is the angle AOB (which is also the along-surface distance between A and B), then

AB² = 2 − 2 cos *ψ*, from the “preliminaries”.

So, 1 − *r*_{A} *r*_{B} cos *Θ* − sin *φ*_{A} sin *φ*_{B} = 1 − cos *ψ*.

*ψ* = arccos(cos *φ*_{A} cos *φ*_{B} cos *Θ* + sin *φ*_{A} sin *φ*_{B}).