Vector2.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /**
  2. * 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.
  3. *
  4. * @class
  5. * @param {number} x
  6. * @param {number} y
  7. */
  8. function Vector2(x, y)
  9. {
  10. this.x = x || 0;
  11. this.y = y || 0;
  12. }
  13. /**
  14. * Set vector x and y values.
  15. *
  16. * @param {number} x
  17. * @param {number} y
  18. */
  19. Vector2.prototype.set = function(x, y)
  20. {
  21. this.x = x;
  22. this.y = y;
  23. };
  24. /**
  25. * Set a scalar value into the x and y values.
  26. */
  27. Vector2.prototype.setScalar = function(scalar)
  28. {
  29. this.x = scalar;
  30. this.y = scalar;
  31. };
  32. /**
  33. * Create a clone of this vector object.
  34. */
  35. Vector2.prototype.clone = function()
  36. {
  37. return new Vector2(this.x, this.y);
  38. };
  39. /**
  40. * Copy the content of another vector into this one.
  41. *
  42. * @param {Vector2} v
  43. */
  44. Vector2.prototype.copy = function(v)
  45. {
  46. this.x = v.x;
  47. this.y = v.y;
  48. };
  49. /**
  50. * Add the content of another vector to this one.
  51. *
  52. * @param {Vector2} v
  53. */
  54. Vector2.prototype.add = function(v)
  55. {
  56. this.x += v.x;
  57. this.y += v.y;
  58. };
  59. /**
  60. * Add a scalar value to booth vector components.
  61. *
  62. * @param {number} s
  63. */
  64. Vector2.prototype.addScalar = function(s)
  65. {
  66. this.x += s;
  67. this.y += s;
  68. };
  69. /**
  70. * Add two vectors and store the result in this vector.
  71. *
  72. * @param {Vector2} a
  73. * @param {Vector2} b
  74. */
  75. Vector2.prototype.addVectors = function(a, b)
  76. {
  77. this.x = a.x + b.x;
  78. this.y = a.y + b.y;
  79. };
  80. /**
  81. * Scale a vector components and add the result to this vector.
  82. *
  83. * @param {Vector2} v
  84. * @param {number} s
  85. */
  86. Vector2.prototype.addScaledVector = function(v, s)
  87. {
  88. this.x += v.x * s;
  89. this.y += v.y * s;
  90. };
  91. /**
  92. * Subtract the content of another vector to this one.
  93. *
  94. * @param {Vector2} v
  95. */
  96. Vector2.prototype.sub = function(v)
  97. {
  98. this.x -= v.x;
  99. this.y -= v.y;
  100. };
  101. /**
  102. * Subtract a scalar value to booth vector components.
  103. *
  104. * @param {number} s
  105. */
  106. Vector2.prototype.subScalar = function(s)
  107. {
  108. this.x -= s;
  109. this.y -= s;
  110. };
  111. /**
  112. * Subtract two vectors and store the result in this vector.
  113. *
  114. * @param {Vector2} a
  115. * @param {Vector2} b
  116. */
  117. Vector2.prototype.subVectors = function(a, b)
  118. {
  119. this.x = a.x - b.x;
  120. this.y = a.y - b.y;
  121. };
  122. /**
  123. * Multiply the content of another vector to this one.
  124. *
  125. * @param {Vector2} v
  126. */
  127. Vector2.prototype.multiply = function(v)
  128. {
  129. this.x *= v.x;
  130. this.y *= v.y;
  131. };
  132. /**
  133. * Multiply a scalar value by booth vector components.
  134. *
  135. * @param {number} scalar
  136. */
  137. Vector2.prototype.multiplyScalar = function(scalar)
  138. {
  139. this.x *= scalar;
  140. this.y *= scalar;
  141. };
  142. /**
  143. * Divide the content of another vector from this one.
  144. *
  145. * @param {Vector2} v
  146. */
  147. Vector2.prototype.divide = function(v)
  148. {
  149. this.x /= v.x;
  150. this.y /= v.y;
  151. };
  152. /**
  153. * Divide a scalar value by booth vector components.
  154. *
  155. * @param {number} s
  156. */
  157. Vector2.prototype.divideScalar = function(scalar)
  158. {
  159. return this.multiplyScalar(1 / scalar);
  160. };
  161. /**
  162. * Set the minimum of x and y coordinates between two vectors.
  163. *
  164. * X is set as the min between this vector and the other vector.
  165. *
  166. * @param {Vector2} v
  167. */
  168. Vector2.prototype.min = function(v)
  169. {
  170. this.x = Math.min(this.x, v.x);
  171. this.y = Math.min(this.y, v.y);
  172. };
  173. /**
  174. * Set the maximum of x and y coordinates between two vectors.
  175. *
  176. * X is set as the max between this vector and the other vector.
  177. *
  178. * @param {Vector2} v
  179. */
  180. Vector2.prototype.max = function(v)
  181. {
  182. this.x = Math.max(this.x, v.x);
  183. this.y = Math.max(this.y, v.y);
  184. };
  185. Vector2.prototype.clamp = function(min, max)
  186. {
  187. // assumes min < max, componentwise
  188. this.x = Math.max(min.x, Math.min(max.x, this.x));
  189. this.y = Math.max(min.y, Math.min(max.y, this.y));
  190. };
  191. Vector2.prototype.clampScalar = function(minVal, maxVal)
  192. {
  193. this.x = Math.max(minVal, Math.min(maxVal, this.x));
  194. this.y = Math.max(minVal, Math.min(maxVal, this.y));
  195. };
  196. Vector2.prototype.clampLength = function(min, max)
  197. {
  198. var length = this.length();
  199. return this.divideScalar(length || 1).multiplyScalar(Math.max(min, Math.min(max, length)));
  200. };
  201. /**
  202. * Round the vector coordinates to integer by flooring to the smaller integer.
  203. */
  204. Vector2.prototype.floor = function()
  205. {
  206. this.x = Math.floor(this.x);
  207. this.y = Math.floor(this.y);
  208. };
  209. /**
  210. * Round the vector coordinates to integer by ceiling to the bigger integer.
  211. */
  212. Vector2.prototype.ceil = function()
  213. {
  214. this.x = Math.ceil(this.x);
  215. this.y = Math.ceil(this.y);
  216. };
  217. /**
  218. * Round the vector coordinates to their closest integer.
  219. */
  220. Vector2.prototype.round = function()
  221. {
  222. this.x = Math.round(this.x);
  223. this.y = Math.round(this.y);
  224. };
  225. /**
  226. * Negate the coordinates of this vector.
  227. */
  228. Vector2.prototype.negate = function()
  229. {
  230. this.x = -this.x;
  231. this.y = -this.y;
  232. return this;
  233. };
  234. /**
  235. * Dot multiplication between this vector and another vector.
  236. *
  237. * @param {Vector2} vector
  238. * @return {number} Result of the dot multiplication.
  239. */
  240. Vector2.prototype.dot = function(v)
  241. {
  242. return this.x * v.x + this.y * v.y;
  243. };
  244. /**
  245. * Cross multiplication between this vector and another vector.
  246. *
  247. * @param {Vector2} vector
  248. * @return {number} Result of the cross multiplication.
  249. */
  250. Vector2.prototype.cross = function(v)
  251. {
  252. return this.x * v.y - this.y * v.x;
  253. };
  254. /**
  255. * Squared length of the vector.
  256. *
  257. * Faster for comparions.
  258. */
  259. Vector2.prototype.lengthSq = function()
  260. {
  261. return this.x * this.x + this.y * this.y;
  262. };
  263. /**
  264. * Length of the vector.
  265. */
  266. Vector2.prototype.length = function()
  267. {
  268. return Math.sqrt(this.x * this.x + this.y * this.y);
  269. };
  270. /**
  271. * Manhattan length of the vector.
  272. */
  273. Vector2.prototype.manhattanLength = function()
  274. {
  275. return Math.abs(this.x) + Math.abs(this.y);
  276. };
  277. /**
  278. * Normalize the vector (make it length one).
  279. */
  280. Vector2.prototype.normalize = function()
  281. {
  282. return this.divideScalar(this.length() || 1);
  283. };
  284. /**
  285. * Computes the angle in radians with respect to the positive x-axis
  286. */
  287. Vector2.prototype.angle = function()
  288. {
  289. var angle = Math.atan2(this.y, this.x);
  290. if(angle < 0)
  291. {
  292. angle += 2 * Math.PI;
  293. }
  294. return angle;
  295. };
  296. /**
  297. * Distance between two vector positions.
  298. */
  299. Vector2.prototype.distanceTo = function(v)
  300. {
  301. return Math.sqrt(this.distanceToSquared(v));
  302. };
  303. /**
  304. * Distance between two vector positions squared.
  305. *
  306. * Faster for comparisons.
  307. */
  308. Vector2.prototype.distanceToSquared = function(v)
  309. {
  310. var dx = this.x - v.x;
  311. var dy = this.y - v.y;
  312. return dx * dx + dy * dy;
  313. };
  314. /**
  315. * Manhattan distance between two vector positions.
  316. */
  317. Vector2.prototype.manhattanDistanceTo = function(v)
  318. {
  319. return Math.abs(this.x - v.x) + Math.abs(this.y - v.y);
  320. };
  321. /**
  322. * Scale the vector to have a defined length value.
  323. */
  324. Vector2.prototype.setLength = function(length)
  325. {
  326. return this.normalize().multiplyScalar(length);
  327. };
  328. Vector2.prototype.lerp = function(v, alpha)
  329. {
  330. this.x += (v.x - this.x) * alpha;
  331. this.y += (v.y - this.y) * alpha;
  332. };
  333. Vector2.prototype.lerpVectors = function(v1, v2, alpha)
  334. {
  335. return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1);
  336. };
  337. /**
  338. * Check if two vectors are equal.
  339. *
  340. * @param {Vector2} v
  341. */
  342. Vector2.prototype.equals = function(v)
  343. {
  344. return ((v.x === this.x) && (v.y === this.y));
  345. };
  346. /**
  347. * Set vector value from array with a offset.
  348. *
  349. * @param {array} array
  350. * @param {number} [offset]
  351. */
  352. Vector2.prototype.fromArray = function(array, offset)
  353. {
  354. if(offset === undefined) offset = 0;
  355. this.x = array[offset];
  356. this.y = array[offset + 1];
  357. };
  358. /**
  359. * Convert this vector to an array.
  360. *
  361. * @param {array} array
  362. * @param {number} [offset]
  363. */
  364. Vector2.prototype.toArray = function(array, offset)
  365. {
  366. if(array === undefined) array = [];
  367. if(offset === undefined) offset = 0;
  368. array[offset] = this.x;
  369. array[offset + 1] = this.y;
  370. return array;
  371. };
  372. /**
  373. * Rotate the vector around a central point.
  374. *
  375. * @param {Vector2} center
  376. * @param {number} angle
  377. */
  378. Vector2.prototype.rotateAround = function(center, angle)
  379. {
  380. var c = Math.cos(angle);
  381. var s = Math.sin(angle);
  382. var x = this.x - center.x;
  383. var y = this.y - center.y;
  384. this.x = x * c - y * s + center.x;
  385. this.y = x * s + y * c + center.y;
  386. };
  387. export {Vector2};