You are on page 1of 6

Rotation About an Arbitrary Axis in 3 Dimensions

Glenn Murray
June 6, 2013

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):

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

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

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

A translation matrix

The product TP1 v is equivalent to the vector sum ha, b, c, 0i+v, i.e., this transformation
moves the point P1 (a, b, c) to the origin.

1 0 0 a
0 1 0 b

TP1 =
0 0 1 c
0 0 0 1

1 0 0 a
0 1 0 b

TP1 =
0 0 1 c
0 0 0 1

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
sin cos
Rz () =
0
0
0
0

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.

0
0

0
1

0
0

0
1

cos cos cos sin sin cos sin cos cos sin + sin sin
cos sin cos cos + sin sin sin cos sin + cos sin sin
Rz Ry Rx =
sin
cos sin
cos cos
0
0
0
cos cos
cos sin
sin
cos sin + sin sin cos cos cos sin sin sin sin cos
Rx Ry Rz =
sin sin cos sin cos sin cos + cos sin sin cos cos
0
0
0

Transformations for rotating a vector to the z -axis

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

4.1

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




u u2 + v 2 v u2 + v 2
v u2 + v 2 u u2 + v 2
=

0
0
0
0

Txz

4.2

0
0

0
1

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

u2 + v 2 + w 2

0
Tz =
u2 + v 2 u2 + v 2 + w2
0

0
0
1
0


0 u2 + v 2 u2 + v 2 + w2
1
 0
0
w u2 + v 2 + w2
0
0

0
0

0
1

Rotations about the origin

In this section we rotate the point (x, y, z) about the vector hu, v, wi by the angle .
3

5.1

The matrix for rotations about the origin

1 1
This is the product Txz
Tz Rz ()Tz Txz .

u2 +(v 2 +w2 ) cos


u2 +v 2 +w2

uv(1cos )+w u2 +v 2 +w2 sin


u2 +v 2 +w2

uw(1cos )v u2 +v 2 +w2 sin


u2 +v 2 +w2

uv(1cos )w u2 +v 2 +w2 sin


u2 +v 2 +w2
2

v +(u +w ) cos
u2 +v 2 +w2

vw(1cos )+u u2 +v 2 +w2 sin


u2 +v 2 +w2

uw(1cos )+v u2 +v 2 +w2 sin


u2 +v 2 +w2

vw(1cos )u u2 +v 2 +w2 sin


u2 +v 2 +w2
w2 +(u2 +v 2 ) cos
u2 +v 2 +w2

If we multiply this times hx, y, zi we can obtain a function of of seven variables that yields
the result of rotating the point (x, y, z) about the axis hu, v, wi by the angle .
f (x, y, z, u, v, w, ) =

5.2

u(ux+vy+wz)(1cos )+(u2 +v 2 +w2 )x cos + u2 +v 2 +w2 (wy+vz) sin


u2 +v 2 +w2

v(ux+vy+wz)(1cos )+(u2 +v 2 +w2 )y cos + u2 +v 2 +w2 (wxuz) sin


u2 +v 2 +w2

w(ux+vy+wz)(1cos )+(u2 +v 2 +w2 )z cos + u2 +v 2 +w2 (vx+uy) sin


u2 +v 2 +w2

The normalized matrix for rotations about the origin

At this point we would like to simplify the expressions by making the assumption that
hu, v, wi is a unit vector; i.e., that u2 + v 2 + w2 = 1. With this simplification, we obtain the
1 1
following expression for Txz
Tz Rz ()Tz Txz .

2
u + (1 u2 ) cos
uv(1 cos ) w sin uw(1 cos ) + v sin 0

uv(1 cos ) + w sin


2
2
v
+
(1

v
)
cos

vw(1

cos
)

u
sin

2
2
uw(1 cos ) v sin vw(1 cos ) + u sin
w + (1 w ) cos
0

0
0
0
1
If we multiply this times hx, y, zi we can obtain a function of of seven variables that yields
the result of rotating the point (x, y, z) about the axis hu, v, wi (where u2 + v 2 + w2 = 1) by
the angle .
4

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

u(ux + vy + wz)(1 cos ) + x cos + (wy + vz) sin

v(ux + vy + wz)(1 cos ) + y cos + (wx uz) sin

w(ux + vy + wz)(1 cos ) + z cos + (vx + uy) sin

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 hu, v, wi = hd a, e b, f ci. 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

1 1
This is given by the product TP1
Tz Rz ()Tz Txz TP1 . In hopes of fitting the matrix onto
Txz
1
the page we make the substitution L = u2 + v 2 + w2 .

u2 +(v 2 +w2 ) cos


L

uv(1cos )+w L sin

uw(1cos )vL sin

uv(1cos )w L sin
L

uw(1cos )+v L sin


L

a(v 2 +w2 )u(bv+cw) (1cos )+(bwcv) L sin

v 2 +(u2 +w2 ) cos


L

vw(1cos )u L sin
L

b(u2 +w2 )v(au+cw) (1cos )+(cuaw) L sin

vw(1cos )+u L sin


L

w2 +(u2 +v 2 ) cos
L

(1cos )+(avbu) L sin

c(u2 +v 2 )w(au+bv)

If we multiply this times hx, y, zi 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
hu, v, wi by the angle .
f (x, y, z, a, b, c, u, v, w, ) =

a(v 2 +w2 )u(bv+cwuxvywz) (1cos )+Lx cos + L(cv+bwwy+vz) sin


L

2
2
b(u +w )v(au+cwuxvywz) (1cos )+Ly cos + L(cuaw+wxuz) sin

2
2
c(u +v )w(au+bvuxvywz) (1cos )+Lz cos + L(bu+avvx+uy) sin
L
5

6.2

The normalized matrix for rotation about an arbitrary line

Assuming that hu, v, wi is a unit vector so that L = 1, we obtain a more practical result for
1 1
Tz Rz ()Tz Txz TP1 .
TP1
Txz
1
u2 + (v 2 + w2 ) cos

uv(1 cos ) w sin

uw(1 cos ) + v sin

uv(1 cos ) + w sin

uw(1 cos ) v sin

v 2 + (u2 + w2 ) cos

vw(1 cos ) u sin

vw(1 cos ) + u sin

w2 + (u2 + v 2 ) cos

a(v 2 + w2 ) u(bv + cw) (1 cos ) + (bw cv) sin

b(u2 + w2 ) v(au + cw) (1 cos ) + (cu aw) sin


c(u2 + v 2 ) w(au + bv) (1 cos ) + (av bu) sin

If we multiply this times hx, y, zi 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
hu, v, wi (where u2 + v 2 + w2 = 1) by the angle .
f (x, y, z, a, b, c, u, v, w, ) =


a(v 2 + w2 ) u(bv + cw ux vy wz) (1 cos ) + x cos + (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


2
2
c(u + v ) w(au + bv ux vy wz) (1 cos ) + z cos + (bu + av vx + uy) sin

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.

You might also like