home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 July / com!online0702.iso / software / livemotion / DATA1.CAB / Include / Vector.js < prev   
Encoding:
JavaScript  |  2002-05-13  |  5.5 KB  |  206 lines

  1. /***************************************************************
  2. ADOBE SYSTEMS INCORPORATED 
  3. Copyright 2002 Adobe Systems Incorporated 
  4. All Rights Reserved 
  5.  
  6. NOTICE:  Adobe permits you to use, modify, and distribute this 
  7. file in accordance with the terms of the Adobe license agreement 
  8. accompanying it.  If you have received this file from a source 
  9. other than Adobe, then your use, modification, or distribution
  10. of it requires the prior written permission of Adobe. 
  11. ***************************************************************/
  12. /***************************************************************
  13. Author: Mary Obelnicki
  14. ***************************************************************/
  15.  
  16. /***************************************************************
  17.  
  18. This includes provides a prototype constructor for a 2-dimensional 
  19. or 3-dimensional vector object. The constructor takes 3 arguments:
  20. x,y,z.  The third argument is an optional argument.
  21.  
  22. Three methods are available for a constructed vector. 
  23.  
  24. Vector.copy()
  25.     returns a copy of the original vector.
  26.     
  27.     Example:
  28.     var a = new Vector(4,3,2);
  29.     b = a.copy();
  30.  
  31. Vector.normalize()
  32.     normalizes the vector to a magnitude of 1.
  33.     
  34.     Example:
  35.     var a = new Vector(4,3);
  36.     a.normalize();  // a now contains (.8, .6)
  37.  
  38. Vector.polarAngle()
  39.     returns the angle theta of the vector if it was a polar vector.
  40.     To get the coordinates of the polar vector, use Vector.polarAngle()
  41.     and Vector.magnitude.
  42.     
  43.     Example:
  44.     var a = new Vector(4,3)
  45.     theta = a.polarAngle();
  46.  
  47. Six methods compose the Vector object.  These do not require a
  48. constructor, though most of the arguments used are vector objects.
  49.  
  50. Vector.add(a,b)
  51.     returns a new vector, which is the result of adding vectors <a> 
  52.     and <b>.  Vectors <a> and <b> are unchanged. 
  53.  
  54. Vector.crossProduct(a,b)    
  55.     returns a new vector which is the result of <a> X <b>.
  56.     Vectors <a> and <b> are unchanged. 
  57.  
  58. Vector.dotProduct(a,b)
  59.     returns the dot product of vectors <a> and <b>, a scaler.
  60.     Vectors <a> and <b> are unchanged. 
  61.  
  62. Vector.scalerMult(a,c)
  63.     returns a new vector, which is the result of multiplying vector 
  64.     <a>    by the scaler <c>. Vector <a> is unchanged.
  65.  
  66. Vector.subtract(a,b)
  67.     returns a new vector, which is the result of subtracting vector 
  68.     <b> from vector <a>. Vectors <a> and <b> are unchanged.
  69.  
  70. Vector.zAxisRotate(a,rads)
  71.     returns a new vector, which is the result of rotating vector <a>
  72.     <rads> radians in the counter clockwise direction around the 
  73.     z-axis. Vector <a> is unchanged.
  74.  
  75. Example:
  76.  
  77. var a = new Vector(1,1,1);
  78. var b = new Vector(1,0,1);
  79. c = Vector.add(a,b);  //c is a Vector (2,1,2)
  80.  
  81. ***************************************************************/
  82.  
  83. /***************************************************************
  84. DO NOT EDIT BELOW THIS LINE
  85. ***************************************************************/
  86.  
  87.  
  88. //creates a new Vector
  89. function Vector(x, y, z)
  90. {
  91.     this.x = x; 
  92.     this.y = y; 
  93.     if(arguments.length == 3)
  94.     this.z = z; 
  95.     else
  96.     this.z = 0; 
  97.     this.magnitude = Math.sqrt(this.x*this.x + this.y*this.y +this.z*this.z);   
  98. }
  99.  
  100. new Vector(0, 0, 0); 
  101.  
  102. //returns a new vector, which is the result of subtracting vector <b> from
  103. //vector <a>. Vectors <a> and <b> are unchanged. 
  104. Vector.subtract =
  105.     function(a, b){
  106.     return new Vector(a.x - b.x, a.y - b.y, a.z - b.z); 
  107.     };
  108.  
  109. //returns a new vector, which is the result of adding vectors <a> and <b>. 
  110. //Vectors <a> and <b> are unchanged. 
  111. Vector.add = 
  112.     function(a, b)
  113.     {
  114.     return new Vector(a.x + b.x, a.y + b.y, a.z + b.z); 
  115.     };
  116.  
  117. // returns a new vector, which is the result of multiplying vector <a>
  118. // by the scaler <c>. Vector <a> is unchanged.
  119. Vector.scalerMult = 
  120.     function(a, c)
  121.     {
  122.     return new Vector(c*a.x, c*a.y, c*a.z); 
  123.     }; 
  124.  
  125. // returns a new vector, which is the result of rotating vector <a>
  126. // <rads> radians in the counter clockwise direction around the z axis 
  127. // Vector <a> is unchanged.
  128. Vector.zAxisRotate = 
  129.     function(a, rads)
  130.     {
  131.     return new Vector(a.x*Math.cos(-rads) + a.y*Math.sin(-rads), 
  132.               a.y*Math.cos(-rads) - a.x*Math.sin(-rads)); 
  133.     };
  134.  
  135.  
  136.  
  137. // returns the dot product of vectors <a> and <b>, a scaler
  138. Vector.dotProduct = 
  139.     function(a, b)
  140.     {
  141.     return (a.x*b.x + a.y*b.y + a.z+b.z); 
  142.     }
  143.  
  144. // returns a new vector which is the result of <a> X <b>
  145. Vector.crossProduct = 
  146.     function(a, b)
  147.     {
  148.     return new Vector(a.y*b.z-a.z*b.y, 
  149.               a.z*b.x-a.x*b.z, 
  150.               a.x*b.y-a.y*b.z);
  151.     }
  152.  
  153. Vector.prototype.copy = 
  154.     function()
  155.     {
  156.     return new Vector(this.x, this.y, this.z); 
  157.     };
  158.  
  159. // normalizes the vector is the magnitude is one. 
  160. Vector.prototype.normalize = 
  161.     function()
  162.     {
  163.     this.x = this.x/this.magnitude; 
  164.     this.y = this.y/this.magnitude; 
  165.     this.z = this.z/this.magnitude; 
  166.     this.magnitude = 1; 
  167.     };
  168.  
  169. //returns the angle between the vector projected onto the x-y plane and the positive
  170. //x-axis.  The angle is measured in radians. If the vector has no magnitude, the angle 
  171. //defaults to 0.
  172. //This value is the angle theta in polar and spherical coordinates
  173. Vector.prototype.polarAngle = 
  174.     function()
  175.     {
  176.     var testX = this.x; 
  177.     var testY = this.y; 
  178.     var testMagnitude = this.magnitude; 
  179.     var pAngle; 
  180.     if(this.magnitude > 0)
  181.     {
  182.         if(this.y < 0)
  183.         pAngle = (2*Math.PI - Math.acos(this.x/this.magnitude))*180/Math.PI; 
  184.         else
  185.         pAngle = Math.acos(this.x/this.magnitude)*180/Math.PI; 
  186.     }
  187.     else
  188.     {
  189.         pAngle = 0.0; 
  190.     }
  191.     return pAngle; 
  192.     };
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.