public class Matrix3D { double matrix[][] = new double[4][4]; static Matrix3D temp = new Matrix3D (); static Matrix3D temptwo = new Matrix3D (); public void set(int i, int j, double value) { matrix[i][j] = value; } public double get(int i, int j) { return matrix[i][j]; } 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)); } public void identity () { for (int i = 0 ; i < 4 ; i++) for (int j = 0 ; j < 4 ; j++) matrix[i][j] = 0; matrix[0][0] = 1; matrix[1][1] = 1; matrix[2][2] = 1; matrix[3][3] = 1; } public void multiply (Matrix3D B) { for (int i = 0 ; i < 4 ; i++) for (int j = 0 ; j < 4 ; j++) temptwo.set(i,j,matrix[i][j]); 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 += temptwo.get(i,k) * B.get(k,j); matrix[i][j] = sum; } } public void translationMatrix(double a, double b, double c) { identity(); matrix[0][3] = a; matrix[1][3] = b; matrix[2][3] = c; } public void scaleMatrix(double a, double b, double c) { identity(); matrix[0][0] = a; matrix[1][1] = b; matrix[2][2] = c; } public void rotationMatrix(int a, int b, double theta) { identity(); matrix[b][a] = Math.sin(theta); matrix[a][b] = -matrix[b][a]; matrix[a][a] = Math.cos(theta); matrix[b][b] = matrix[a][a]; } public void translate(double x, double y, double z) { temp.translationMatrix(x,y,z); multiply(temp); } public void rotateX(double theta) { temp.rotationMatrix(1,2,theta); multiply(temp); } public void rotateY(double theta) { temp.rotationMatrix(2,0,theta); multiply(temp); } public void rotateZ(double theta) { temp.rotationMatrix(0,1,theta); multiply(temp); } public void scale(double x, double y, double z) { temp.scaleMatrix(x,y,z); multiply(temp); } public void transform(double src[], double dst[]) { for (int i = 0 ; i < 3 ; i++) { dst[i] = matrix[i][0] * src[0] + matrix[i][1] * src[1] + matrix[i][2] * src[2] + matrix[i][3]; } dst[3]=src[3]; } }