Rotation About an Arbitrary Axis in 3 Dimensions

Glenn Murray

June 6, 2013

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. Many of the results were initially obtained with Mathematica.

An algorithm (See Figure 1):


PIC

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


(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.

2 A translation matrix

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.

      ⌊             ⌋
        1  0  0  - a
T   = || 0  1  0  - b||
 P1   ⌈ 0  0  1  - c⌉
        0  0  0   1

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.

         ⌊                      ⌋
           1    0       0     0
         || 0  cos α  - sinα   0 ||
Rx (α) = ⌈ 0  sin α   cosα    0 ⌉
           0    0       0     1

         ⌊                      ⌋
            cos β   0  sinβ   0
         |    0     1    0    0 |
Ry (β) = |⌈                      |⌉
           - sin β  0  cosβ   0
              0     0    0    1

         ⌊                     ⌋
           cos γ  - sinγ  0  0
         | sin γ   cosγ   0  0 |
Rz (γ) = |⌈   0       0    1  0 |⌉

             0       0    0  1

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.

           ⌊                                                                           ⌋
              cosβ cosγ   cosγ sin α sin β - cosα sinγ   cos α cosγ sin β + sin α sin γ   0
           |  cosβ sinγ   cosα cosγ + sinα sinβ sinγ  -  cos γ sin α + cosα sinβ sin γ  0 |
RzRyRx   = |⌈   -  sin β            cos β sinα                    cosα cosβ            0 |⌉

                  0                   0                             0                1

           ⌊                                                                           ⌋
                      cos β cosγ                  - cos β sin γ             sinβ      0
           |  cosα sinγ + sinα sinβ cosγ   cosα cosγ - sinα sinβ sinγ  -  sin α cosβ  0 |
RxRyRz   = |⌈                                                                           |⌉
              sin α sin γ - cosα sinβ cosγ   sin α cosγ + cosα sinβ sinγ   cos α cosβ   0
                           0                            0                    0       1

4 Transformations for rotating a vector to the z-axis

In this section we introduce matrices to move a rotation vector u,v,wto 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.

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

      ⌊     √ -2----2-    √ -2----2-      ⌋
         u/  √u-+--v--- v/√ u-+--v-- 0  0
      ||  - v/  u2 + v2 u/   u2 + v2  0  0 ||
Txz = ⌈       0              0       1  0 ⌉
              0              0       0  1

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

      ⌊        √-------------          √ --------√ -------------   ⌋
            w/  u2 + v2 + w2      0  -   u2 + v2/  u2 + v2 + w2  0
      |             0             1               0              0 |
Tz =  |⌈ √u2--+-v2/√u2--+-v2-+-w2- 0       w/ √u2-+-v2-+-w2-      0 |⌉

                    0             0               0              1

5 Rotations about the origin

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

5.1 The matrix for rotations about the origin

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

⌊        2   2  2                           √-2---2--2-                  √ -2--2---2-       ⌋
        u+-(2v-+w2-)co2sθ-        uv(1- cosθ)-2 w-2u-+v2-+w-sinθ uw-(1- cosθ)+2v-2u-+v2-+w-sin-θ  0
|         u +v  +w                      u +v +w                       u +v +w               |
||uv(1- cosθ)+w √u2+v2+w2--sin θ        v2+(u2+w2 )cosθ        vw (1- cosθ)- u√u2+v2+w2-sin θ   ||
|---------u2+v2+w2-----------        ---u2+v2+w2----        ----------u2+v2+w2----------  0 |
|             √ ----------                  √----------                                     |
||uw-(1--cosθ)--v--u2+v2+w2--sin-θ  vw(1- cosθ)+u-u2+v2+w2-sinθ-        w2+-(u2+v2-)cosθ-        0 ||
⌈         u2+v2+w2                      u2+v2+w2                      u2+v2+w2              ⌉

              0                             0                            0                1

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

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

⌊                                                √ -----------                ⌋
   u (ux+vy+wz    )(1- cos θ)+(u2+v2+w2   )xcos θ+   u2+v2+w2  (- wy+vz  )sinθ
   ---------------------------------2---2---2--------------------------------
|                                 u  +v  +w      √ -----------                |
|   v(ux+vy+wz   )(1- cosθ)+ (u2+v2+w2   )ycosθ+   u2+v2+w2   (wx - uz)sinθ   |
||   --------------------------------2---2---2-------------------------------  ||
⌈                                 u  +v  +w        -----------                ⌉
   w (ux+vy+wz   )(1- cosθ)+ (u2+v2+w2  )z cosθ+ √ u2+v2+w2   (- vx+uy  )sinθ
   ---------------------------------2---2---2--------------------------------
                                  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,wis 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.

⌊    2         2                                                          ⌋
    u  + (1 - u )cosθ    uv (1 - cosθ) - w sin θ  uw (1 - cosθ) + vsin θ  0
||                            2        2                                   ||
|| uv(1 - cos θ) + w sinθ    v + (1 - v ) cosθ    vw (1 - cosθ) - usin θ  0||
|                                                   2         2           |
|⌈ uw (1 - cosθ) - vsinθ  vw (1 - cosθ ) + u sin θ   w  + (1 - w ) cosθ    0|⌉

            0                       0                      0             1

If we multiply this times x,y,zwe 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,θ) =

⌊                                                       ⌋
  u(ux + vy + wz )(1 - cosθ) + x cosθ + (- wy + vz) sin θ
|                                                       |
||  v(ux + vy + wz )(1 - cosθ) + ycos θ + (wx - uz) sin θ ||
⌈                                                       ⌉
  w(ux +  vy + wz )(1 - cos θ) + z cosθ + (- vx + uy) sin θ

6 Rotation about an arbitrary line

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.

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.

⌊                            √--                √--                                    √--   ⌋
 u2+(v2+w2)cosθ    uv(1-cosθ)-w-L-sinθ  uw(1-cosθ)+v-L-sinθ  (a(v2+w2-)--u(bv+cw))(1-cosθ)+(bw--cv)-L-sinθ
|      L                  L                  L                            L                  |
||uv(1-cosθ)+w√L-sinθ    v2+(u2+w2)cosθ    vw(1-cosθ)-u√L-sinθ  (b(u2+w2)-v(au+cw))(1-cosθ)+(cu- aw )√L-sinθ||
||------L--------    ------L------    --------L--------  ------------------L------------------||
|         √ --               √--                                                       √--   |
||uw-(1-cosθ)-v--Lsinθ  vw(1-cosθ)+u-L-sinθ    w2+-(u2+v2)cosθ     (c(u2+v2)-w-(au+bv))(1-cosθ)+(av--bu)-L-sinθ||
⌈      L                  L                  L                            L                  ⌉
       0                  0                  0                            1

If we multiply this times x,y,zwe 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,wby the angle θ.

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

⌊                                                          √ --                       ⌋
   (a(v2+w2-)--u(bv+cw--- ux---vy--wz-))(1- cos-θ)+Lx-cosθ+--L(--cv+bw--- wy+vz-)sin-θ-
                                           L
|                                                           √--                       |
||  (b(u2+w2  )- v(au+cw - ux - vy - wz))(1- cos θ)+Ly cos θ+ L (cu- aw+wx  - uz )sin θ ||
|  ---------------------------------------------------------------------------------- |
⌈                                          L                                          ⌉
       2   2                                               √ --
   (c(u-+v--)--w-(au+bv----ux--vy--wz-))(1--cosθ)+Lz--cosθ+---L(--bu+av---vx+uy--)sin-θ-
                                           L

6.2 The normalized matrix for rotation about an arbitrary line

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

⌊22   2                                                    2    2                                   ⌋
u+(v+ w )cosθ   uv(1- cosθ)- w sinθ  uw (1 - cos θ)+ vsin θ  (a(v + w  )- u(bv + cw ))(1- cosθ)+ (bw - cv)sinθ
||              2    2    2                               2   2                                    ||
||uv(1-cosθ)+ wsinθ   v + (u + w )cosθ   vw (1 - cosθ) - u sin θ (b(u + w )- v(au +cw ))(1- cosθ)+ (cu- aw) sinθ||
||uw(1-cosθ)- vsinθ  vw(1- cosθ)+ u sinθ   w2 + (u2 + v2)cos θ  (c(u2 + v2)- w (au + bv))(1- cosθ)+ (av- bu)sin θ||
|⌈                                                                                                 |⌉
 0                    0                   0                                1

If we multiply this times x,y,zwe 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,θ) =

⌊    2     2                                                                               ⌋
 (a(v  + w  ) - u(bv + cw - ux - vy - wz ))(1 - cos θ) + xcos θ + (- cv + bw - wy + vz)sin θ
||     2    2                                                                               ||
| (b(u  + w  ) - v(au + cw - ux -  vy - wz ))(1 - cosθ ) + y cosθ + (cu - aw + wx - uz )sin θ |
⌈                                                                                          ⌉
  (c(u2 + v2) - w (au + bv - ux - vy - wz ))(1 - cos θ) + zcos θ + (- bu + av - vx + uy )sinθ

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 at https://sites.google.com/site/glennmurray/Home/rotation-matrices-and-formulas.