/* * ------------------------------------------------------ * Matrix3x3Class.as * ------------------------------------------------------ * Version: 1.0 * ------------------------------------------------------ * Dependency: Vector3DClass.as * ------------------------------------------------------ * Latest update: August 1, 2003 * ------------------------------------------------------ * Developer: Richard Wright [wisolutions2002@shaw.ca] * ------------------------------------------------------ * Copyright: (c) 2003, Brandon Williams [brandon@plotdev.com] * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of this software nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ------------------------------------------------------ * Functions: * - class * 1. Matrix3x3(vx,vy,vz) * - prototypes * Matrix3x3.prototype. * 2. toString() * 3. scalarMultiplication(s) * 4. vectorMultiplication(V) * 5. matrixMultiplication(M) * 6. transpose() * 7. loadIdentity() * 8. loadRotationX(sine,cosine) * 9. loadRotationY(sine,cosine) * 10. loadRotationZ(sine,cosine) * 11. loadRotationAxis(A,sine,cosine) * ------------------------------------------------------ * Updates may be available at: * http://members.shaw.ca/flashmath101/as/ * ------------------------------------------------------ */ // check if this file has already been included if (!_global._MATRIX3X3CLASS_AS) { // create the variable that keeps track of whether or not this file has been included _global._MATRIX3X3CLASS_AS = true; // dependency #include "Vector3DClass.as" // 1. constructor for a 3x3 Matrix datatype Matrix3x3 = function() { // elements of the matrix this.e = new Array(3); // initialize the elements to the identity matrix this.e[0] = new Array(1.0,0.0,0.0); this.e[1] = new Array(0.0,1.0,0.0); this.e[2] = new Array(0.0,0.0,1.0); }; // 2. make matrix trace-able Matrix3x3.prototype.toString = function() { return ("Matrix3x3 : " + '\n' + "[ " + this.e[0][0]+" , "+this.e[0][1]+" , "+this.e[0][2]+" ]\n"+ "[ " + this.e[1][0]+" , "+this.e[1][1]+" , "+this.e[1][2]+" ]\n"+ "[ " + this.e[2][0]+" , "+this.e[2][1]+" , "+this.e[2][2]+" ]" ); }; // 3. returns the matrix made from multiplying this matrix with the scalar 's' Matrix3x3.prototype.scalarMultiplication = function(s) { // matrix that will be returned var product = new Matrix3x3(); // calculate the elements of the product matrix product.e[0][0] = this.e[0][0]*s; product.e[0][1] = this.e[0][1]*s; product.e[0][2] = this.e[0][2]*s; product.e[1][0] = this.e[1][0]*s; product.e[1][1] = this.e[1][1]*s; product.e[1][2] = this.e[1][2]*s; product.e[2][0] = this.e[2][0]*s; product.e[2][1] = this.e[2][1]*s; product.e[2][2] = this.e[2][2]*s; // return the scaled matrix return (product); }; // 4. returns the vector made from multiplying this matrix with V Matrix3x3.prototype.vectorMultiplication = function(V) { // vector that will be returned var product = new Vector3D(0.0,0.0,0.0); // calculate the components of the vector product.x = V.x*this.e[0][0]+V.y*this.e[0][1]+V.z*this.e[0][2]; product.y = V.x*this.e[1][0]+V.y*this.e[1][1]+V.z*this.e[1][2]; product.z = V.x*this.e[2][0]+V.y*this.e[2][1]+V.z*this.e[2][2]; // return the product vector return (product); }; // 5. returns the matrix made from multiplying this matrix with M Matrix3x3.prototype.matrixMultiplication = function(M) { // matrix that will be returned var product = new Matrix3x3(); // calculate the elements of the product matrix product.e[0][0] = M.e[0][0]*this.e[0][0]+M.e[1][0]*this.e[0][1]+M.e[2][0]*this.e[0][2]; product.e[0][1] = M.e[0][1]*this.e[0][0]+M.e[1][1]*this.e[0][1]+M.e[2][1]*this.e[0][2]; product.e[0][2] = M.e[0][2]*this.e[0][0]+M.e[1][2]*this.e[0][1]+M.e[2][2]*this.e[0][2]; product.e[0][0] = M.e[0][0]*this.e[1][0]+M.e[1][0]*this.e[1][1]+M.e[2][0]*this.e[1][2]; product.e[0][1] = M.e[0][1]*this.e[1][0]+M.e[1][1]*this.e[1][1]+M.e[2][1]*this.e[1][2]; product.e[0][2] = M.e[0][2]*this.e[1][0]+M.e[1][2]*this.e[1][1]+M.e[2][2]*this.e[1][2]; product.e[0][0] = M.e[0][0]*this.e[2][0]+M.e[1][0]*this.e[2][1]+M.e[2][0]*this.e[2][2]; product.e[0][1] = M.e[0][1]*this.e[2][0]+M.e[1][1]*this.e[2][1]+M.e[2][1]*this.e[2][2]; product.e[0][2] = M.e[0][2]*this.e[2][0]+M.e[1][2]*this.e[2][1]+M.e[2][2]*this.e[2][2]; // return the product matrix return (product); }; // 6. returns the transpose matrix of this matrix Matrix3x3.prototype.transpose = function() { // transposed matrix that will be returned var transpose = new Matrix3x3(); // set the elements of the transposed matrix transpose.e[0][0] = this.e[0][0]; transpose.e[0][1] = this.e[1][0]; transpose.e[0][2] = this.e[2][0]; transpose.e[1][0] = this.e[0][1]; transpose.e[1][1] = this.e[1][1]; transpose.e[1][2] = this.e[2][1]; transpose.e[2][0] = this.e[0][2]; transpose.e[2][1] = this.e[1][2]; transpose.e[2][2] = this.e[2][2]; // return the transposed matrix return (transpose); }; // 7. loads the identity matrix into this matrix Matrix3x3.prototype.loadIdentity = function() { // set the elements of the matrix for an identity matrix this.e[0][0] = 1.0; this.e[0][1] = 0.0; this.e[0][2] = 0.0; this.e[1][0] = 0.0; this.e[1][1] = 1.0; this.e[1][2] = 0.0; this.e[2][0] = 0.0; this.e[2][1] = 0.0; this.e[2][2] = 1.0; }; // 8. loads a rotation matrix for a rotation around the x-axis given the sine and cosine of the rotation angle Matrix3x3.prototype.loadRotationX = function(sine,cosine) { // set the elements of the rotation matrix this.e[0][0] = 1.0; this.e[0][1] = 0.0; this.e[0][2] = 0.0; this.e[1][0] = 0.0; this.e[1][1] = cosine; this.e[1][2] = -sine; this.e[2][0] = 0.0; this.e[2][1] = sine; this.e[2][2] = cosine; }; // 9. loads a rotation matrix for a rotation around the y-axis given the sine and cosine of the rotation angle Matrix3x3.prototype.loadRotationY = function(sine,cosine) { // set the elements of the rotation matrix this.e[0][0] = cosine; this.e[0][1] = 0.0; this.e[0][2] = sine; this.e[1][0] = 0.0; this.e[1][1] = 1.0; this.e[1][2] = 0.0; this.e[2][0] = -sine; this.e[2][1] = 0.0; this.e[2][2] = cosine; }; // 10. loads a rotation matrix for a rotation around the z-axis given the sine and cosine of the rotation angle Matrix3x3.prototype.loadRotationZ = function(sine,cosine) { // set the elements of the rotation matrix this.e[0][0] = cosine; this.e[0][1] = -sine; this.e[0][2] = 0.0; this.e[1][0] = sine; this.e[1][1] = cosine; this.e[1][2] = 0.0; this.e[2][0] = 0.0; this.e[2][1] = 0.0; this.e[2][2] = 1.0; }; // 11. loads a rotation matrix about a unit vector given the sine and cosine of the rotation angle Matrix3x3.prototype.loadRotationAxis = function(A,sine,cosine) { var t = 1-cosine; // set the elements of the rotation matrix this.e[0][0] = t*A.x*A.x+cosine; this.e[0][1] = t*A.x*A.y-sine*A.z; this.e[0][2] = t*A.x*A.z+sine*A.y; this.e[1][0] = t*A.x*A.y+sine*A.z; this.e[1][1] = t*A.y*A.y+cosine; this.e[1][2] = t*A.y*A.z-sine*A.x; this.e[2][0] = t*A.x*A.z-sine*A.y; this.e[2][1] = t*A.y*A.z+sine*A.x; this.e[2][2] = t*A.z*A.z+cosine; }; } // closes the 'if' statement that checked if this file had already been included