Advertisement
Delta

Vec.js

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