Derivation of formula for heading to another point (lat/long) I know the formula for calculating the heading between to given points (latitude, longitude) is
$$ \tag{1}
\theta = \arctan2(\sin(\Delta\lambda)*\cos(\varphi_2), \cos(\varphi_1)*\sin(\varphi_2) − \sin(\varphi_1)*\cos(\varphi_2)*\cos(\Delta\lambda))
$$
where $\theta$ is the heading from the starting point $P_1(\varphi_1, \lambda_1)$ (latitude, longitude) to the target point $P_2(\varphi_2, \lambda_2)$ and the difference in longitude is $\Delta\lambda = \lambda_2 - \lambda_1$ unless the great circle between the points crosses longitude $\pi$ or $-\pi$, in which case you have to correct.
Equation 1 comes from the formula
$$\tag{2}
\tan(\theta) = \frac{\sin(\Delta\lambda)\cos(\varphi_2)}{\cos(\varphi_1)\sin(\varphi_2)-\sin(\varphi_1)\cos(\varphi_2)\cos(\Delta\lambda)}
$$
but I don't know where this one comes from. Can anyone provide a derivation as I want to understand how this formula came to be.
 A: Have a look at this sketch of a triangle on a sphere. B denotes point 1, i.e., $(\phi_1, \lambda_1)$ as well as the angle in the depicted triangle at that point. C denotes point 2, i.e., $(\phi_2, \lambda_2)$ as well as the depicted angle in the triangle at that point. A denotes the north pole as well as the angle in the triangle at that point. We want to find the angle B, which you denoted by $\theta$ in your question.
We can read off these sides and angles from the sketch:
\begin{align}
c&=(\pi/2 - \phi_1)  \\
b&=(\pi/2-\phi_2) \\
A &= (\lambda_2 - \lambda_1) =: \Delta \lambda
\end{align}
To start off, let's use one of the cosine rules of spherical trigonometry
\begin{align}
\cos a&= \cos b \cos c + \sin b \sin c \cos A \\
 &=\sin \phi_2 \sin \phi_1 + \cos \phi_2 \cos \phi_1 \cos (\Delta \lambda) 
\end{align}
Note that I used $\sin(\pi/2-x)=\cos(x)$ and $\cos(\pi/2-x)=\sin(x)$ for the last step.
Now that we have $\cos a$, let's solve the following cosine rule of spherical trigonometry for $\cos B$:
\begin{align}
\cos b&= \cos c \cos a + \sin c \sin a \cos B \\
\cos B &= \frac{\cos b -\cos c \cos a }{\sin c \sin a} 
\end{align}
The formula you are looking for is given in terms of $\tan B$. So we want to divide $\sin B$ by $\cos B$. For $\sin B$ we will use the sine rule of spherical trigonometry, which states that 
\begin{align}
\frac{\sin A}{\sin a}=\frac{\sin B}{\sin b}=\frac{\sin C}{\sin c}
\end{align}
Hence
\begin{align}
\sin B = \sin b \frac{\sin A }{\sin a}
\end{align}
Now we have a formula for $\cos B$ and a formula for $\sin B$. We can use that to find
\begin{align}
\frac{\sin B}{\cos B} &= \sin b \frac{\sin A }{\sin a} \frac{1}{\cos B} \\
\tan (B) &= \cos \phi_2 \frac{\sin(\Delta \lambda)}{\sin a}  \frac{\sin c \sin a} {\cos b -\cos c \cos a }\\
&= \cos \phi_2 \sin(\Delta \lambda)  \frac{\sin c} {\cos b -\cos c \cos a }\\
&= \cos \phi_2 \sin(\Delta \lambda)  \frac{\cos \phi_1} {\sin \phi_2 -\sin \phi_1  \cos a } \\
&= \cos \phi_2 \sin(\Delta \lambda)  \frac{\cos \phi_1} {\sin \phi_2 -\sin \phi_1(  \sin \phi_2 \sin \phi_1 + \cos \phi_2 \cos \phi_1 \cos (\Delta \lambda) ) } \\
&=   \frac{ \sin(\Delta \lambda)  \cos \phi_2 \cos \phi_1} {\sin \phi_2(1-\sin^2\phi_1) -\sin \phi_1 \cos \phi_2 \cos \phi_1 \cos (\Delta \lambda) ) }  \\
&=   \frac{\sin(\Delta \lambda)  \cos \phi_2 } {\sin \phi_2 \cos \phi_1 -\sin \phi_1 \cos \phi_2  \cos (\Delta \lambda) ) } 
\end{align}
