# Thread: Silly yet annoying porblem concening vector rotation

1. ## Silly yet annoying porblem concening vector rotation

Hi guys,

I wish to find the rotation matrix which rotates a general 3d vector (x,y,z) into a z-axis vector (0,0,z'). z' can of course be computed easily, but I'm interested in the rotation matrix. If possible, I'd like to avoid solving a set of 9 algebraic equations to find it.
Can anyone help with this?

2. I recently worked out the matrix multiplications necessary to rotation a vector around a given axis through a given angle. That is related to this problem.

First, you need to know that, in two dimensions, the matrix that rotates around the origin through angle $\theta$ is of the form $\begin{bmatrix}cos(\theta) & -sin(\theta) \\ sin(\theta) & cos(\theta)\end{bmatrix}$. You can see that is true by looking at what it does to the "basis" vectors $\begin{bmatrix}1 \\ 0 \end{bmatrix}$ and $\begin{bmatrix}0 \\ 1\end{bmatrix}$:

$\begin{bmatrix}cos(\theta) & -sin(\theta) \\ sin(\theta) & cos(\theta)\end{bmatrix}\begin{bmatrix}1 \\ 0\end{bmatrix}= \begin{bmatrix}cos(\theta) \\ sin(\theta)\end{bmatrix}$ which is clearly a vector making angle $\theta$ with the x-axis and

$\begin{bmatrix}cos(\theta) & -sin(\theta) \\ sin(\theta) & cos(\theta)\end{bmatrix}\begin{bmatrix}0 \\ 1\end{bmatrix}= \begin{bmatrix} -sin(\theta) \\ cos(\theta)\end{bmatrix}$ which is clearly a vector making angle $\theta$ with the y-axis.

In three dimensions it is easy extend that and see that $\begin{bmatrix}cos(\theta) & -sin(\theta) & 0 \\ sin(\theta) & cos(\theta) & 0 \\ 0 & 0 & 1 \end{bmatrix}$ rotates through an angle $\theta$ around the z-axis and then to see that $\begin{bmatrix}1 & 0 & 0 \\ 0 & cos(\theta) & -sin(\theta) \\ 0 & sin(\theta) & cos(\theta)\end{bmatrix}$ and $\begin{bmatrix}cos(\theta) & 0 & -sin(\theta) \\ 0 & 1 & 0 \\ sin(\theta) & 0 & cos(\theta)\end{bmatrix}$ rotate around the x and y axes, respectively.

To rotate through angle $\theta$, around arbitrary axis $x_0\vec{i}+ y_0\vec{j}+ z_0\vec{k}$, use the following strategy:
1) Rotate around the z- axis so that the vector $\vec{v}= x_0\vec{i}+ y_0\vec{j}+ z_0\vec{k}$ is rotated into $\vec{u}= r\vec{i}+ 0\vec{j}+ z_0\vec{k}$ in the xz-plane. That is, rotate so that the y component is 0. Since a rotation preserves length, we must have $r= \sqrt{x_0^2+ y_0^2}$.

2) Rotate around the y- axis so that the vector $\vec{u}$ is rotated into the vector $\vec{w}= 0\vec{i}+ 0\vec{j}+ \rho\vec{k}$ pointing along the z-axis. Again, since a rotation preserves length, we must have $\rho= \sqrt{x_0^2+ y_0^2+ z_0^2}= \sqrt{r^2+ z_0^2}$.

3) Rotate around the z- axis through angle $\theta$

4) Rotate back reversing the rotation in (2).

5) Rotate back reverseing the rotation in (3).

The rotation in (1), since it is about the z-axis, is of the form $\begin{bmatrix}cos(\theta) & - sin(\theta) & 0 \\ sin(\theta) & cos(\theta) & 0 \\ 0 & 0 & 1\end{bmatrix}$. Since the specific angle is not relevant, I am going to call that $\begin{bmatrix}c & -s & 0 \\ s & c & 0 \\ 0 & 0 & 1\end{bmatrix}$.

That is, we must have $\begin{bmatrix}c & -s & 0 \\ s & c & 0 \\ 0 & 0 & 1\end{bmatrix}\begin{bmatrix}x_0 \\ y_0 \\ z_0\end{bmatrix}= \begin{bmatrix} r \\ 0 \\ z_0\end{bmatrix}$.
That gives the two equations $cx_0- sy_0= r$ and $sx_0+ cy_0= 0$. From the second equation, $s= -\frac{y_0}{x_0}c$. Putting that into the first equation, $cx_0+ \frac{y_0^2}{x_0}c= c\frac{x_0^2+ y_0^2}{y_0}= c\frac{r^2}{y_0}= r$ so that $c= \frac{y_0}{r}$. From that, $s= -\frac{x_0}{r}$.
That is, the matrix required for the first rotation is $\begin{bmatrix}\frac{y_0}{r} & \frac{x_0}{r} & 0 \\ -\frac{x_0}{r} & \frac{y_0}{r} & 0 \\ 0 & 0 & 1\end{bmatrix}$.

Now, for (2) we need to rotate around the y-axis. Specifically, we need $\begin{bmatrix}c & 0 & -s \\ 0 & 1 & 0 \\ s & 0 & c\end{bmatrix}\begin{bmatrix} r \\ 0 \\ z_0\end{bmatrix}= \begin{bmatrix}0 \\ 0 \\ \rho\end{bmatrix}$.
That gives the two equations $cr- sz_0= 0$ and $sr+ cz_0= \rho$. From the first equation, $s= \frac{r}{z_0}c$. Putting that into the second equation, $\frac{r^2}{z_0}c+ z_0c= \frac{r^2+ z_0^2}{z_0}c= \frac{\rho^2}{z_0}= \rho$ so that $c= \frac{z_0}{\rho}$ and then $s= \frac{r}{\rho}$.

That is, the matrix giving the rotation in (2) is $\begin{bmatrix}\frac{z_0}{\rho} & -\frac{r}{\rho} & 0 \\ 0 & 1 & 0 \\ \frac{r}{\rho} & 0 & \frac{z_0}{\rho}\end{bmatrix}$.

The rotation in (3), about the z-axis through angle $\theta$ is, of course, $\begin{bmatrix}cos(\theta) & -sin(\theta) & 0 \\ sin(\theta) & cos(\theta) & 0 \\ 0 & 0 & 1\end{bmatrix}$.

The matrix in (4) is the inverse of the matrix in (2) so we are rotating around the y-axis but through the negative angle. Since cosine is an even function and sine is odd, that just changes the sign on the "s" terms. The matrix needed is
$\begin{bmatrix}\frac{z_0}{\rho} & \frac{r}{\rho} & 0 \\ 0 & 1 & 0 \\ -\frac{r}{\rho} & 0 & \frac{z_0}{\rho}\end{bmatrix}$

The matrix is (5) is the inverse of the matrix is (1) so we just need to change the signs on the "s" terms. The matrix is
$\begin{bmatrix}\frac{y_0}{r} & -\frac{x_0}{r} & 0 \\ \frac{x_0}{r} & \frac{y_0}{r} & 0 \\ 0 & 0 & 1\end{bmatrix}$

Putting that all together, to rotate the vector $\begin{bmatrix}a \\ b \\ c \end{bmatrix}$, around the axis vector $\begin{bmatrix}x_0 \\ y_0 \\ z_0\end{bmatrix}$ through angle $\theta$, do the matrix multiplications:
$\begin{bmatrix}\frac{y_0}{r} & -\frac{x_0}{r} & 0 \\ \frac{x_0}{r} & \frac{y_0}{r} & 0 \\ 0 & 0 & 1\end{bmatrix}$ $\begin{bmatrix}\frac{z_0}{\rho} & \frac{r}{\rho} & 0 \\ 0 & 1 & 0 \\ -\frac{r}{\rho} & 0 & \frac{z_0}{\rho}\end{bmatrix}$ $\begin{bmatrix}cos(\theta) & -sin(\theta) & 0 \\ sin(\theta) & cos(\theta) & 0 \\ 0 & 0 & 1\end{bmatrix}$ $\begin{bmatrix}\frac{z_0}{\rho} & -\frac{r}{\rho} & 0 \\ 0 & 1 & 0 \\ \frac{r}{\rho} & 0 & \frac{z_0}{\rho}\end{bmatrix}$ $\begin{bmatrix}\frac{y_0}{r} & \frac{x_0}{r} & 0 \\ -\frac{x_0}{r} & \frac{y_0}{r} & 0 \\ 0 & 0 & 1\end{bmatrix}$ $\begin{bmatrix}a \\ b \\ c \end{bmatrix}$ where, again, $r= \sqrt{x_0^2+ y_0^2}$ and $\rho= \sqrt{x_0^2+ y_0^2+ z_0^2}= \sqrt{r^2+ z_0^2}$.

That is to rotate around the vector $x_0\vec{i}+ y_0\vec{j}+ z_0\vec{k}$, through angle $\theta$.

To rotate so that vector $\vec{u}= a\vec{i}+ b\vec{j}+ c\vec{k}$ is rotated into vector $\ve{v}= x\vec{i}+ y\vec{j}+ z\vec{k}$, we need to rotate around an axis vector perpendicular to both through the angle they make with each other. Since it is direction that is important here, not length, it is sufficient to assume that both have length 1. If not, just divide each by its length to make that true.

The vector perpendicular to both is, of course, the cross product of the two vectors: $\ve{u}\times\vec{v}= (bz- cy)\vec{i}+ (cx- az)\vec{j}+ (ay- bx)\vec{k}$. That is our " $x_0\vec{i}+ y_0\vec{j}+ z_0\vec{k}$ above: $x_0= bz- cy$, $y_0= cx-az$, and $z_0= ay- bx$.

The angle between the two vectors is given by their dot product: $\vec{u}\cdot\vec{v}= ||\vec{u}||||\vec{v}|| cos(\theta)$. Since it is direction that is important here, not length, it is sufficient to assume that both have length 1. If not, just divide each by its length to make that true. With [math