import java.lang.*; /** * Matrix represent and provides operations on 4 by 4 matrices. * * @author Anthony Steed * @version 1.0 */ public class Matrix { /* * Holds the actual array of values */ private double m[][]; /* * Create a new zero matrix */ public Matrix() { m = new double[4][4]; for (int i=0; i<4; i++) { for (int j=0; j<4; j++) { m[i][j] = 0.0; } } } /* * Create a new matrix by copying an exisiting matrix * @param the matrix to copy */ public Matrix(Matrix mm) { for (int i=0; i<4; i++) { for (int j=0; j<4; j++) { m[i][j] = mm.m[i][j]; } } } /* * Set this matrix to the identity matrix */ public void setToIdentity() { for(int i=0; i<4;++i) { for(int j=i;j<4;++j) { if (i==j) { m[i][j] = 1.0; } else { m[i][j] = m[j][i] = 0.0; } } } } /* * Set this matrix to product of two given matrices * @param m1 the matrix on the left of the product * @param m2 the matrix on the right of the product */ public void multiplyMatrix(Matrix m1, Matrix m2) { for(int i=0; i<4; ++i) { for(int j=0; j<4; ++j) { m[i][j] = 0.0; for(int k=0;k<4;++k) { m[i][j] += (m1.m[i][k]*m2.m[k][j]); } } } } /* * Set this matrix to a translation matrix where the translation is * given by a vector * @param v the translation */ public void setToTranslation(Vector v) { setToIdentity(); m[3][0] = v.X; m[3][1] = v.Y; m[3][2] = v.Z; } /* * Set this matrix to a scale matrix where the scale is * given by a vector * @param v the scale factors */ public void setToScaling(Vector v) { setToIdentity(); m[0][0] = v.X; m[1][1] = v.Y; m[2][2] = v.Z; } /* * Set this matrix to a rotation about the X axis by the given * factor specified in degrees. * @param a the rotation angle */ public void setToXRotation(double a) { double cs,sn; cs = Math.cos(a); sn = Math.sin(a); setToIdentity(); m[1][1] = cs; m[1][2] = sn; m[2][1] = -sn; m[2][2] = cs; } /* * Set this matrix to a rotation about the Y axis by the given * factor specified in degrees. * @param a the rotation angle */ public void setToYRotation(double a) { double cs,sn; cs = Math.cos(a); sn = Math.sin(a); setToIdentity(); m[0][0] = cs; m[0][2] = -sn; m[2][0] = sn; m[2][2] = cs; } /* * Set this matrix to a rotation about the Z axis by the given * factor specified in degrees. * @param a the rotation angle */ public void setToZRotation(double a) { double cs,sn; cs = Math.cos(a); sn = Math.sin(a); setToIdentity(); m[0][0] = cs; m[1][0] = -sn; m[0][1] = sn; m[1][1] = cs; } /* * Set an individual element in the matrix. * * Note that rows and columns are specified from zero, not one as is * commonly done in texts on matrices. * @param x the row * @param y the column * @param v the value */ public void setValue(int x, int y, double v) { // Note we could throw ArrayIndexOutOfBoundsException but we do // not because that would probably be a package error rather than // a user error. m[x][y] = v; } /* * Get individual element in the matrix. * * Note that rows and columns are specified from zero, not one as is * commonly done in texts on matrices. * @param x the row * @param y the column * @return the matrix element */ public double getValue(int x, int y) { // Note we could throw ArrayIndexOutOfBoundsException but we do // not because that would probably be a package error rather than // a user error. return m[x][y]; } }