Homework due Tuesday, Sep 18, 2007

In this assignment you are going to implement your basic building block routines for doing three dimensional linear transformations, which can be described by 4×4 matrices. You'll want to package your matrices up into a Java class that contains various access routines:

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

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


In the on-line notes I go over the form of the identity, translation, rotation, and scale matrices. You can use these to make primitive methods for your Matrix3D class:

    translationMatrix(double x, double y, double z);
    xRotationMatrix(double theta);
    yRotationMatrix(double theta);
    zRotationMatrix(double theta);
    scaleMatrix(double x, double y, double z);

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));

Your method within class Matrix3D to build a translation matrix might be implemented like this:

public class Matrix3D {


   public void translationMatrix(double a, double b, double c) {
      data[0][3] = a;
      data[1][3] = b;
      data[2][3] = c;
Given that you have implemented primitive matrix routines to create an identity, translation, rotation, and scale, you need to build software that transforms one matrix by another, so that you can start combining transformations.

To do this, you'll need to implement a method to do matrix multiplication. As most of you already know, 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];

So you can implement matrix multiplication by:

   Matrix3D temp = new Matrix3D();


   void multiply(Matrix M) {
      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) * M.get(k,j);
         data[i][j] = sum;  // THEN COMPUTE NEW DATA VALUES FOR ME

Note that I use a method copy(Matrix3D src) in the code above. To implement matrix multiplication, 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);
   scale(a, b, c);
Each of these methods must first internally create a translation, rotation or scale matrix, respectively, and then it must do 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 ME 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();
Finally, you'll eventually want to use your matrices to transform triangle vertices. To do this, you need to implement 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] = data[i][0] * src[0] + data[i][1] * src[1] + data[i][2] * src[2] + data[i][3];

Your assignment is to:

  1. Implement a Matrix3D class with identity, translate, rotateX, rotateY, rotateZ, scale, and transform methods, as well as any support methods these may require.

  2. I've created a really simple applet to help you test your Matrix class. What you should do is replace the simple house 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 over time. Make a fun animation. :-)

    Post your result to your class web page.