Vector2.js 9.6 KB

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