How does the formula for azimuth to another point work If I want to know the azimuth (initial heading) to another point on a sphere I use the formula
$$\tag{1}
\tan(\theta) = \frac{\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 initial bearing to point 2, $\varphi_1$ and $\varphi_2$ are the latitudes of point one and two respectively and $\Delta\lambda$ is the difference in longitudes of the two points.
To get the azimuth I then use the $atan2$-function and insert
$$\tag{2}
\sin(\Delta\lambda)\cos(\varphi_2)
$$
for $X$ and
$$\tag{3}
\cos(\varphi_1)\sin(\varphi_2)-\sin(\varphi_1)\cos(\varphi_2)\cos(\Delta\lambda)
$$
for $Y$. I know that $\tan(\alpha) = \frac{X}{Y}$ is used to calculate the heading $\alpha$ of a vector. However, I can't figure out why equation 2 is my $X$ and equation 3 my $Y$. Where do these formulae come from?
 A: Let $p$, $p'$ be two unit vectors, directed from the Earth center to points $P$ and $P'$ on the sphere, and $n$ the analogous unit vector for the North Pole.
To compute the heading from $P$ to $P'$ you must set up a coordinate system in a plane perpendicular to $p$, with the $y$-axis pointing towards the North Pole. This can be simply done by constructing two unit vectors $x$ and $y$ as follows:
$$
y={p\times n\over|p\times n|}\times p,\quad x=y\times p.
$$
A vector $t$ in the same plane pointing towards $P'$ is:
$$
t=(p\times p')\times p
$$
and its coordinates are then its projections along vectors $x$ and $y$:
$$
t_x=t\cdot x,\quad t_y=t\cdot y.
$$
If you now express the coordinates of $p$, $p'$ as a function of their latitude and longitude (and of course $n=(0,0,1)$), you can find explicit expressions for $t_x$ and $t_y$ and should recover your formulas.
EDIT.
If $\phi$, $\phi'$ are the latitudes, and $\lambda$, $\lambda'$ the longitudes of points $P$ and $P'$, we have:
$$
p=(\cos\phi\cos\lambda,\cos\phi\sin\lambda,\sin\phi),\quad
p'=(\cos\phi'\cos\lambda',\cos\phi'\sin\lambda',\sin\phi'),\quad
n=(0,0,1).
$$
It follows that:
$$
p\times n=(\cos\phi\sin\lambda,-\cos\phi\cos\lambda,0),
\quad
|p\times n|=\cos\phi,
$$
whence
$$
y=(-\cos\lambda\sin\phi,-\sin\lambda\sin\phi,\cos\phi),
\quad
x=(-\sin\lambda,\cos\lambda,0).
$$
Moreover:
$$
t=(p\times p')\times p=-p(p'\cdot p)+p'=\\
\big(
\sin ^2\phi \cos \phi' \cos \lambda'
+\cos \phi^2\cos \phi' \sin \lambda \sin (\lambda-\lambda')
-\sin\phi\cos \phi  \sin \phi' \cos \lambda,\\
\sin ^2\phi\cos \phi' \sin \lambda'
-\cos^2 \phi\cos \phi' \cos \lambda \sin (\lambda-\lambda')
-\sin\phi \cos \phi \sin \phi' \sin \lambda,\\
\cos \phi (\cos
   \phi \sin \phi'-\sin \phi \cos \phi' \cos
   (\lambda-\lambda'))\big),
$$
and finally:
$$
t_x=-\cos\phi'\sin(\lambda-\lambda'),
\quad
t_y=-\cos\phi'\sin\phi\cos(\lambda-\lambda')+\cos\phi\sin\phi'.
$$
