Homework 2, due Tuesay, Sep 28

As you might have gathered form that insanely whirlwind lecture in class, this assignment is going to focus on three dimensional transformations, which can be described by 4×4 matrices. Presumably you'll want to package your matrices up into a Java class with various access routines:

   public class Matrix3D {
      double matrix[][] = new double[4][4];

      public void set(int i, int j, double value) { matrix[i][j] = value; } // SET ONE VALUE
      public double get(int i, int j) { return matrix[i][j]; }              // GET ONE VALUE

      ...
   }
You can use these set and get routines in various ways. For example, you can copy one matrix into another by:
   public void copy(Matrix3D src) {
      for (int i = 0 ; i < 4 ; i++)
      for (int j = 0 ; j < 4 ; j++)
	 set(i,j, src.get(i,j));
   }

To review some of what we discussed in class about linear transformations:

3D Transformation Matrices:

Here are the basic 3D matrix primitives (not including perspective):

identity:
1000
0100
0010
0001
translationMatrix(a,b,c):
100a
010b
001c
0001
xRotationMatrix(θ):
1000
0cos(θ)-sin(θ)0
0sin(θ)cos(θ)0
0001
yRotationMatrix(θ):
cos(θ)0sin(θ)0
0100
-sin(θ)0cos(θ)0
0001
zRotationMatrix(θ):
cos(θ)-sin(θ)00
sin(θ)cos(θ)00
0010
0001
scaleMatrix(a,b,c):
a000
0b00
00c0
0001
For example, your method within class Matrix3D to build a translation matrix might look like this:
public class Matrix3D {

...

   public void translationMatrix(double a, double b, double c) {
      identity();
      matrix[0][3] = a;
      matrix[1][3] = b;
      matrix[2][3] = c;
   }
}
You'll also want to implement a method to do matrix multiplication. Remember that you do this by taking the inner product between the rows of the first matrix and the columns of the second matrix. As you recall from your linear algebra classes:, the inner product (or dot product) of two vectors is the sum of the products of their elements:
   dotProduct = 0;
   for (int i = 0 ; i < V1.length ; i++)
      dotProduct += V1[i] * V2[i];

As we reviewed in class, to multiply two matrices A and B:

   static Matrix3D temp = new Matrix3D();

   ...

   temp.copy(A); // FIRST COPY TO A TEMPORARY MATRIX
   for (int i = 0 ; i < 4 ; i++)
   for (int j = 0 ; j < 4 ; j++) {
      double sum = 0;
      for (int k = 0 ; k < 4 ; k++)
         sum += temp.get(i,k) * B.get(k,j);
      matrix[i][j] = sum;

Note that I use a method copy(Matrix3D src) in the code above. To implement matrix multiply, you will find it convenient to implement a matrix copy method.

You should also make the following methods to modify a matrix:

   translate(a, b, c);
   rotateX(theta);
   rotateY(theta);
   rotateZ(theta);
   scale(a, b, c);
Each of these first creates a translation, rotation or scale matrix, respectively, and then does a matrix multiply. For example:
   public void translate(double a, double b, double c) {
      temp.translationMatrix(a,b,c); // CREATE A TRANSLATION MATRIX
      multiply(temp);                // MULTIPLY BY THAT MATRIX
   }
This will give you everything you need to combine matrix operations. For example, here is a sequence of steps to first translate, then rotate, and then scale a matrix.
   Matrix3D m = new Matrix3D();
   ...
   m.identity();
   m.translate(1,0,0);
   m.rotateY(Math.PI/2);
   m.scale(.5,.5,.5);
Finally, you'll need a method that applies your matrix to transform a point src[], placing the result into dst[]. Here's how you might implement that:
   public void transform(double src[], double dst[]) {
      for (int i = 0 ; i < 3 ; i++)
         dst[i] = matrix[i][0] * src[0] + matrix[i][1] * src[1] + matrix[i][2] * src[2] + matrix[i][3];
   }

Your assignment is to:

  1. Implement translate, rotateX, rotateY, rotateZ, scale, and transform, as well as any support methods these may require.

  2. I've created really simple applet to help you test your Matrix class. What you should do is replace the simple shape that I use in my example with a more interesting, more wonderful, or just plain cooler shape of your own devising. Then use matrices to transform that shape. Make a fun animation. :-)

    Post your result to your class web page as usual.