## History and Motivation

Why do we care about quaternions? Short answer: 3D rotations. Longer answer: for centuries, mathematicians have tried to find new, simpler algorithms to perform geometry computations. A common problem is to compute rotations of geometric figures.

### Two Dimensions

For example, in two dimensions, suppose you define a triangle by the Cartesian coordinates of its vertices, such as , , and , which gives us an isosceles right triangle whose legs have length and whose hypotenuse has length .

If one rotates it degrees counterclockwise about the point , the transformed triangle now has vertices , , and . How would we compute that? One method is to use trigonometry; the method can be summarized as: convert the points to polar form, add degrees ( radians) to the angles, and then convert the points back to Cartesian form. With a little algebra, one can collapse these three steps into one formula.

If you are like me, you don’t like to memorize such formulas, and you are looking for something mathematically “natural”. It turns out that one can use complex numbers: the points of the (original, unrotated) triangle are now the three complex numbers , , and . To rotate by degrees counterclockwise, you only need to multiply the numbers by , giving , , and . This is easier to remember. How did we know that is degrees counterclockwise? Well, we still need some trigonometry for that: convert , or in complex numbers, , to polar form, rotate by degrees counterclockwise by adding to the angle, then convert back to the complex number . Unlike before, we only need to do this once instead of 3 times. We then multiply the complex representations of the three points by the result . This is really advantageous for more complex figures, say a sprite with 100 vertices.

### Three dimensions

However, we care about 3D rotations. How would we do this in three dimensions? This problem was the lifelong focus of the Irish mathematician William Rowan Hamilton. He tried to find a three-dimensional version of complex numbers so he could rotate figures in three dimensions by multiplying numbers instead of by converting 3D Cartesian coordinates to, for example, spherical coordinates, adjusting the angles, and then converting back to Cartesian coordinates.

His original plan was unsuccessful: mathematicians would later prove that, in technical language, “one cannot find a three-dimensional associative division ring over the real numbers”. In fact, the theorem showed more: “The only associative division rings over the reals have dimensions 1, 2, or 4”. We know the one-dimensional case is the real numbers, the two-dimensional case is the complex numbers. The next case is four-dimensions. These turn out to be the quaternions.

Hamilton could not find his three-dimensional analog of the complex numbers, but he found a four-dimensional analog instead. He was able to use it for computing 3D rotations as he had hoped. Modern day graphics cards still use Hamilton’s quaternions for this purpose, this being the most efficient way of rotating meshes having millions of vertices.

## What Is a Quaternion?

Just as a complex number looks like where and are real numbers, a quaternion looks like where are real numbers.

Multiplication of quaternions is done algebraically, except you have to remember that it is not commutative. for example, but . Real numbers commute with all other quaternions, so and .

Associativity and distributivity still hold, so with care, ordinary algebraic simplification operations can be done. The simplification rules are: , and all other algebraic operations on quaternions can be derived from these.

### Example Computations

- What is ? We reason as follows:

. Thus we have proved . - Using a similar procedure, we can prove all of the following:

. Using these as theorems, we can now simplify arbitrary quaternion sums, differences, and products. - Let us simplify :

Applying the distributive property repeatedly to gives:

. Cancelling terms gives:

. - The previous example can be generalized by using variables instead of numbers for coefficients. If w, x, y, and z are real numbers, following the same procedure gives us:

.

### Dividing Quaternions

Using example 4 as a theorem, we can now divide quaternions. Since

, and is a real number, we can do algebra to get the following formula: . The last division can be done because the denominator is an ordinary real number.

As an example, let us compute . is just , so using the formula, . Thus, . This of course could have been shown by noting and dividing both sides of this equation by . We do one more example.

.

## Quaternion Formulas

Rather than giving the algebraic procedures for manipulating quaternions by hand, we can apply those procedures to general quaternions (with variables as coefficients, like ) to produce formulas that can be put in our code. Here are the basic formulas. Let and be quaternions, where the are real numbers. Then (and these can be proved through algebra just as with the above calculations):

- Addition:
- Subtraction:
- Multiplication:
- Division:

## Rotations in 3D

Suppose we want to rotate some figure which may be a mesh defined by millions of vertices. One way to specify the rotation is by specifying its rotation in three different directions, with an axis parallel to the x axis, an axis parallel to the y axis, and an axis parallel to the z axis. Order matters! Rotating 30 degrees about the x axis then 20 degrees about the y axis is not going to give the same result as rotating 20 degrees about the y axis and then 30 degrees about the x axis.

In addition to specifying the axis of rotation (a vector, so the x axis is , the y axis is , and the z axis is ), we need a pivot point to rotate around. Thus, a rotation can be specified with three quantities: a vector axis which has to be scaled so that it is a unit vector, that is, so that , a pivot point , and the angle of rotation . Note any nonzero vector can be scaled to a unit vector by replacing with where is the length of the vector, which is equal to .

### Using Quatnerions

We wish to convert this data to a quaternion that can be used to easily compute the rotations of all the vertices of the mesh . This quaternion is then:

.

Then, if the point of that we want to rotate has coordinates , we first make this into a quaternion . Then, compute the product , then express that as a quaternion , and the new rotated point is .

Note, computing is easy for this specific value of : (this works out because is a unit vector, that is, , and also because the square of the sine of something plus the square of the cosine of the same thing is 1; after that, it’s just algebra).

So in summary, the steps are: translate the vertices of so that its pivot point is moved to the origin; Rotate about the specified axis by sending each vertex to ; then translate the result back to the original pivot point.

### Three Axes of Rotation

But we want to rotate, in general, about three axes, a different angle for each axis. How do we do that? Well, the information is there if you read between the lines! Do it like this:

Let be the value of used to perform the rotation about the axis, which is the vector , with the angle we want. Similarly, let be the quaternion for the axis and the angle for that, and be for the axis and the angle for that. Then, rotating about the axis first, then the axis, and then the axis, is the same as using the quaternion for rotating. Thus each point is moved to the point which can also be written as . Remember, order matters with quaternions: they do not generally commute! As before, we do this after translating from the pivot point to the origin, and after that, we translate the changed back to the pivot point.

## Example

As an example, let us work out by hand (part of) the rotation of a cube. A former boss told me, you don’t understand a computation unless you can do it (a small case or a representative part of it) by hand.

So, suppose we have a cube whose eight vertices are at , , , , , , , and . This is a 1x1x1 cube whose faces are oriented perpendicular to the , , and axes and whose center is at . We shall use this center as the pivot point, and rotate the cube 45 degrees counterclockwise (when looking downward) about the axis (or rather, the line through the pivot point that is parallel to the axis). Here is a screenshot from Blender showing the cube:

Note that, as is somewhat standard in many graphics applications, the axis is red, the axis is green, and the axis is blue.

First let us compute *q*, the rotation quaternion. , where degrees (or radians), and . Thus,

.

and

.

Now, for each vertex, we translate by , make a quaternion out of it, compute , then translate by . Here we go:

- so gets rotated to .

Similarly, we have the following rotations:

- rotates to
- rotates to
- rotates to
- rotates to
- rotates to
- rotates to
- and rotates to .

## Pseudocode

Let *F* be a mesh with any number of vertices.

Let *(a_x, a_y, a_z)* be the axis of rotation, and* (o_x, o_y, o_z)* be the pivot.

Suppose we want to rotate by an angle *t*, in radians, counterclockwise

when looking down the axis (from positive to negative).

Let *q = [q_w, q_x, q_y, q_z] = [cos(t/2), a_x sin(t/2), a_y sin(t/2), a_z sin(t/2)]* and its

inverse *q’ = [q’_w, q’_x, q’_y, q’_z] = [cos(t/2), -a_x sin(t/2), -a_y sin(t/2), -a_z sin(t/2)]*

For each vertex* (v_x, v_y, v_z)* of the mesh *F*:

Let* p = [p_w, p_x, p_y, p_z] = [0, v_x-o_x, v_y-o_y, v_z-o_z]*

Compute *p’ = [p’_w, p’_x, p’_y, p’_z] = qpq’* as follows:

*p’_w = 0* (since it is equal to* p_w (q_w^2 + q_x^2 + q_y^2 + q_z^2)*, and *p_w=0*)

*p’_x = 2 (p_z q_w q_y + p_y q_x q_y – p_y q_w q_z + p_z q_x q_z) +*

*p_x (q_w^2 + q_x^2 – q_y^2 – q_z^2)*

*p’_y = 2 (-p_z q_w q_x + p_x q_x q_y + p_x q_w q_z + p_z q_y q_z) +*

*p_y (q_w^2 – q_x^2 + q_y^2 – q_z^2)*

*p’_z = 2 (p_y q_w q_x – p_x q_w q_y + p_x q_x q_z + p_y q_y q_z) +*

*p_z (q_w^2 – q_x^2 – q_y^2 + q_z^2)*

Then, compute *(v’_x, v’_y, v’_z) = (p’_x+o_x, p’_y+o_y, p’_z+o_z)*.

Replace the vertex *(v_x, v_y, v_z)* in F with the rotated vertex* (v’_x, v’_y, v’_z)*

End For

You must be logged in to post a comment.