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. 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 θ 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).
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 are really 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.
The product TP1 ⋅ v is equivalent to the vector sum ⟨−a,−b,−c, 0⟩ + v, i.e., this transformation moves the point P1(a,b,c) to the origin.


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



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.


In this section we introduce matrices to move a rotation vector ⟨u,v,w⟩ to the z-axis. Note that we use the components to form expressions for the cosines and sines to avoid using inverse trigonometric functions. We require that the rotation vector not be parallel to the z-axis, else u = v = 0 and the denominators vanish.


In this section we rotate the point (x,y,z) about the vector ⟨u,v,w⟩ by the angle θ.
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,θ) =

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−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⟩ (where u2 + v2 + w2 = 1) by the angle θ.
f(x,y,z,u,v,w,θ) =

We will define an arbitrary line by a point the line goes through and a direction vector. If the axis of rotation is given by two points P1 = (a,b,c) and P2 = (d,e,f), then a direction vector can be obtained by ⟨u,v,w⟩ = ⟨d−a,e−b,f −c⟩. We can now write a transformation for the rotation of a point about this 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 (a,b,c) with direction vector ⟨u,v,w⟩ by the angle θ.
f(x,y,z,a,b,c,u,v,w,θ) =

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,θ) =

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.