Homework 2, due Wednesday, Sept 17.

When you have finished the assignment below, post the working applet onto the web. Post the source code too. Make sure that you maintain a folder that contains a web page referring to all the assignments for this class, not just for individual assignments. I'd like to link to your folder from the class page.

This assignment is going to focus on two dimensional matrices, which can be stored in Java as 3×3 arrays:

   double matrix[][] = new double[3][3];
For this assignment, I'd like you to make a cool and interesting and creative picture or animation using instances of shapes (ie: show the same shapes translated, rotated, and scaled in different ways). You'll find it particularly useful to use the java.awt drawLine, drawPolygon and fillPolygon methods. Methods to draw rectangles and ovals won't work so well once you start rotating and scaling things.

You'll need to implement the following primitive operations:

Identity:
100
010
001
translationMatrix(a,b):
10a
01b
001
rotationMatrix(θ):
cos(theta)-sin(theta)0
sin(theta)cos(theta)0
001
scaleMatrix(a,b):
a00
0b0
001
You might want to implement all these as static methods in a class called Matrix2D. The reason for using static methods is that you don't need to keep any data around as object instances of the class. For example, your method to build a translation matrix might look like this:
public class Matrix2D {

...

   public static void translationMatrix(double matrix[][], double a, double b) {
      identity(matrix);
      matrix[0][2] = a;
      matrix[1][2] = b;
   }
}
You'll also want to implement a method to do matrix multiplication. Remember that you do this by doing nine dot (inner) products. To do this, it's convenient to implement a method that copies the contents of one matrix into another. Then to multiply two matrices A and B:
   copy(A,temp); // FIRST COPY TO A TEMPORARY 3×3 MATRIX
   for (int i = 0 ; i < 3 ; i++)
   for (int j = 0 ; j < 3 ; j++)
      A[i][j] = temp[i][0]*B[0][j] + temp[i][1]*B[1][j] + temp[i][2]*B[2][j];
For convenience, you'll probably want to make methods
   translate(matrix, a, b);
   rotate(matrix, theta);
   scale(matrix, a, b);
which first create the translation, rotation or scale matrix, respectively, and then do a matrix multiply. For example:
   static double tempMatrix[][] = new double[3][3];
   ...
   public static void translate(double matrix[][], double a, double b) {
      translationMatrix(tempMatrix,a,b); // CREATES A TRANSLATION MATRIX
      multiply(matrix, tempMatrix);      // MODIFIES THE FIRST ARGUMENT IN PLACE
   }
Finally, you'll want to implement a method that applies a matrix transformation to a point. Here's how I might implement that:
   public static void transform(double src[], double dst[], double matrix[][]) {
      dst[0] = matrix[0][0] * src[0] + matrix[0][1] * src[1] + matrix[0][2];
      dst[1] = matrix[1][0] * src[0] + matrix[1][1] * src[1] + matrix[1][2];
   }
Now that you have all the tools, you can make pictures that have instances. For example, if you define a square shape as:
   double square[][] = {{-1,-1},{1,-1},{1,1},{-1,1}};
then you can make all kinds of instances of it by making transformation matrices and then using them to transform the square. You might find it convenient to keep around a temporary array for the transformed points:
   double xy[][] = new double[100][2];

   ...

   // TRANSFORM ALL THE POINTS
   int len = square.length;
   for (int i = 0 ; i < len ; i++)
      transform(square[i], xy[i], matrix);
as well as temporary arrays for the X and Y coordinates of polygons you want to draw:
   int X[] = new int[100];
   int Y[] = new int[100];
   // DO THE VIEWPORT TRANSFORM AND DRAW A POLYGON
   for (int i = 0 ; i < len ; i++) {
      X[i] = viewportX(xy[i]);
      Y[i] = viewportY(xy[i]);
   }
   g.drawPolygon(X[i],Y[i],len);
As we discussed in class, the ViewPort transformation is something like:
   screenX =  width * x + width/2;
   screenY = -width * y + height/2;
so that the point at the origin (0,0) will display in the middle of the screen.