/* * ----------------------------------------------------- * Class: Vector3DClass.as * ----------------------------------------------------- * Version: 1.0 * ----------------------------------------------------- * 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. Vector3D(vx,vy,vz) * - prototypes * Vector3D.prototype. * 2. toString() * 3. reset_components(vx,vy,vz) * 4. copy_components(V) * 5. increment_x(inc_x) * 6. increment_y(inc_y) * 7. increment_z(inc_z) * 8. incrememnt_components(inc_x,inc_y,inc_z) * 9. addition(V) * 10. return_addition(V) * 11. subtract(V) * 12. return_subtraction(V) * 13. dot_product(V) * 14. cross_product(V) * 15. norm() * 16. unit_vector() * 17. normalize() * ----------------------------------------------------- * Updates may be available at: * http://members.shaw.ca/flashmath101/as/ * ----------------------------------------------------- */ // check if this file has already been included if (!_global._VECTOR3DCLASS_AS) { // create the variable that keeps track of whether or not this file has been included _global._VECTOR3DCLASS_AS = true; // 1. constructor for a 3D vector datatype Vector3D = function(vx,vy,vz) { this.x = vx; this.y = vy; this.z = vz; }; // 2. make vector trace-able Vector3D.prototype.toString = function() { return ("Vector3D = ( "+this.x+" , "+this.y+" , "+this.z+" )"); }; // 3. resets the components of the vector Vector3D.prototype.reset_components = function(vx,vy,vz) { this.x = vx; this.y = vy; this.z = vz; }; // 4. sets the components of this vector equal to the components of V Vector3D.prototype.copy_components = function(V) { this.x = V.x; this.y = V.y; this.z = V.z; }; // 5. increments the x-component of the vector by 'inc_x' Vector3D.prototype.increment_x = function(inc_x) { this.x += inc_x; }; // 6. increments the y-component of the vector by 'inc_y' Vector3D.prototype.increment_y = function(inc_y) { this.y += inc_y; }; // 7. increments the z-component of the vector by 'inc_z' Vector3D.prototype.increment_z = function(inc_z) { this.z += inc_z; }; // 8. increments each of the components of the vector Vector3D.prototype.incrememnt_components = function(inc_x,inc_y,inc_z) { this.x += inc_x; this.y += inc_y; this.z += inc_z; }; // 9. adds the vector V to this vector Vector3D.prototype.addition = function(V) { this.x += V.x; this.y += V.y; this.z += V.z; }; // 10.returns the vector made from adding V to this vector Vector3D.prototype.return_addition = function(V) { return (new Vector3D ((this.x+V.x),(this.y+V.y),(this.z+V.z))); }; // 11. subtracts vector V from this vector Vector3D.prototype.subtract = function(V) { this.x -= V.x; this.y -= V.y; this.z -= V.z; }; // 12. returns the vector made from subtracting V from this vector Vector3D.prototype.return_subtraction = function(V) { return (new Vector3D ((this.x-V.x),(this.y-V.y),(this.z-V.z))); }; // 13. returns the dot product of this vector with V Vector3D.prototype.dot_product = function(V) { return ((this.x*V.x)+(this.y*V.y)+(this.z*V.z)); }; // 14. returns the cross product of this vector with V Vector3D.prototype.cross_product = function(V) { // cross product vector that will be returned cross = new Vector3D (0.0, 0.0, 0.0); // calculate the components of the cross product cross.x = (this.y * V.z) - (this.z * V.y); cross.y = (this.z * V.x) - (this.x * V.z); cross.z = (this.x * V.y) - (this.y * V.x); // return the cross product of the two vectors return (cross); }; // 15. returns the norm of this vector Vector3D.prototype.norm = function() { return (Math.sqrt((this.x*this.x)+(this.y*this.y)+(this.z*this.z))); }; // 16. returns the unit vector of this vector Vector3D.prototype.unit_vector = function() { // unit vector that will be returned var unit = new Vector3D(this.x,this.y,this.z); // norm of this vector var norm = this.norm(); // calculate the components of the unit vector unit.x /= norm; unit.y /= norm; unit.z /= norm; // return the unit vector return (unit); }; // 17. normalizes this vector Vector3D.prototype.normalize = function() { // norm of this vector var norm = this.norm(); // calculate the components of this vector this.x /= norm; this.y /= norm; this.z /= norm; }; } // closes the 'if' statement that checked if this file had already been included