Vector2.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. "use strict";
  2. /**
  3. * Class representing a 2D vector. A 2D vector is an ordered pair of numbers (labeled x and y), which can be used to represent points in space, directions, etc.
  4. *
  5. * @class
  6. */
  7. function Vector2(x, y)
  8. {
  9. this.x = x || 0;
  10. this.y = y || 0;
  11. }
  12. Vector2.prototype.set = function(x, y)
  13. {
  14. this.x = x;
  15. this.y = y;
  16. };
  17. Vector2.prototype.setScalar = function(scalar)
  18. {
  19. this.x = scalar;
  20. this.y = scalar;
  21. };
  22. Vector2.prototype.clone = function()
  23. {
  24. return new Vector2(this.x, this.y);
  25. };
  26. Vector2.prototype.copy = function(v)
  27. {
  28. this.x = v.x;
  29. this.y = v.y;
  30. };
  31. Vector2.prototype.add = function(v)
  32. {
  33. this.x += v.x;
  34. this.y += v.y;
  35. };
  36. Vector2.prototype.addScalar = function(s)
  37. {
  38. this.x += s;
  39. this.y += s;
  40. };
  41. Vector2.prototype.addVectors = function(a, b)
  42. {
  43. this.x = a.x + b.x;
  44. this.y = a.y + b.y;
  45. };
  46. Vector2.prototype.addScaledVector = function(v, s)
  47. {
  48. this.x += v.x * s;
  49. this.y += v.y * s;
  50. };
  51. Vector2.prototype.sub = function(v)
  52. {
  53. this.x -= v.x;
  54. this.y -= v.y;
  55. };
  56. Vector2.prototype.subScalar = function(s)
  57. {
  58. this.x -= s;
  59. this.y -= s;
  60. };
  61. Vector2.prototype.subVectors = function(a, b)
  62. {
  63. this.x = a.x - b.x;
  64. this.y = a.y - b.y;
  65. };
  66. Vector2.prototype.multiply = function(v)
  67. {
  68. this.x *= v.x;
  69. this.y *= v.y;
  70. };
  71. Vector2.prototype.multiplyScalar = function(scalar)
  72. {
  73. this.x *= scalar;
  74. this.y *= scalar;
  75. };
  76. Vector2.prototype.divide = function(v)
  77. {
  78. this.x /= v.x;
  79. this.y /= v.y;
  80. };
  81. Vector2.prototype.divideScalar = function(scalar)
  82. {
  83. return this.multiplyScalar(1 / scalar);
  84. };
  85. Vector2.prototype.min = function(v)
  86. {
  87. this.x = Math.min(this.x, v.x);
  88. this.y = Math.min(this.y, v.y);
  89. };
  90. Vector2.prototype.max = function(v)
  91. {
  92. this.x = Math.max(this.x, v.x);
  93. this.y = Math.max(this.y, v.y);
  94. };
  95. Vector2.prototype.clamp = function(min, max)
  96. {
  97. // assumes min < max, componentwise
  98. this.x = Math.max(min.x, Math.min(max.x, this.x));
  99. this.y = Math.max(min.y, Math.min(max.y, this.y));
  100. };
  101. Vector2.prototype.clampScalar = function(minVal, maxVal)
  102. {
  103. this.x = Math.max(minVal, Math.min(maxVal, this.x));
  104. this.y = Math.max(minVal, Math.min(maxVal, this.y));
  105. };
  106. Vector2.prototype.clampLength = function(min, max)
  107. {
  108. var length = this.length();
  109. return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
  110. };
  111. Vector2.prototype.floor = function()
  112. {
  113. this.x = Math.floor(this.x);
  114. this.y = Math.floor(this.y);
  115. };
  116. Vector2.prototype.ceil = function()
  117. {
  118. this.x = Math.ceil(this.x);
  119. this.y = Math.ceil(this.y);
  120. };
  121. Vector2.prototype.round = function()
  122. {
  123. this.x = Math.round(this.x);
  124. this.y = Math.round(this.y);
  125. };
  126. Vector2.prototype.roundToZero = function()
  127. {
  128. this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
  129. this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
  130. };
  131. Vector2.prototype.negate = function()
  132. {
  133. this.x = -this.x;
  134. this.y = -this.y;
  135. return this;
  136. };
  137. Vector2.prototype.dot = function(v)
  138. {
  139. return this.x * v.x + this.y * v.y;
  140. };
  141. Vector2.prototype.cross = function(v)
  142. {
  143. return this.x * v.y - this.y * v.x;
  144. };
  145. Vector2.prototype.lengthSq = function()
  146. {
  147. return this.x * this.x + this.y * this.y;
  148. };
  149. Vector2.prototype.length = function()
  150. {
  151. return Math.sqrt(this.x * this.x + this.y * this.y);
  152. };
  153. Vector2.prototype.manhattanLength = function()
  154. {
  155. return Math.abs(this.x) + Math.abs(this.y);
  156. };
  157. Vector2.prototype.normalize = function()
  158. {
  159. return this.divideScalar(this.length() || 1);
  160. };
  161. /**
  162. * Computes the angle in radians with respect to the positive x-axis
  163. */
  164. Vector2.prototype.angle = function()
  165. {
  166. var angle = Math.atan2(this.y, this.x);
  167. if(angle < 0)
  168. {
  169. angle += 2 * Math.PI;
  170. }
  171. return angle;
  172. };
  173. Vector2.prototype.distanceTo = function(v)
  174. {
  175. return Math.sqrt(this.distanceToSquared(v));
  176. };
  177. Vector2.prototype.distanceToSquared = function(v)
  178. {
  179. var dx = this.x - v.x;
  180. var dy = this.y - v.y;
  181. return dx * dx + dy * dy;
  182. };
  183. Vector2.prototype.manhattanDistanceTo = function(v)
  184. {
  185. return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
  186. };
  187. Vector2.prototype.setLength = function(length)
  188. {
  189. return this.normalize().multiplyScalar(length);
  190. };
  191. Vector2.prototype.lerp = function(v, alpha)
  192. {
  193. this.x += (v.x - this.x) * alpha;
  194. this.y += (v.y - this.y) * alpha;
  195. };
  196. Vector2.prototype.lerpVectors = function(v1, v2, alpha)
  197. {
  198. return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
  199. };
  200. Vector2.prototype.equals = function(v)
  201. {
  202. return ((v.x === this.x) && (v.y === this.y));
  203. };
  204. Vector2.prototype.fromArray = function(array, offset)
  205. {
  206. if(offset === undefined) offset = 0;
  207. this.x = array[offset];
  208. this.y = array[offset + 1];
  209. };
  210. Vector2.prototype.toArray = function(array, offset)
  211. {
  212. if(array === undefined) array = [];
  213. if(offset === undefined) offset = 0;
  214. array[offset] = this.x;
  215. array[offset + 1] = this.y;
  216. return array;
  217. };
  218. Vector2.prototype.rotateAround = function(center, angle)
  219. {
  220. var c = Math.cos(angle);
  221. var s = Math.sin(angle);
  222. var x = this.x - center.x;
  223. var y = this.y - center.y;
  224. this.x = x * c - y * s + center.x;
  225. this.y = x * s + y * c + center.y;
  226. };
  227. export {Vector2};