/* Matrix3D.java * * Class that implements and contains a 4x4 Matrix * */ public class Matrix3D { double matrix[][] = new double[4][4]; public Matrix3D() { identity(); } /* transform(double[]) * returns array with 3 values representing the * src point transformed by the matrix */ public double[] transform(double src[]) { double dst[] = new double[3]; 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]; return dst; } /* multiply(Matrix3D) * Set matrix to the result of multiplying this.matrix by * the Matrix3D.matrix passed in */ public void multiply(Matrix3D a) { Matrix3D myCopy = new Matrix3D(); myCopy.copy(this); // 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 += myCopy.get(i, k) * a.get(k, j); matrix[i][j] = sum; } } /* translate (double,double,double) * Translate matrix by (a,b,c) */ public void translate(double a, double b, double c) { Matrix3D temp = new Matrix3D(); temp.translationMatrix(a, b, c); // CREATE A TRANSLATION MATRIX multiply(temp); // MULTIPLY BY THAT MATRIX } /* scale(double,double,double) * Scale matrix by (a, b, c) */ public void scale(double a, double b, double c) { Matrix3D temp = new Matrix3D(); temp.scaleMatrix(a, b, c); multiply(temp); } /* rotateX(double) * Rotate matrix by (theta) radians */ public void rotateX(double theta) { Matrix3D temp = new Matrix3D(); temp.rotateXMatrix(theta); multiply(temp); } /* rotateY(double) * Rotate matrix by (theta) radians */ public void rotateY(double theta) { Matrix3D temp = new Matrix3D(); temp.rotateYMatrix(theta); multiply(temp); } /* rotateZ(double) * Rotate matrix by (theta) radians */ public void rotateZ(double theta) { Matrix3D temp = new Matrix3D(); temp.rotateZMatrix(theta); multiply(temp); } /* prints the matrix - for error cheking */ public void printMatrix() { System.out.print("\n"); for (int i = 0; i < matrix.length; i++) { System.out.print("\n"); for (int j=0; j < matrix[i].length; j++) System.out.print("(" + matrix[i][j] + ")"); } System.out.print("\n"); } /* rotateXMatrix(double) * Set matrix to rotate theta radians round x axis */ private void rotateXMatrix(double theta) { identity(); set(1, 1, Math.cos(theta)); set(1, 2, - (Math.sin(theta))); set(2, 1, Math.sin(theta)); set(2, 2, Math.cos(theta)); } /* rotateYMatrix(double) * Set matrix to rotate theta radians round Y axis */ private void rotateYMatrix(double theta) { identity(); set(0, 0, Math.cos(theta)); set(0, 2, Math.sin(theta)); set(2, 0, - (Math.sin(theta))); set(2, 2, Math.cos(theta)); } /* rotateZMatrix(double) * Set matrix to rotate theta radians round Z axis */ private void rotateZMatrix(double theta) { identity(); set(0, 0, Math.cos(theta)); set(0, 1, - (Math.sin(theta))); set(1, 0, Math.sin(theta)); set(1, 1, Math.cos(theta)); } /* copy(Matrix3D) * Copies other matrix into this.matrix */ 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)); } /* identity() * sets matrix to the identity matrix */ public void identity() { // set each (i,j) to 0 // unless i==j, then set to 1 for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) set(i, j, (i == j) ? 1 : 0); } /* set(int,int,double) * Sets matrix[i][j] to value */ private void set(int i, int j, double value) { matrix[i][j] = value; } /* get(int,int) * Returns value of matrix[i][j] */ public double get(int i, int j) { return matrix[i][j]; } /* translationMatrix(double,double,double) * Sets matrix to translation that would * move x, y, z by a, b, c */ private void translationMatrix(double a, double b, double c) { identity(); set(0, 3, a); set(1, 3, b); set(2, 3, c); } /* scaleMatrix(double,double,double) * Sets matrix to scaling matrix to scale * x, y, z by a, b, c */ private void scaleMatrix(double a, double b, double c) { identity(); set(0, 0, a); set(1, 1, b); set(2, 2, c); } }