First find the azimuth $\theta$ and elevation $\phi$ and radius $r$ of the final point $(x,y,z)$. Then rotate the the initial point in the x,y plane by the $\theta$. Then rotate that point around the axis perpendicular to the line at the angle of $\theta$. Let this second axis of rotation be defined by the unit vector $u = (u_x, u_y, u_z)$.
First rotation about z axis
$R_z = \left(\begin{array}{ccc} \cos\left(\mathrm{\theta}\right) & -\sin\left(\mathrm{\theta}\right) & 0\\ \sin\left(\mathrm{\theta}\right) & \cos\left(\mathrm{\theta}\right) & 0\\ 0 & 0 & 1 \end{array}\right)$
Second rotation about u axis. In this case the axis of rotation is defined by: $u_x = -\sin(\theta)$, $u_y = \cos(\theta)$, $u_z = 0$.
$R_u=\left(\begin{array}{ccc} \cos\left(\mathrm{\phi}\right)-{u_{x}}^2\,\left(\cos\left(\mathrm{\phi}\right)-1\right) & -u_{z}\,\sin\left(\mathrm{\phi}\right)-u_{x}\,u_{y}\,\left(\cos\left(\mathrm{\phi}\right)-1\right) & u_{y}\,\sin\left(\mathrm{\phi}\right)-u_{x}\,u_{z}\,\left(\cos\left(\mathrm{\phi}\right)-1\right)\\ u_{z}\,\sin\left(\mathrm{\phi}\right)-u_{x}\,u_{y}\,\left(\cos\left(\mathrm{\phi}\right)-1\right) & \cos\left(\mathrm{\phi}\right)-{u_{y}}^2\,\left(\cos\left(\mathrm{\phi}\right)-1\right) & -u_{x}\,\sin\left(\mathrm{\phi}\right)-u_{y}\,u_{z}\,\left(\cos\left(\mathrm{\phi}\right)-1\right)\\ -u_{y}\,\sin\left(\mathrm{\phi}\right)-u_{x}\,u_{z}\,\left(\cos\left(\mathrm{\phi}\right)-1\right) & u_{x}\,\sin\left(\mathrm{\phi}\right)-u_{y}\,u_{z}\,\left(\cos\left(\mathrm{\phi}\right)-1\right) & \cos\left(\mathrm{\phi}\right)-{u_{z}}^2\,\left(\cos\left(\mathrm{\phi}\right)-1\right) \end{array}\right)$
Then to map the vector $(0,0,1)$ to the point $(x,y,z)$ I need to do the following:
${\begin{pmatrix}
x\\
y\\
z
\end{pmatrix}} = R_u R_z {\begin{pmatrix}
0\\
0\\
1
\end{pmatrix}}$
Because the matrices only do rotations, this formula will only work if the magnitude of the vector $(x,y,z)$ is 1
Is there a way to take advantage from the fact that the initial vector is perfectly in the z direction?
– Matthew James Oct 30 '20 at 07:51