How to rotate a vector using a rotation matrix One of the most useful operations in computer graphics is the rotation of a vector using a rotation matrix. I want to introduce the underlying idea of the rotation matrix as it is based on the trig functions sine and cosine. Let's begin by reviewing the dot product of two vectors. < A, B > < C, D> = AC + BD So, remember that the dot product of two vectors is a scalar number. Keep this in mind as we work through the following matrix ideas. A matrix is a number of colums and rows of numbers that might look like either of these: The first matrix above is called a 2 by 2 matrix because it has 2 rows and 2 columns. The second is a 3 by 3 matrix because it has 3 rows and 3 columns. Matrices can be rectangular in shape, for example 4 rows by 3 columns or 5 rows by 8 columns, but for our purposes, all the matrices we will use will be square, with the same number of rows as columns. Matrices with 2 rows and 2 columns are called 2 by 2 matrices, and matrices with 3 rows and 3 columns are 3 by 3 matrices... etc. Vectors can be multiplied by matrices. The rules for multiplication are very much like the rules for the dot product of two vectors. Take this example. Suppose we want to multiply the following vector by the matrix:
To multiply a vector by a matrix, begin by (dot product) multiplying the vector by the first column of the matrix as if that column were a vector. ie do it as if you were doing this: and that 16 becomes the x component of the resulting vector: Then multiply the vector by the second column of the matrix to get the y component of the answer.
It's that simple. Multiply a vector by a matrix by multiplying the vector by each column of that matrix, one at a time, and you will get a vector for an answer. Now, how does that relate to trigonometry? One more small idea before we get there. There is a matrix that acts as an identity matrix in the same way that the number 1 acts on scalar numbers. If you multiply any number by 1, you will get that number. For example 300 X 1 = 300 The 2 by 2 identity matrix looks like this: Suppose we multiply a vector by this matrix using the same rules as I described above. This is what it would look like: When you multiply <4, 6> by the first column, the 4 is multiplied by 1 but the 6 is multiplied by 0, so all that is left is the 4. When you multiply the vector by the second column, the 4 is multiplied by 0 but the 6 is multiplied by 1. So all you get is 6. If you think about it, it makes perfect sense. ok, now let's talk trig There is a special matrix that you can multiply by a vector to make the
vector rotate around the origin. Essentially, this matrix changes the direction of the vector by adding or subtracting an angle to its angle. This is what this matrix looks like when it is used on a vector in the x y plane: A is the angle you want to add to the vector's current angle. Here's an example of how it works using numbers. Suppose you wanted to rotate the vector < 3,4> and angle of 50 degrees CCW (counter clockwise). Here is the initial set-up: Now you multiply the vector by the matrix one column at a time to get: And when you finish all the arithmetic you get the new x, y componenents that are the rotated vector. < -1.1358, 4.8693 >
Now, what does that look like on a graph? Finally, let me show you why this rotation matrix works. It is based on the sum and difference identities for cosines and sines. Suppose, instead of thinking of the vector in the <x, y > component form, we think of it in the < M cos A, M sin A > form. In this form we have a triangle from the unit circle with and angle of A and sides cosa, sina. But we have multiplied the radius (1 unit) by M which makes a triangle with sides M = hypotenuse, McosA = x side, MsinA = y side. The component form of this vector is <McosA,MsinA> and we can factor out the M to get M < cosa, sina >. Now suppose we want to rotate this
vector angle B using the rotation matrix: This shows that multiplying the rotation matrix with a vector results in the vector having new direction that is the sum of the original direction A and the added angle B. Final Comments In conclusion, in a fly by, I want to show you what the homogeneous version of the xy vector and rotation matrix looks like.
Note that the vector now has a new number 1 added to the x and y in the vector and that the rotation matrix is now 3 by 3. Although the rotation matrix as I demonstrated it above will correctly rotate an xy vector, it became necessary to make these changes shown to accommodate the translation matrix ( another matrix that moves points rather than rotates them) The translation matrix requires 3 rows and columns, and that led to the need to make the rotation matrix agree with it so they can work in concert to rotate and translate. The homogeneous '1' does not affect the outcome from rotations. It is like a silent place holder that does nothing except ensure that all of the matrix operations (rotation, translation, scaling) can work together.