Rotation About an Arbitrary Axis in 3 Dimensions

Glenn Murray, Colorado School of Mines



Introduction

The problem of rotation about an arbitrary axis in three dimensions arises in many fields including computer graphics and molecular simulation. In this article we give an algorithm and matrices for calculating the motion. Many of the results were initially obtained with Mathematica.

An algorithm (see Figure 1):

(1) translate space so that the rotation axis passes through the origin

(2) rotate space about the $ z$ -axis so that the rotation axis lies in the $ xz$ -plane

(3) rotate space about the $ y$ -axis so that the rotation axis lies along the $ z$ -axis

(4) perform the desired rotation by $ \theta$ about the $ z$ -axis

(5) apply the inverse of step (3)

(6) apply the inverse of step (2)

(7) apply the inverse of step (1)

Figure 1: Moving the axis of rotation $ A$ to the $ z$ -axis
\includegraphics[width=4in]{Figure_1}

This algorithm assumes that the axis of rotation is not parallel to the $ z$ -axis. If it is, we may omit steps (2), (3), (5), and (6); see the Programming Notes in Section 8. The choice of $ z$ -axis is arbitrary, we could just as well have used the $ x$ -axis or $ y$ -axis. The final results in Section 6 and Section 7 are independent of the choice of axis in the algorithm.

We will write our three-dimensional points in four homogeneous coordinates; i.e., (x, y, z) will be written as (x, y, z, 1). This enables us to do coordinate transformations using 4x4 matrices. Note that these 4x4 matrices are only necessary for translations, if we omitted translations from our movements we could do the motions with 3x3 rotation matrices obtained by deleting the last rows and last columns of the 4x4 matrices. In this article vectors are multiplied by matrices on the vector's left.

A translation matrix

The product Tp.v is equivalent to the vector sum $ \langle -a, -b, -c,
0\rangle + v$ , i.e., this transformation moves the point $ P_1(a, b,
c)$ to the origin.

\begin{displaymath}
T_{P_1} =
\left[
\begin{array}{cccc}
1 &0 &0 &-a \\
0 &1 &0 &-b \\
0 &0 &1 &-c \\
0 &0 &0 &1
\end{array}
\right]
\end{displaymath}

Rotation matrices

Here are the matrices for rotation by $ \alpha$ around the $ x$ -axis, $ \beta$ around the $ y$ -axis, and $ \gamma$ around the $ z$ -axis.

$\displaystyle R_x(\alpha) =
\left[ \begin{array}{cccc}
1&0&0&0 \\
0&\cos\alph...
...\sin\alpha &0 \\
0&\sin\alpha &\cos\alpha &0 \\
0&0&0&1
\end{array} \right]
$

$\displaystyle R_y(\beta) =
\left[ \begin{array}{cccc}
\cos\beta &0&\sin\beta &0 \\
0&1&0&0 \\
-\sin\beta &0&\cos\beta &0 \\
0&0&0&1
\end{array} \right]
$

$\displaystyle R_z(\gamma) =
\left[ \begin{array}{cccc}
\cos\gamma &-\sin\gamma...
...0 \\
\sin\gamma &\cos\gamma &0&0 \\
0&0&1&0 \\
0&0&0&1
\end{array} \right]
$

The general rotation matrix depends on the order of rotations. The first matrix rotates about $ x$ , then $ y$ , then $ z$ ; the second rotates about $ z$ , then $ y$ , then $ x$ .

$\displaystyle R_z R_y R_x =
\left[ \begin{array}{cccc}
\cos\beta \cos\gamma &\...
...\cos\beta \sin\alpha &\cos\alpha \cos\beta &0 \\
0&0&0&1
\end{array} \right]
$

$\displaystyle R_x R_y R_z =
\left[ \begin{array}{cccc}
\cos\alpha \cos\beta &-...
...\cos\beta \sin\gamma &\cos\beta \cos\gamma &0 \\
0&0&0&1
\end{array} \right]
$

Transformations for moving a vector to the z-axis

In this section we introduce matrices to move a rotation vector $ \langle u, v, w\rangle$ to the $ z$ -axis. This is illustrated in Figure 1. Note that we use the vector's components to form expressions for the cosines and sines. We require that the rotation vector not be parallel to the $ z$ -axis, else $ u=v=0$ and the denominators vanish.

The matrix to rotate a vector about the $ z$ -axis to the $ xz$ -plane is

$\displaystyle R_{xz} =
\left[ \begin{array}{cccc}
u\big/{\sqrt{{u^2}+{v^2}}}&v...
...}&u\big/{\sqrt{{u^2}+{v^2}}}&0&0 \\
0&0&1&0 \\
0&0&0&1
\end{array} \right].
$

The matrix to rotate the vector in the xz-plane to the $ z$ -axis is

$\displaystyle R_{xz2z} =
\left[ \begin{array}{cccc}
w\big/{\sqrt{u^2+v^2+w^2}}...
...u^2+v^2+w^2}}&0&w\big/{\sqrt{u^2+v^2+w^2}}&0 \\
0&0&0&1
\end{array} \right].
$

Rotations about the origin

In this section we rotate the point $ (x, y, z)$ about the vector $ \langle u, v, w\rangle$ by the angle $ \theta$ . The matrix for rotations about the origin is the product $ R_{xz}^{-1} R_{xz2z}^{-1}
R_z(\theta) R_{xz2z} R_{xz}$ :

$\displaystyle %\hspace{-.4in}
\Large {
\left[ \hspace{-6pt} \begin{array}{cccc}...
...theta}{u^2+v^2+w^2}
&0 \\ [10pt]
0&0&0&1
\end{array} \hspace{-6pt} \right].
}
$

If we multiply this times the point to be rotated, $ (x, y, z)$ , we get the rotated point:

$\displaystyle \Large {
\left[ \begin{array}{c}
\frac{u (u x+v y+w z) + \big(x(v...
...+w^2} (-v x+u y) \sin\theta }{u^2+v^2+w^2} \\ [10pt]
1
\end{array} \right].
}
$


Rotation about an arbitrary line

We must give the axis an orientation so that positive and negative angles of rotation are defined. If the axis of rotation is given by two points $ P_1 = (a, b, c)$ and $ P_2 = (d, e, f)$ , then the (oriented) vector of rotation can be given by $ \langle u, v, w \rangle
= \langle d - a, e - b, f - c \rangle$ . The matrix for rotation about an arbitrary line is given by the product

$\displaystyle T_{P_1}^{-1} R_{xz}^{-1} R_{xz2z}^{-1} R_z(\theta) R_{xz2z} R_{xz}T_{P_1}.$

This is the rotation operator for rotations about the line through $ P_1(a, b,
c)$ parallel to $ \langle u, v, w\rangle$ by the angle $ \theta$ . In hopes of fitting the matrix onto the page we make the substitution $ L=\sqrt{u^2+v^2+w^2}$ .

$\displaystyle \hspace{-40pt} % Squeeze this large matrix to the left
\Large {
\...
...n\theta}{L^2} \\ [10pt]
\par
0&0&0&1
\par
\end{array} \hspace{-6pt} \right]
}
$

If we multiply this times the point to be rotated, $ (x, y, z)$ , we get the rotated point.

$\displaystyle %\hspace{-.3in}
\Large {
\left[ \hspace{-6pt} \begin{array}{c}
\p...
... y) \sin\theta}{u^2+v^2+w^2}
\\ [10pt]
1
\end{array} \hspace{-6pt} \right]
}
$


A function for an arbitrary rotation

We can extract the first terms of the homogeneous coordinates of the rotated point in the last section to obtain a three dimensional function of ten variables for the rotation of the point $ (x, y, z)$ about the line through $ (a, b ,c)$ parallel to $ \langle u, v, w\rangle$ by the angle $ \theta$ . It is simplified here as a convenience.


$ f(x, y, z, a, b, c, u, v, w, \theta) = $

$\displaystyle %\hspace{-.3in}
\Large {
\left[ \hspace{-6pt} \begin{array}{c}
\p...
... - vx + uy) \sin\theta}{u^2+v^2+w^2}
\par
\end{array} \hspace{-6pt} \right]
}
$


Programming notes

The most immediate way to program the result is to use the function in the last section. This is the simplest way to obtain a single rotation. However, to apply rotations to many points it is more efficient to calculate the matrix of rotation and apply it to each point.

If it is known that the axis of rotation is nearly parallel to one of the coordinate axes then the rotation and translations matrices given earlier can be used directly. For example, in the case of the axis of rotation being nearlly parallel to the $ z$ -axis (say $ u^2+v^2 <
\epsilon$ for some small $ \epsilon$ ) then the rotation can be approximated by $ T_P^{-1}R_z(\theta) T_P$ , or, in the case where the axis is oriented opposite to the $ z$ -axis, by $ T_P^{-1}R_z(-\theta)T_P$ . This method is often given in textbooks and online resources as it provides a check against a division by zero error.

A Java implementation of the rotations about an arbitrary line is available at

http://www.mines.edu/~gmurray/ArbitraryAxisRotation/java.zip

Note that with most browsers, the original LATEX for the matrices may be seen by viewing the page source. This may be helpful for translation to other languages.

About this document ...

Rotation About an Arbitrary Axis in 3 Dimensions

This document was generated using the LaTeX2HTML translator Version 2002-2-1 (1.71) The command line arguments were:
latex2html -white -split +0 -no_navigation ArbitraryAxisRotation.tex


Glenn Murray 2005-09-07