class Vector3D{ float x,y,z; //----------- Vector3D(float x,float y, float z){ this.x = x; this.y = y; this.z = z; } //----------- void apply(Transform t){ float[] vec = new float[]{x,y,z,1}; float[] newVec = new float[]{0,0,0,0}; for(int i=0 ; i<4 ; i++) for(int k=0 ; k<4 ; k++) newVec[i] += vec[k] * t.matrix[i][k]; x = newVec[0]; y = newVec[1]; z = newVec[2]; } //----------- float cosine(Vector3D vec){ float d = mag(x,y,z) * mag(vec.x,vec.y,vec.z); return (x*vec.x + y*vec.y + z*vec.z) / d; } } //===================================================== //===================================================== class Transform{ float[][] matrix; //----------- Transform(){ matrix = new float[4][4]; for(int i=0 ; i<4 ; i++) matrix[i][i] = 1; }//----------- Transform(int zero){ matrix = new float[4][4]; } //----------- void echo(){ for(int i=0 ; i<4 ; i++) for(int j=0 ; j<4 ; j++) print( matrix[i][j]+", " ); println(""); } //----------- void apply(Transform trans){ float[][] newMatrix = new float[4][4]; for(int i=0 ; i<4 ; i++) for(int j=0 ; j<4 ; j++) for(int k=0 ; k<4 ; k++) newMatrix[i][j] += this.matrix[j][k] * trans.matrix[i][k]; this.matrix = newMatrix; } //----------- void scale(float s){ for(int i=0 ; i<3 ; i++) for(int j=0 ; j<3 ; j++) matrix[i][j] *= s; } //----------- void translate(float x, float y, float z){ Transform foo = new Transform(); foo.matrix[0][3] = x; foo.matrix[1][3] = y; foo.matrix[2][3] = z; this.apply( foo ); } //----------- void rotate(float x, float y, float z){ Transform foo; foo = new Transform(); foo.matrix[1][1] = cos(x); foo.matrix[2][1] = -sin(x); foo.matrix[1][2] = sin(x); foo.matrix[2][2] = cos(x); this.apply( foo ); foo = new Transform(); foo.matrix[0][0] = cos(y); foo.matrix[2][0] = sin(y); foo.matrix[0][2] = -sin(y); foo.matrix[2][2] = cos(y); this.apply( foo ); foo = new Transform(); foo.matrix[0][0] = cos(z); foo.matrix[1][0] = -sin(z); foo.matrix[0][1] = sin(z); foo.matrix[1][1] = cos(z); this.apply( foo ); } }