# Find two angles between two vectors with direction?

So using the image above I have two vectors, u and v. Vector u is formed using P1 and P2 which are two coordinates in 3D space. Vector v is formed using P1 and P3.

I wish to first rotate vector v around the Z axis until it lines up under vector u which is shown by w in the image. After that then rotate around the x axis, so it is in line with vector u. I wish to be able to calculate those two angles. Hopefully this makes sense.

The goal is to find angle a and b

The other trick is I wish to keep track of direction as well, meaning that the given two angles can be positive or negative.

I do know how to find the angle between two vectors but am unsure how to do so like above.

• There is too little data provided to calculate anything precisely. You need values of some sort: distances, coordinates, angles, ... otherwise there is nothing you can say. – Arnaud Mortier May 1 '18 at 17:49
• @ArnaudMortier Well you would know two points for each vector, to which one (the starting point) is the same for both. – Eric F May 1 '18 at 17:59
• I don't know what this means, "you know two points". If you mean that you know coordinates, then you should include them in the question. – Arnaud Mortier May 1 '18 at 18:02
• @ArnaudMortier There you are. I have updated my question – Eric F May 1 '18 at 18:17
• Your problem is not well formulated, meaning it can lead to no solution. For example, let's have $P_1$ at origin, and $P_2$ and $P_3$ in the $x-z$ plane. Then $a=0$, but no rotation around $x$ axis will rotate one vector on top of the other. You are probably looking at a rotation around the axis perpendicular to the plane$uw$ – Andrei May 1 '18 at 18:43

A counterclockwise (wrt order $(x, y)$) rotation of angle $\theta$ about the $z$-axis is given by the matrix $$M_{\theta,z}=\left(\matrix{\cos \theta& -\sin \theta&0\\\sin \theta& \cos \theta&0\\0&0&1\\}\right)$$

A counterclockwise (wrt order $(y, z)$) rotation of angle $\varphi$ about the $x$-axis is given by the matrix $$M_{\varphi,x}=\left(\matrix{1&0&0\\0&\cos \varphi& -\sin \varphi\\0&\sin \varphi& \cos \varphi\\}\right)$$

You have to find $\theta$ and $\varphi$ such that $M_{\varphi,x}\cdot M_{\theta,z}\cdot v=u$. If you know the coordinates of $u$ and $v$ this will give you a system of equations to solve.

• How would I work backwards to solve for the two angles based on your equations? – Eric F May 1 '18 at 18:39
• Just write them down, they're not difficult. – Arnaud Mortier May 1 '18 at 18:46
• I think I have it down then. I have plugged everything in and worked it out to having 3 equations with the two unknown angles so can definitely solve – Eric F May 1 '18 at 19:02
• Great. Glad I could help. – Arnaud Mortier May 1 '18 at 19:05

You’re essentially looking for the difference in the spherical coordinates of the two vectors, so you can use the standard formulas for conversion from Cartesian coordinates: $$r=\sqrt{x^2+y^2+z^2} \\ \theta = \arccos{\frac zr} \\ \phi = \arctan{\frac yx}.$$ Remember that $\theta$ is measured from the positive $z$-axis, and you’ll need to adjust $\Delta\theta$ to be in the range $(-\pi,\pi]$ if you want the use both clockwise and counterclockwise rotations about the $z$-axis for the first rotation.

If all you really need is the two rotations, though, they can be constructed without explicitly computing either angle. The basic idea is to build sets of orthonormal bases that represent the “before” and “after” states. For the first rotation, project $\mathbf u$ and $\mathbf v$ onto the $x$-$y$ plane and normalize to get $\mathbf u' = (u_x^2+u_y^2)^{-1/2}(u_x,u_y,0)^T$ and $\mathbf v' = (v_x^2+v_y^2)^{-1/2}(v_x,v_y,0)^T$, respectively. The required rotation matrix is then $$R_1 = \begin{bmatrix}\mathbf u'&\mathbf e_z\times\mathbf u'&\mathbf e_z\end{bmatrix} \begin{bmatrix}\mathbf v'&\mathbf e_z\times\mathbf v'&\mathbf e_z\end{bmatrix}^{-1} = \begin{bmatrix}\mathbf u'&\mathbf e_z\times\mathbf u'&\mathbf e_z\end{bmatrix} \begin{bmatrix}\mathbf v'^T \\ (\mathbf e_z\times\mathbf v')^T \\ \mathbf e_z^T\end{bmatrix},$$ with $\mathbf e_z = (0,0,1)^T$, the unit $z$-vector. (Note that computing the cross product with $\mathbf e_z$ are a matter of swapping coordinates and negating one of them.)

The second rotation is constructed in a similar manner, except that you’re now rotating $R_1\mathbf v$ onto $\mathbf u$, so you’ll need to normalize both of those vectors and use $\mathbf w = R_1\mathbf v\times\mathbf u$, normalized as the rotation axis.

If needed, the rotation angle $\theta$ can be extracted from each of the resulting rotation matrices using the identity $\operatorname{tr}R = 1+2\cos\theta$.