Matrix Rotations About an Arbitrary Axis in 3 Dimensions

Glenn Murray

Feb 10, 2024

1.  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 doing the movement about any axis, not just those through the origin. Many of the results were initially obtained with Mathematica.

An arbitrary axis in three dimensions is any line and can be described with the following information: a point P on the line, and a direction vector A(u,v,w) for that line. Once the point P is translated to the origin, then a sequence of rotations about coordinate axes will move the rotation axis to the z-axis, as shown in Figure 1:

Figure 1: Moving the axis of rotation A to the z-axis.

Here is a description of the algorithm. Let θ denote the angle of rotation.

If these matrix transformations are applied to any point P(x,y,z), then it will be rotated by θ around the given axis of rotation.

Note that 3x3 matrices are sufficient only for rotations about axes through the origin. To handle the translation of arbitrary axes to the origin we need to use 4x4 matrices. Accordingly, 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 as described in the next section. If we omitted translations from our movements we could do the motions with 3x3 rotation matrices obtained by deleting the last row and last column of the 4x4 matrices. In this article vectors are multiplied by matrices on the vector’s left.

2.  A translation matrix

For a vector v = ⟨a,b,c, 1⟩, the matrix 

times v yields ⟨0, 0, 0, 1⟩; i.e., this matrix translates the point P(a,b,c) to the origin. 

3.  3D Coordinate axes rotation matrices

Here are the matrices for rotation by α around the x-axis, β around the y-axis, and γ around the z-axis.

An arbitrary rotation is achieved by applying these and their inverses in succession. From Sec. 1, we see we only need Ry and Rz.

The general rotation matrix depends on the application order. Here is an example of the axis rotation matrices applied in different orders. The first matrix rotates about x, then y, then z; the second rotates about z, then y, then x. As expected, the final results differ, since matrix multiplication is not commutative.

4.  Transformations for rotating a vector to the z-axis

In this section we introduce matrices to move a direction vector ⟨u,v,w⟩ to the z-axis as described in the algorithm in Sec. 1. We use the Rz and the Ry matrices of Sec. 3 to rotate the vector to the xz-plane and the z-axis, respectively, and we use the vector’s components to form expressions for the cosines and sines

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

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

5.  Rotations about the origin

In this section we rotate the point (x,y,z) about the vector ⟨u,v,w⟩ by the angle θ.

5.1  The matrix for rotations about the origin

This is the product Txz−1T z−1R z(θ)TzTxz.

If we multiply this times ⟨x,y,z⟩ we can obtain a function of of seven variables that yields the result of rotating the point (x,y,z) about the axis ⟨u,v,w⟩ by the angle θ.

f(x,y,z,u,v,w,θ) =

5.2  The normalized matrix for rotations about the origin

At this point we would like to simplify the expressions by making the assumption that ⟨u,v,w⟩ is a unit vector; i.e., that u2 + v2 + w2 = 1. With this simplification, we obtain the following expression for Txz−1T z−1Rz(θ)TzTxz.

This is known as Rodrigues’ rotation formula. If we multiply this matrix times ⟨x,y,z⟩ we can obtain a function of seven variables that yields the result of rotating the point (x,y,z) about the axis ⟨u,v,w⟩ (where u2 + v2 + w2 = 1) by the angle θ.

f(x,y,z,u,v,w,θ) =

6.  Rotation about an arbitrary line

We now consider rotating about an axis not necessarily through the origin. Our axix is an arbitrary line defined by an arbitrary point P(a,b,c) and an arbitrary non-zero direction vector ⟨u,v,w⟩. Note that the results in this section reduce to the results for rotations about the origin (Sec. 5) when P is the zero vector. 

6.1  The matrix for rotation about an arbitrary line

This is given by the product TP1−1T xz−1T z−1R z(θ)TzTxzTP1. In hopes of fitting the matrix onto the page we make the substitution L = u2 + v2 + w2.

If we multiply this times ⟨x,y,z⟩ we can obtain a function of of ten variables that yields the result of rotating the point (x,y,z) about the line through P(a,b,c) with direction vector ⟨u,v,w⟩ by the angle θ.

f(x,y,z,a,b,c,u,v,w,θ) =

6.2  The normalized matrix for rotation about an arbitrary line

Assuming that ⟨u,v,w⟩ is a unit vector so that L = 1, we obtain a more practical result for TP1−1T xz−1T z−1R z(θ)TzTxzTP1.

If we multiply this times ⟨x,y,z⟩ we can obtain a function of of ten variables that yields the result of rotating the point (x,y,z) about the line through (a,b,c) with direction vector ⟨u,v,w⟩ (where u2 + v2 + w2 = 1) by the angle θ.

f(x,y,z,a,b,c,u,v,w,θ) =

7.  Code and visualization

A graphic visualization of rotating a point about a line can be found at http://twist-and-shout.appspot.com/.

Tested Java code for the matrices and formulas, released under the Apache license, is available at https://sites.google.com/site/glennmurray/Home/rotation-matrices-and-formulas.