Advertisement
Delta

Vec.js

Mar 23rd, 2011
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.MathHelper = {};
  2.  
  3. MathHelper.PiOver180 = window.Math.PI / 180;
  4. MathHelper.PiOver2 = window.Math.PI / 2;
  5. MathHelper.PiOver4 = window.Math.PI / 4;
  6. MathHelper.TwoPi = window.Math.PI * 2;
  7.  
  8. MathHelper.ToRadians = function(Degrees)
  9. {
  10.     return this.PiOver180 * Degrees;
  11. };
  12.  
  13. MathHelper.ToDegrees = function(Radians)
  14. {
  15.     return 180 / window.Math.PI * Radians;
  16. };
  17.  
  18. function VectorObject()
  19. {
  20.  
  21.     this.LengthSquared = function()
  22.     {
  23.         return this.X * this.X + this.Y * this.Y + (this.Z ? this.Z * this.Z : 0);
  24.     };
  25.  
  26.     this.Length = function()
  27.     {
  28.         return Math.sqrt(this.LengthSquared());
  29.     };
  30.  
  31.     this.Normalize = function()
  32.     {
  33.         var Length = this.Length();
  34.  
  35.         this.X != 0 ? this.X /= Length : null;
  36.         this.Y != 0 ? this.Y /= Length : null;
  37.         this.Z != undefined ? this.Z != 0 ? this.Z /= Length : null : null;
  38.  
  39.         return this;
  40.     };
  41.  
  42.     this.CreateVector = function(V)
  43.     {
  44.         if(V.length == 1)
  45.             return V[0];
  46.  
  47.         return V[2] == undefined ? new Vector2(V[0], V[1]) : new Vector3(V[0], V[1], V[2]);
  48.     };
  49.  
  50.     this.Dot = function()
  51.     {
  52.         var B = this.CreateVector(arguments);
  53.  
  54.         return this.X * B.X + this.Y * B.Y + (this.Z ? this.Z * B.Z : 0);
  55.     };
  56.    
  57.     this.Cross = function()
  58.     {
  59.         var B = this.CreateVector(arguments);
  60.        
  61.         var C = new Vector3(this.Y * B.Z - this.Z * B.Y, this.Z * B.X - this.X * B.Z, this.X * B.Y - this.Y * B.X);
  62.         this.X = C.X;
  63.         this.Y = C.Y;
  64.         this.Z = C.Z;
  65.        
  66.         return this;
  67.     };
  68.  
  69.     this.Angle = function()
  70.     {
  71.         if(arguments.length == 0)
  72.             return Math.atan(this.Y / this.X);
  73.  
  74.         var B = this.CreateVector(arguments);
  75.  
  76.         return Math.acos(this.Dot(B) / this.Length() / B.Length());
  77.     };
  78.  
  79.     this.Add = function()
  80.     {
  81.         var B = this.CreateVector(arguments);
  82.  
  83.         this.X += B.X;
  84.         this.Y += B.Y;
  85.         B.Z != undefined ? this.Z += B.Z : null;
  86.  
  87.         return this;
  88.     };
  89.  
  90.     this.Subtract = function()
  91.     {
  92.         var B = this.CreateVector(arguments);
  93.  
  94.         this.X -= B.X;
  95.         this.Y -= B.Y;
  96.         B.Z != undefined ? this.Z -= B.Z : null;
  97.  
  98.         return this;
  99.     };
  100.    
  101.     this.Scale = function(S)
  102.     {
  103.         this.X *= S;
  104.         this.Y *= S;
  105.         this.Z != undefined ? this.Z *= S : null;
  106.        
  107.         return this;
  108.     };
  109.  
  110.     this.Negate = function()
  111.     {
  112.         this.X *= -1;
  113.         this.Y *= -1;
  114.         this.Z != undefined ? this.Z *= -1 : null;
  115.  
  116.         return this;
  117.     };
  118.  
  119.     this.Clone = function()
  120.     {
  121.         return this.Z == undefined ? new Vector2(this.X, this.Y) : new Vector3(this.X, this.Y, this.Z);
  122.     };
  123.  
  124.     this.toString = function(Base)
  125.     {
  126.         var out = [this.X.toString(Base), this.Y.toString(Base)];
  127.         this.Z != undefined ? out.push(this.Z.toString(Base)) : null;
  128.         return out.join(',');
  129.     };
  130.    
  131.     this.ToArray = function()
  132.     {
  133.         var out = [this.X, this.Y];
  134.         this.Z != undefined ? out.push(this.Z) : null;
  135.        
  136.         return out;
  137.     };
  138.    
  139.     this.Equals = function()
  140.     {
  141.         var B = this.CreateVector(arguments);
  142.        
  143.         if((this.Z == undefined && B.Z != undefined) || (B.Z == undefined && this.Z != undefined))
  144.             return false;
  145.  
  146.         return this.X != B.X || this.Y != B.Y || (this.Z != undefined && this.Z != B.Z) ? false : true;
  147.     };
  148.    
  149.     this.IsNull = function()
  150.     {
  151.         return this.X == 0 && this.Y == 0 && (this.Z != undefined ? this.Z == 0 : true) ? true : false;
  152.     };
  153. }
  154.  
  155. function Vector2(X, Y)
  156. {
  157.     if(arguments.length == 1 && arguments[0].IsVector)
  158.     {
  159.         Vector2.call(this, arguments[0].X, arguments[0].Y);
  160.         return;
  161.     }
  162.  
  163.     VectorObject.call(this);
  164.  
  165.     this.X = X ? X : 0;
  166.     this.Y = Y ? Y : 0;
  167.    
  168.     this.IsVector = true;
  169. }
  170.  
  171. function Vector3(X, Y, Z)
  172. {
  173.     if(arguments.length == 1 && arguments[0].IsVector)
  174.     {
  175.         Vector3.call(this, arguments[0].X, arguments[0].Y, arguments[0].Z != undefined ? arguments[0].Z : 0);
  176.         return;
  177.     }
  178.  
  179.     VectorObject.call(this);
  180.  
  181.     this.X = X ? X : 0;
  182.     this.Y = Y ? Y : 0;
  183.     this.Z = Z ? Z : 0;
  184.    
  185.     this.IsVector = true;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement