Homework 2, due Thursday, Sep 23

This assignment is going to focus on three dimensional transformations, which can be described by 4×4 matrices. As I mentioned in class, you'll want to package this 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 what we discussed in class about linear transformations:

3D Transformation Matrices:

Here are the basic 3D matrix primitives:

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 multiplying the elements in the rows of the first matrix and the columns of the second matrix. You will find it convenient to implement a method that copies the contents of one matrix into another. Then 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;
You should also make methods:
   translate(a, b, c);
   rotateX(theta);
   rotateY(theta);
   rotateZ(theta);
   scale(a, b, c);
Each of these first creates the 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 a matrix transformation to a point. Here's how you might implement that:
   public void transform(double src[], double dst[]) {
      dst[0] = matrix[0][0] * src[0] + matrix[0][1] * src[1] + matrix[0][2] * src[2] + matrix[0][3];
      dst[1] = matrix[1][0] * src[0] + matrix[1][1] * src[1] + matrix[1][2] * src[2] + matrix[1][3];
      dst[2] = matrix[2][0] * src[0] + matrix[2][1] * src[1] + matrix[2][2] * src[2] + matrix[2][3];
   }

Your assignment for Thursday is to:

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

  2. Test your Matrix class by applying various combinations of translate, rotate and scale to the simple shape created in my simple HOUSE APPLET, and post your version to your class web page.