home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************
- ADOBE SYSTEMS INCORPORATED
- Copyright 2002 Adobe Systems Incorporated
- All Rights Reserved
-
- NOTICE: Adobe permits you to use, modify, and distribute this
- file in accordance with the terms of the Adobe license agreement
- accompanying it. If you have received this file from a source
- other than Adobe, then your use, modification, or distribution
- of it requires the prior written permission of Adobe.
- ***************************************************************/
- /***************************************************************
- Author: Mary Obelnicki
- ***************************************************************/
-
- /***************************************************************
-
- This includes provides a prototype constructor for a 2-dimensional
- or 3-dimensional vector object. The constructor takes 3 arguments:
- x,y,z. The third argument is an optional argument.
-
- Three methods are available for a constructed vector.
-
- Vector.copy()
- returns a copy of the original vector.
-
- Example:
- var a = new Vector(4,3,2);
- b = a.copy();
-
- Vector.normalize()
- normalizes the vector to a magnitude of 1.
-
- Example:
- var a = new Vector(4,3);
- a.normalize(); // a now contains (.8, .6)
-
- Vector.polarAngle()
- returns the angle theta of the vector if it was a polar vector.
- To get the coordinates of the polar vector, use Vector.polarAngle()
- and Vector.magnitude.
-
- Example:
- var a = new Vector(4,3)
- theta = a.polarAngle();
-
- Six methods compose the Vector object. These do not require a
- constructor, though most of the arguments used are vector objects.
-
- Vector.add(a,b)
- returns a new vector, which is the result of adding vectors <a>
- and <b>. Vectors <a> and <b> are unchanged.
-
- Vector.crossProduct(a,b)
- returns a new vector which is the result of <a> X <b>.
- Vectors <a> and <b> are unchanged.
-
- Vector.dotProduct(a,b)
- returns the dot product of vectors <a> and <b>, a scaler.
- Vectors <a> and <b> are unchanged.
-
- Vector.scalerMult(a,c)
- returns a new vector, which is the result of multiplying vector
- <a> by the scaler <c>. Vector <a> is unchanged.
-
- Vector.subtract(a,b)
- returns a new vector, which is the result of subtracting vector
- <b> from vector <a>. Vectors <a> and <b> are unchanged.
-
- Vector.zAxisRotate(a,rads)
- returns a new vector, which is the result of rotating vector <a>
- <rads> radians in the counter clockwise direction around the
- z-axis. Vector <a> is unchanged.
-
- Example:
-
- var a = new Vector(1,1,1);
- var b = new Vector(1,0,1);
- c = Vector.add(a,b); //c is a Vector (2,1,2)
-
- ***************************************************************/
-
- /***************************************************************
- DO NOT EDIT BELOW THIS LINE
- ***************************************************************/
-
-
- //creates a new Vector
- function Vector(x, y, z)
- {
- this.x = x;
- this.y = y;
- if(arguments.length == 3)
- this.z = z;
- else
- this.z = 0;
- this.magnitude = Math.sqrt(this.x*this.x + this.y*this.y +this.z*this.z);
- }
-
- new Vector(0, 0, 0);
-
- //returns a new vector, which is the result of subtracting vector <b> from
- //vector <a>. Vectors <a> and <b> are unchanged.
- Vector.subtract =
- function(a, b){
- return new Vector(a.x - b.x, a.y - b.y, a.z - b.z);
- };
-
- //returns a new vector, which is the result of adding vectors <a> and <b>.
- //Vectors <a> and <b> are unchanged.
- Vector.add =
- function(a, b)
- {
- return new Vector(a.x + b.x, a.y + b.y, a.z + b.z);
- };
-
- // returns a new vector, which is the result of multiplying vector <a>
- // by the scaler <c>. Vector <a> is unchanged.
- Vector.scalerMult =
- function(a, c)
- {
- return new Vector(c*a.x, c*a.y, c*a.z);
- };
-
- // returns a new vector, which is the result of rotating vector <a>
- // <rads> radians in the counter clockwise direction around the z axis
- // Vector <a> is unchanged.
- Vector.zAxisRotate =
- function(a, rads)
- {
- return new Vector(a.x*Math.cos(-rads) + a.y*Math.sin(-rads),
- a.y*Math.cos(-rads) - a.x*Math.sin(-rads));
- };
-
-
-
- // returns the dot product of vectors <a> and <b>, a scaler
- Vector.dotProduct =
- function(a, b)
- {
- return (a.x*b.x + a.y*b.y + a.z+b.z);
- }
-
- // returns a new vector which is the result of <a> X <b>
- Vector.crossProduct =
- function(a, b)
- {
- return new Vector(a.y*b.z-a.z*b.y,
- a.z*b.x-a.x*b.z,
- a.x*b.y-a.y*b.z);
- }
-
- Vector.prototype.copy =
- function()
- {
- return new Vector(this.x, this.y, this.z);
- };
-
- // normalizes the vector is the magnitude is one.
- Vector.prototype.normalize =
- function()
- {
- this.x = this.x/this.magnitude;
- this.y = this.y/this.magnitude;
- this.z = this.z/this.magnitude;
- this.magnitude = 1;
- };
-
- //returns the angle between the vector projected onto the x-y plane and the positive
- //x-axis. The angle is measured in radians. If the vector has no magnitude, the angle
- //defaults to 0.
- //This value is the angle theta in polar and spherical coordinates
- Vector.prototype.polarAngle =
- function()
- {
- var testX = this.x;
- var testY = this.y;
- var testMagnitude = this.magnitude;
- var pAngle;
- if(this.magnitude > 0)
- {
- if(this.y < 0)
- pAngle = (2*Math.PI - Math.acos(this.x/this.magnitude))*180/Math.PI;
- else
- pAngle = Math.acos(this.x/this.magnitude)*180/Math.PI;
- }
- else
- {
- pAngle = 0.0;
- }
- return pAngle;
- };
-
-
-
-
-
-
-
-
-
-
-
-
-
-