# 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 $\displaystyle \theta$ is of the form $\displaystyle \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 $\displaystyle \begin{bmatrix}1 \\ 0 \end{bmatrix}$ and $\displaystyle \begin{bmatrix}0 \\ 1\end{bmatrix}$:

$\displaystyle \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 $\displaystyle \theta$ with the x-axis and

$\displaystyle \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 $\displaystyle \theta$ with the y-axis.

In three dimensions it is easy extend that and see that $\displaystyle \begin{bmatrix}cos(\theta) & -sin(\theta) & 0 \\ sin(\theta) & cos(\theta) & 0 \\ 0 & 0 & 1 \end{bmatrix}$ rotates through an angle $\displaystyle \theta$ around the z-axis and then to see that $\displaystyle \begin{bmatrix}1 & 0 & 0 \\ 0 & cos(\theta) & -sin(\theta) \\ 0 & sin(\theta) & cos(\theta)\end{bmatrix}$ and $\displaystyle \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 $\displaystyle \theta$, around arbitrary axis $\displaystyle 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 $\displaystyle \vec{v}= x_0\vec{i}+ y_0\vec{j}+ z_0\vec{k}$ is rotated into $\displaystyle \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 $\displaystyle r= \sqrt{x_0^2+ y_0^2}$.

2) Rotate around the y- axis so that the vector $\displaystyle \vec{u}$ is rotated into the vector $\displaystyle \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 $\displaystyle \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 $\displaystyle \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 $\displaystyle \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 $\displaystyle \begin{bmatrix}c & -s & 0 \\ s & c & 0 \\ 0 & 0 & 1\end{bmatrix}$.

That is, we must have $\displaystyle \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 $\displaystyle cx_0- sy_0= r$ and $\displaystyle sx_0+ cy_0= 0$. From the second equation, $\displaystyle s= -\frac{y_0}{x_0}c$. Putting that into the first equation, $\displaystyle 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 $\displaystyle c= \frac{y_0}{r}$. From that, $\displaystyle s= -\frac{x_0}{r}$.
That is, the matrix required for the first rotation is $\displaystyle \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 $\displaystyle \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 $\displaystyle cr- sz_0= 0$ and $\displaystyle sr+ cz_0= \rho$. From the first equation, $\displaystyle s= \frac{r}{z_0}c$. Putting that into the second equation, $\displaystyle \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 $\displaystyle c= \frac{z_0}{\rho}$ and then $\displaystyle s= \frac{r}{\rho}$.

That is, the matrix giving the rotation in (2) is $\displaystyle \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 $\displaystyle \theta$ is, of course, $\displaystyle \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
$\displaystyle \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
$\displaystyle \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 $\displaystyle \begin{bmatrix}a \\ b \\ c \end{bmatrix}$, around the axis vector $\displaystyle \begin{bmatrix}x_0 \\ y_0 \\ z_0\end{bmatrix}$ through angle $\displaystyle \theta$, do the matrix multiplications:
$\displaystyle \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}$$\displaystyle \begin{bmatrix}\frac{z_0}{\rho} & \frac{r}{\rho} & 0 \\ 0 & 1 & 0 \\ -\frac{r}{\rho} & 0 & \frac{z_0}{\rho}\end{bmatrix}$$\displaystyle \begin{bmatrix}cos(\theta) & -sin(\theta) & 0 \\ sin(\theta) & cos(\theta) & 0 \\ 0 & 0 & 1\end{bmatrix}$$\displaystyle \begin{bmatrix}\frac{z_0}{\rho} & -\frac{r}{\rho} & 0 \\ 0 & 1 & 0 \\ \frac{r}{\rho} & 0 & \frac{z_0}{\rho}\end{bmatrix}$$\displaystyle \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}$$\displaystyle \begin{bmatrix}a \\ b \\ c \end{bmatrix}$ where, again, $\displaystyle r= \sqrt{x_0^2+ y_0^2}$ and $\displaystyle \rho= \sqrt{x_0^2+ y_0^2+ z_0^2}= \sqrt{r^2+ z_0^2}$.

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

To rotate so that vector $\displaystyle \vec{u}= a\vec{i}+ b\vec{j}+ c\vec{k}$ is rotated into vector $\displaystyle \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: $\displaystyle \ve{u}\times\vec{v}= (bz- cy)\vec{i}+ (cx- az)\vec{j}+ (ay- bx)\vec{k}$. That is our "$\displaystyle x_0\vec{i}+ y_0\vec{j}+ z_0\vec{k}$ above: $\displaystyle x_0= bz- cy$, $\displaystyle y_0= cx-az$, and $\displaystyle z_0= ay- bx$.

The angle between the two vectors is given by their dot product: $\displaystyle \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