Rotated coordinates on a unit sphere Given three points on a unit sphere, their coordinates are $p_1 = [\phi_1, \theta_1]$, $p_2 = [\phi_2, \theta_2]$, and $p_3 = [\phi_3, \theta_3]$, where $\phi$ and $\theta$ are azimuthal angle and polar angle, respectively.
Rotate about the sphere center so that $p_1$ locates at $[0, 0]$ and $p_2$ at $[0, l]$ (obviously, $l$ equals to the great-arc distance between $p_1$ and $p_2$).
On the rotated sphere, what is the new coordinates $[\phi, \theta]$ for $p_3$?
 A: Here is a brute force method to find the new coordinates of $p_3$.
Assuming that $\phi$ is the azimuthal angle measured counter clockwise from the positive $x$ axis, and $\theta$ is the polar angle measured from the positive $z$ axis, we can first convert the spherical coordinates into rectangular coordinates as follows:
$ p_1 = \begin{bmatrix} \sin \theta_1 \cos \phi_1 \\ \sin \theta_1 \sin \phi_1 \\ \cos \theta_1 \end{bmatrix}  \hspace{40pt} p_2 = \begin{bmatrix} \sin \theta_2 \cos \phi_2 \\ \sin \theta_2 \sin \phi_2 \\ \cos \theta_2 \end{bmatrix}  \hspace{40pt}p_3 = \begin{bmatrix} \sin \theta_3 \cos \phi_3 \\ \sin \theta_3 \sin \phi_3 \\ \cos \theta_3 \end{bmatrix}  $
In addition, let vectors $q_1 $ and $q_2$ be defined as follows:
$q_1 = \begin{bmatrix} 0\\ 0 \\ 1\end{bmatrix} \hspace{40pt} q_2 = \begin{bmatrix} \sin \psi \\ 0\\ \cos \psi \end{bmatrix} $
where the angle $\psi = \cos^{-1} \left(p_1 \cdot p_2\right) $
Now consider the rotation that takes $p_1$ and sends it to $q_1$ and takes $p_2$ and sends it to $q_2$, this can expressed as
$q_1 = R p_1,  \hspace{40pt} q_2 = R p_2 $
We need a third vector, so we'll take the cross product and write
$q_1 \times q_2 = R ( p_1 \times p_2 ) $
Hence, we now have the matrix equation,
$ \begin{bmatrix} q_1 , q_2 , q_1 \times q_2 \end{bmatrix} = R \begin{bmatrix} p_1 , p_2 , p_1 \times p_2 \end{bmatrix} $
which is of the form
$ Q = R P $
From which it follows that
$R = Q P^{-1} $
Finally, apply this rotation to $p_3$ to obtain $q_3$
$q_3 = R p_3 = \begin{bmatrix} \sin \theta \cos \phi \\ \sin \theta \sin \theta \\ \cos \theta \end{bmatrix} $
The new angles $\theta$ and $\phi$ can be found from the coordinates of $q_3$ as follows
$ \theta = \cos^{-1} q_{3z} $
$ \phi = \text{ATAN2} ( q_{3x} , q_{3y} ) $
