Associated Topics || Dr. Math Home || Search Dr. Math

### Solving Problems Using Matrices

```
Date: 8/31/95 at 0:34:51
From: Anonymous
Subject: rotations

I am a data communications technician who programs in his spare time.
My question is this.  What is the formula to solve for x,y,z - given
a current x,y,z coordinate (i.e. -60,-60,-60) and an angle
(i.e. 90 degrees) and a plane to rotate about (i.e. z)?

I am currently taking x,y and solving for its angle (with ATN) and radius,
adding to the angle (i.e. 90 degrees), then using sine and cosine to
re-get an x,y coordinate. (Does this make sense?)

In my example I would get (60,-60,-60 x,y,z) using my method.
But it's a tedious method.

Why am I doing this?  I am rotating objects in 3 dimensions.

It's been years since I've taken math classes.  I've been trying to
find some good resources on the internet and referred to some old
books I have.  But no answer to my question.

-Thank you
-Brian
```

```
Date: 9/22/95 at 15:5:9
From: Doctor Jeremy
Subject: Re: rotations

We ("Dr. Math") usually prefer to answer questions from K-12 students, as
that is our mission.  However, as a fellow programmer, I'll answer.

Rotations of any kind can be accomplished by the use of matrices.  I'm not
sure if you know them, so I'll give a brief introduction to them in case
you don't.  If they are old hat to you, skip ahead.

------- BEGIN The dirt on matrices --------

Matrices are a kind of mathematical object.  The theory of matrices is
complicated (being essentially the subject of a semester-long college math
class called "linear algebra"), but the practical deal, as far as rotation
is concerned, is pretty simple.

A matrix is usually represented as a rectangular array of values (usually
numbers); you usually work with matrices of a particular size, such as
3x3 (first being rows, second being columns).

Matrices can be multiplied; any mXn matrix can be multiplied by an nXp
matrix to yield an mXp matrix.  Note that this multiplication is not
commutative; if A and B are matrices, AB is not necessarily the same as BA.
One may even exist without the other existing.

Matrices are multiplied like this:

If A is a mXn matrix like
a[1,1]   a[1,2] ... a[1,n]
a[2,1]   a[2,2] ... a[2,n]
.
.
a[m,1]   a[m,2] ... a[m,n]

(note the order: row, then column)

and B is a nXp matrix, equivalently represented (with b's)
Let C=AB; C is a mXp matrix, likewise with c's.
So for any i,j,
c[i,j] = a[i,1]*b[1,j] + a[i,2]*b[2,j] + ... + a[i,n]*b[n,j]
You dig?  If you have questions, write back with them.

A vector (like the coordinates (-60,-60,-60) you gave) can be represented
by a matrix; a 3-vector can either be a 3x1 matrix or a 1x3 one.

When you multiply a matrix and a vector (of compatible sizes, on the
appropriate side) you get another vector, which is the first vector after
being  transformed in various ways.

--------------END The dirt on matrices --------

So, for a 3-vector, you could multiply it by a 3x3 matrix.  This would
allow you to rotate it, scale it, and do some other less useful operations.
(In math-ese, you can do any linear transformation, but nothing that isn't
a linear transformation) Unfortunately, rotation around any point other
than the origin is not a linear transformation (it's an affine
transformation).  If you just want to rotate around the origin, then fine.
If not...

I've seen two ways to get more versatility.  Both ways add two main
capabilities - they let you rotate around arbitrary centers, and they
let you translate (move) stuff.

1. You can work with 4-dimensional vectors and 4x4 matrices.  What you do
is you set the last entry of the vector to 1.  What this does is that the
last row (or column, depending on which side you multiply on) of the matrix
multiplies with that 1, and essentially adds values to each component.
(This provides translation; rotation around an arbitrary center is really
rotation around the origin plus translation)

2. You can keep to sane old 3-d vectors and 3x3 matrices, and also keep
a 3-d vector to add to the input vector.

Both ways are basically equivalent, and should be chosen based
on implementation ease.  Since you're always using the same size vectors,
you can just write the equations for the transformation, i.e.

newx = oldx * a + oldy * b + oldz * c + movex
newy = oldx * d + oldy * e + oldz * f + movey
newz = oldx * g + oldy * h + oldz * i + movez

Practically, the above is equivalent to the other methods.  Its major
drawback is that you don't get the real usefulness of matrices.  Using
various facts about matrices, you can make one transformation by combining
several, or check a transformation to check to see if it is orthogonal
(i.e. does not distort the object), etc.

OK.  So now you're asking, what kind of matrices can be used for rotation.
Not all matrices work.  So here are some examples:

1 0
0 1    -- take a 2d vector and don't change it.  This is called
the identity matrix of rank 2.
1 0 0
0 1 0
0 0 1 -- ditto for 3d vectors; it's the identity matrix of rank 3

2 0
0 2   -- don't rotate at all, but double the size of a 2d vector

0.707 -0.707
0.707 0.707	-- rotate 45 degrees around the origin, for a 2d
vector (0.707 = sin(45) = cos(45))

0.707      0.707        0
-0.707      0.707        0
0          0            1
^^^-- rotate a 3d vector 45 degrees about the Z axis.  Note that
the 3rd row&column (the part corresponding with Z) is copied
from the identity matrix of rank 3 (see above).  If you wanted
the X axis, you'd do the same for the 1st row and column.

In general, to get the 2x2 matrix to rotate by an angle of 't' you do
cos(t)   sin(t)
-sin(t)   cos(t)

(You can swap the signs on the sines to get the opposite rotation; I never
bother to intuitively figure out which is which.)

The 3d matrix is, to rotate around the X, Y or Z axes, you make a 3x3
matrix with the appropriate row & column copied from the identity matrix,
and then take the remaining four entries from the 2x2 rotation matrix.
(You want to put them in order of course; the key is make sure the cosine
terms are all on a diagonal going from the upper left corner of the matrix
([1,1]) to the lower right corner ([3,3], or [n,n] in general).

Hope I've helped...

-Doctor Jeremy,  The Geometry Forum

```
Associated Topics:
College Algorithms
College Linear Algebra

Search the Dr. Math Library:

 Find items containing (put spaces between keywords):   Click only once for faster results: [ Choose "whole words" when searching for a word like age.] all keywords, in any order at least one, that exact phrase parts of words whole words

Submit your own question to Dr. Math
Math Forum Home || Math Library || Quick Reference || Math Forum Search