Spherical law of cosines (source code)

= Spherical law of cosines
{title2=$\cos a=\cos b\cos c+\sin b\sin c\cos A$}
{wiki}

For a nondegenerate convex minor <spherical triangle>, the <spherical law of cosines> relates two angular sides $b,c$, their included interior <angle> $A$, and the opposite angular side $a$:
$$
\cos a=\cos b\cos c+\sin b\sin c\cos A.
$$
Relabelling the vertices gives the corresponding formulas for $b,B$ and $c,C$. As with the <spherical law of sines>, physical side <lengths> must first be divided by the <sphere>'s <radius>. The <spherical law of cosines> determines $a\in(0,\pi)$ uniquely from $b,c,A$ when the data describe such a <spherical triangle>. It also recovers each interior <angle> from three sides, for example
$$
\cos A=\frac{\cos a-\cos b\cos c}{\sin b\sin c}.
$$
Using the <cosine> and the range $0<A<\pi$ selects a unique <angle>, avoiding the supplementary-angle ambiguity of an <inverse sine> calculation.

To derive the <spherical law of cosines>, let $u,v,w$ be the vertex <unit vectors> at $A,B,C$, so $u\cdot v=\cos c$, $u\cdot w=\cos b$ and $v\cdot w=\cos a$. The outgoing unit <tangent vectors> at $u$ are $t_B=(v-\cos c\,u)/\sin c$ and $t_C=(w-\cos b\,u)/\sin b$. Their <dot product> is both $\cos A$ and $(\cos a-\cos b\cos c)/(\sin b\sin c)$, proving the identity. This gives a geometric explanation: <spherical trigonometry> compares <angles> in the <tangent plane> using <vectors> in the surrounding <Euclidean space>.

For a worked example on the <unit sphere>, take the vertices
$$
A=(0,0,1),\qquad B=(\sqrt3/2,0,1/2),\qquad C=(0,\sqrt3/2,1/2).
$$
At $A$, the outgoing unit <tangent vectors> are $(1,0,0)$ and $(0,1,0)$, so the interior <angle> is a <right angle>. The vertex <dot products> give $b=c=\pi/3=60^\circ$. The <spherical law of cosines> then reduces to the right-angle identity $\cos a=\cos b\cos c$, giving
$$
\cos a=\frac14,\qquad a=\arccos(1/4)\approx1.318116\text{ radians}=75.522488^\circ.
$$
To find $B$, use the relabelled <spherical law of cosines>:
$$
\cos B=\frac{\cos b-\cos a\cos c}{\sin a\sin c}=\frac{1/2-1/8}{(\sqrt{15}/4)(\sqrt3/2)}=\frac1{\sqrt5},\qquad B=C\approx63.434949^\circ.
$$
The physical side <lengths> on a <sphere> of <radius> $R$ are $\ell_a\approx1.318116R$ and $\ell_b=\ell_c=\pi R/3\approx1.047198R$. For example, on a toy <sphere> with $R=10$ metres they are approximately $13.18116$, $10.47198$ and $10.47198$ metres; the interior <angles> stay the same. Applying the planar <Pythagorean theorem> directly to the angular sides would instead give $\sqrt{60^2+60^2}\approx84.852814^\circ$. That flat <right triangle> calculation does not give the <great-circle distance> between $B$ and $C$.

\Image[/spherical-right-triangle-trigonometry.png]
{title=A non-octant spherical triangle with a right angle at $A$, angular sides $b=c=60^\circ$ and $a=75.522488^\circ$, and interior angles $B=C=63.434949^\circ$}

The familiar <law of cosines> reappears as a local approximation. With $a,b,c$ all small in <radians>, substitute the <small-angle approximations> $\sin x\approx x$ and $\cos x\approx1-x^2/2$ into the <spherical law of cosines> and keep terms through second order:
$$
1-\frac{a^2}{2}\approx1-\frac{b^2+c^2}{2}+bc\cos A,\qquad a^2\approx b^2+c^2-2bc\cos A.
$$
Multiplication by $R^2$ gives the planar <law of cosines> for the physical side <lengths>. For a fixed small surface <triangle> on a progressively larger <sphere>, the angular sides shrink and this approximation improves. Thus the curved and flat formulas fit together: the <Pythagorean theorem> is recovered locally when $A$ is a <right angle>.