Source: vec2.js

  1. import * as glMatrix from "./common.js";
  2. /**
  3. * 2 Dimensional Vector
  4. * @module vec2
  5. */
  6. /**
  7. * Creates a new, empty vec2
  8. *
  9. * @returns {vec2} a new 2D vector
  10. */
  11. export function create() {
  12. let out = new glMatrix.ARRAY_TYPE(2);
  13. if(glMatrix.ARRAY_TYPE != Float32Array) {
  14. out[0] = 0;
  15. out[1] = 0;
  16. }
  17. return out;
  18. }
  19. /**
  20. * Creates a new vec2 initialized with values from an existing vector
  21. *
  22. * @param {vec2} a vector to clone
  23. * @returns {vec2} a new 2D vector
  24. */
  25. export function clone(a) {
  26. let out = new glMatrix.ARRAY_TYPE(2);
  27. out[0] = a[0];
  28. out[1] = a[1];
  29. return out;
  30. }
  31. /**
  32. * Creates a new vec2 initialized with the given values
  33. *
  34. * @param {Number} x X component
  35. * @param {Number} y Y component
  36. * @returns {vec2} a new 2D vector
  37. */
  38. export function fromValues(x, y) {
  39. let out = new glMatrix.ARRAY_TYPE(2);
  40. out[0] = x;
  41. out[1] = y;
  42. return out;
  43. }
  44. /**
  45. * Copy the values from one vec2 to another
  46. *
  47. * @param {vec2} out the receiving vector
  48. * @param {vec2} a the source vector
  49. * @returns {vec2} out
  50. */
  51. export function copy(out, a) {
  52. out[0] = a[0];
  53. out[1] = a[1];
  54. return out;
  55. }
  56. /**
  57. * Set the components of a vec2 to the given values
  58. *
  59. * @param {vec2} out the receiving vector
  60. * @param {Number} x X component
  61. * @param {Number} y Y component
  62. * @returns {vec2} out
  63. */
  64. export function set(out, x, y) {
  65. out[0] = x;
  66. out[1] = y;
  67. return out;
  68. }
  69. /**
  70. * Adds two vec2's
  71. *
  72. * @param {vec2} out the receiving vector
  73. * @param {vec2} a the first operand
  74. * @param {vec2} b the second operand
  75. * @returns {vec2} out
  76. */
  77. export function add(out, a, b) {
  78. out[0] = a[0] + b[0];
  79. out[1] = a[1] + b[1];
  80. return out;
  81. }
  82. /**
  83. * Subtracts vector b from vector a
  84. *
  85. * @param {vec2} out the receiving vector
  86. * @param {vec2} a the first operand
  87. * @param {vec2} b the second operand
  88. * @returns {vec2} out
  89. */
  90. export function subtract(out, a, b) {
  91. out[0] = a[0] - b[0];
  92. out[1] = a[1] - b[1];
  93. return out;
  94. }
  95. /**
  96. * Multiplies two vec2's
  97. *
  98. * @param {vec2} out the receiving vector
  99. * @param {vec2} a the first operand
  100. * @param {vec2} b the second operand
  101. * @returns {vec2} out
  102. */
  103. export function multiply(out, a, b) {
  104. out[0] = a[0] * b[0];
  105. out[1] = a[1] * b[1];
  106. return out;
  107. }
  108. /**
  109. * Divides two vec2's
  110. *
  111. * @param {vec2} out the receiving vector
  112. * @param {vec2} a the first operand
  113. * @param {vec2} b the second operand
  114. * @returns {vec2} out
  115. */
  116. export function divide(out, a, b) {
  117. out[0] = a[0] / b[0];
  118. out[1] = a[1] / b[1];
  119. return out;
  120. }
  121. /**
  122. * Math.ceil the components of a vec2
  123. *
  124. * @param {vec2} out the receiving vector
  125. * @param {vec2} a vector to ceil
  126. * @returns {vec2} out
  127. */
  128. export function ceil(out, a) {
  129. out[0] = Math.ceil(a[0]);
  130. out[1] = Math.ceil(a[1]);
  131. return out;
  132. }
  133. /**
  134. * Math.floor the components of a vec2
  135. *
  136. * @param {vec2} out the receiving vector
  137. * @param {vec2} a vector to floor
  138. * @returns {vec2} out
  139. */
  140. export function floor(out, a) {
  141. out[0] = Math.floor(a[0]);
  142. out[1] = Math.floor(a[1]);
  143. return out;
  144. }
  145. /**
  146. * Returns the minimum of two vec2's
  147. *
  148. * @param {vec2} out the receiving vector
  149. * @param {vec2} a the first operand
  150. * @param {vec2} b the second operand
  151. * @returns {vec2} out
  152. */
  153. export function min(out, a, b) {
  154. out[0] = Math.min(a[0], b[0]);
  155. out[1] = Math.min(a[1], b[1]);
  156. return out;
  157. }
  158. /**
  159. * Returns the maximum of two vec2's
  160. *
  161. * @param {vec2} out the receiving vector
  162. * @param {vec2} a the first operand
  163. * @param {vec2} b the second operand
  164. * @returns {vec2} out
  165. */
  166. export function max(out, a, b) {
  167. out[0] = Math.max(a[0], b[0]);
  168. out[1] = Math.max(a[1], b[1]);
  169. return out;
  170. }
  171. /**
  172. * Math.round the components of a vec2
  173. *
  174. * @param {vec2} out the receiving vector
  175. * @param {vec2} a vector to round
  176. * @returns {vec2} out
  177. */
  178. export function round (out, a) {
  179. out[0] = Math.round(a[0]);
  180. out[1] = Math.round(a[1]);
  181. return out;
  182. }
  183. /**
  184. * Scales a vec2 by a scalar number
  185. *
  186. * @param {vec2} out the receiving vector
  187. * @param {vec2} a the vector to scale
  188. * @param {Number} b amount to scale the vector by
  189. * @returns {vec2} out
  190. */
  191. export function scale(out, a, b) {
  192. out[0] = a[0] * b;
  193. out[1] = a[1] * b;
  194. return out;
  195. }
  196. /**
  197. * Adds two vec2's after scaling the second operand by a scalar value
  198. *
  199. * @param {vec2} out the receiving vector
  200. * @param {vec2} a the first operand
  201. * @param {vec2} b the second operand
  202. * @param {Number} scale the amount to scale b by before adding
  203. * @returns {vec2} out
  204. */
  205. export function scaleAndAdd(out, a, b, scale) {
  206. out[0] = a[0] + (b[0] * scale);
  207. out[1] = a[1] + (b[1] * scale);
  208. return out;
  209. }
  210. /**
  211. * Calculates the euclidian distance between two vec2's
  212. *
  213. * @param {vec2} a the first operand
  214. * @param {vec2} b the second operand
  215. * @returns {Number} distance between a and b
  216. */
  217. export function distance(a, b) {
  218. var x = b[0] - a[0],
  219. y = b[1] - a[1];
  220. return Math.sqrt(x*x + y*y);
  221. }
  222. /**
  223. * Calculates the squared euclidian distance between two vec2's
  224. *
  225. * @param {vec2} a the first operand
  226. * @param {vec2} b the second operand
  227. * @returns {Number} squared distance between a and b
  228. */
  229. export function squaredDistance(a, b) {
  230. var x = b[0] - a[0],
  231. y = b[1] - a[1];
  232. return x*x + y*y;
  233. }
  234. /**
  235. * Calculates the length of a vec2
  236. *
  237. * @param {vec2} a vector to calculate length of
  238. * @returns {Number} length of a
  239. */
  240. export function length(a) {
  241. var x = a[0],
  242. y = a[1];
  243. return Math.sqrt(x*x + y*y);
  244. }
  245. /**
  246. * Calculates the squared length of a vec2
  247. *
  248. * @param {vec2} a vector to calculate squared length of
  249. * @returns {Number} squared length of a
  250. */
  251. export function squaredLength (a) {
  252. var x = a[0],
  253. y = a[1];
  254. return x*x + y*y;
  255. }
  256. /**
  257. * Negates the components of a vec2
  258. *
  259. * @param {vec2} out the receiving vector
  260. * @param {vec2} a vector to negate
  261. * @returns {vec2} out
  262. */
  263. export function negate(out, a) {
  264. out[0] = -a[0];
  265. out[1] = -a[1];
  266. return out;
  267. }
  268. /**
  269. * Returns the inverse of the components of a vec2
  270. *
  271. * @param {vec2} out the receiving vector
  272. * @param {vec2} a vector to invert
  273. * @returns {vec2} out
  274. */
  275. export function inverse(out, a) {
  276. out[0] = 1.0 / a[0];
  277. out[1] = 1.0 / a[1];
  278. return out;
  279. }
  280. /**
  281. * Normalize a vec2
  282. *
  283. * @param {vec2} out the receiving vector
  284. * @param {vec2} a vector to normalize
  285. * @returns {vec2} out
  286. */
  287. export function normalize(out, a) {
  288. var x = a[0],
  289. y = a[1];
  290. var len = x*x + y*y;
  291. if (len > 0) {
  292. //TODO: evaluate use of glm_invsqrt here?
  293. len = 1 / Math.sqrt(len);
  294. out[0] = a[0] * len;
  295. out[1] = a[1] * len;
  296. }
  297. return out;
  298. }
  299. /**
  300. * Calculates the dot product of two vec2's
  301. *
  302. * @param {vec2} a the first operand
  303. * @param {vec2} b the second operand
  304. * @returns {Number} dot product of a and b
  305. */
  306. export function dot(a, b) {
  307. return a[0] * b[0] + a[1] * b[1];
  308. }
  309. /**
  310. * Computes the cross product of two vec2's
  311. * Note that the cross product must by definition produce a 3D vector
  312. *
  313. * @param {vec3} out the receiving vector
  314. * @param {vec2} a the first operand
  315. * @param {vec2} b the second operand
  316. * @returns {vec3} out
  317. */
  318. export function cross(out, a, b) {
  319. var z = a[0] * b[1] - a[1] * b[0];
  320. out[0] = out[1] = 0;
  321. out[2] = z;
  322. return out;
  323. }
  324. /**
  325. * Performs a linear interpolation between two vec2's
  326. *
  327. * @param {vec2} out the receiving vector
  328. * @param {vec2} a the first operand
  329. * @param {vec2} b the second operand
  330. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  331. * @returns {vec2} out
  332. */
  333. export function lerp(out, a, b, t) {
  334. var ax = a[0],
  335. ay = a[1];
  336. out[0] = ax + t * (b[0] - ax);
  337. out[1] = ay + t * (b[1] - ay);
  338. return out;
  339. }
  340. /**
  341. * Generates a random vector with the given scale
  342. *
  343. * @param {vec2} out the receiving vector
  344. * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
  345. * @returns {vec2} out
  346. */
  347. export function random(out, scale) {
  348. scale = scale || 1.0;
  349. var r = glMatrix.RANDOM() * 2.0 * Math.PI;
  350. out[0] = Math.cos(r) * scale;
  351. out[1] = Math.sin(r) * scale;
  352. return out;
  353. }
  354. /**
  355. * Transforms the vec2 with a mat2
  356. *
  357. * @param {vec2} out the receiving vector
  358. * @param {vec2} a the vector to transform
  359. * @param {mat2} m matrix to transform with
  360. * @returns {vec2} out
  361. */
  362. export function transformMat2(out, a, m) {
  363. var x = a[0],
  364. y = a[1];
  365. out[0] = m[0] * x + m[2] * y;
  366. out[1] = m[1] * x + m[3] * y;
  367. return out;
  368. }
  369. /**
  370. * Transforms the vec2 with a mat2d
  371. *
  372. * @param {vec2} out the receiving vector
  373. * @param {vec2} a the vector to transform
  374. * @param {mat2d} m matrix to transform with
  375. * @returns {vec2} out
  376. */
  377. export function transformMat2d(out, a, m) {
  378. var x = a[0],
  379. y = a[1];
  380. out[0] = m[0] * x + m[2] * y + m[4];
  381. out[1] = m[1] * x + m[3] * y + m[5];
  382. return out;
  383. }
  384. /**
  385. * Transforms the vec2 with a mat3
  386. * 3rd vector component is implicitly '1'
  387. *
  388. * @param {vec2} out the receiving vector
  389. * @param {vec2} a the vector to transform
  390. * @param {mat3} m matrix to transform with
  391. * @returns {vec2} out
  392. */
  393. export function transformMat3(out, a, m) {
  394. var x = a[0],
  395. y = a[1];
  396. out[0] = m[0] * x + m[3] * y + m[6];
  397. out[1] = m[1] * x + m[4] * y + m[7];
  398. return out;
  399. }
  400. /**
  401. * Transforms the vec2 with a mat4
  402. * 3rd vector component is implicitly '0'
  403. * 4th vector component is implicitly '1'
  404. *
  405. * @param {vec2} out the receiving vector
  406. * @param {vec2} a the vector to transform
  407. * @param {mat4} m matrix to transform with
  408. * @returns {vec2} out
  409. */
  410. export function transformMat4(out, a, m) {
  411. let x = a[0];
  412. let y = a[1];
  413. out[0] = m[0] * x + m[4] * y + m[12];
  414. out[1] = m[1] * x + m[5] * y + m[13];
  415. return out;
  416. }
  417. /**
  418. * Rotate a 2D vector
  419. * @param {vec2} out The receiving vec2
  420. * @param {vec2} a The vec2 point to rotate
  421. * @param {vec2} b The origin of the rotation
  422. * @param {Number} c The angle of rotation
  423. * @returns {vec2} out
  424. */
  425. export function rotate(out, a, b, c) {
  426. //Translate point to the origin
  427. let p0 = a[0] - b[0],
  428. p1 = a[1] - b[1],
  429. sinC = Math.sin(c),
  430. cosC = Math.cos(c);
  431. //perform rotation and translate to correct position
  432. out[0] = p0*cosC - p1*sinC + b[0];
  433. out[1] = p0*sinC + p1*cosC + b[1];
  434. return out;
  435. }
  436. /**
  437. * Get the angle between two 2D vectors
  438. * @param {vec2} a The first operand
  439. * @param {vec2} b The second operand
  440. * @returns {Number} The angle in radians
  441. */
  442. export function angle(a, b) {
  443. let x1 = a[0],
  444. y1 = a[1],
  445. x2 = b[0],
  446. y2 = b[1];
  447. let len1 = x1*x1 + y1*y1;
  448. if (len1 > 0) {
  449. //TODO: evaluate use of glm_invsqrt here?
  450. len1 = 1 / Math.sqrt(len1);
  451. }
  452. let len2 = x2*x2 + y2*y2;
  453. if (len2 > 0) {
  454. //TODO: evaluate use of glm_invsqrt here?
  455. len2 = 1 / Math.sqrt(len2);
  456. }
  457. let cosine = (x1 * x2 + y1 * y2) * len1 * len2;
  458. if(cosine > 1.0) {
  459. return 0;
  460. }
  461. else if(cosine < -1.0) {
  462. return Math.PI;
  463. } else {
  464. return Math.acos(cosine);
  465. }
  466. }
  467. /**
  468. * Returns a string representation of a vector
  469. *
  470. * @param {vec2} a vector to represent as a string
  471. * @returns {String} string representation of the vector
  472. */
  473. export function str(a) {
  474. return 'vec2(' + a[0] + ', ' + a[1] + ')';
  475. }
  476. /**
  477. * Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===)
  478. *
  479. * @param {vec2} a The first vector.
  480. * @param {vec2} b The second vector.
  481. * @returns {Boolean} True if the vectors are equal, false otherwise.
  482. */
  483. export function exactEquals(a, b) {
  484. return a[0] === b[0] && a[1] === b[1];
  485. }
  486. /**
  487. * Returns whether or not the vectors have approximately the same elements in the same position.
  488. *
  489. * @param {vec2} a The first vector.
  490. * @param {vec2} b The second vector.
  491. * @returns {Boolean} True if the vectors are equal, false otherwise.
  492. */
  493. export function equals(a, b) {
  494. let a0 = a[0], a1 = a[1];
  495. let b0 = b[0], b1 = b[1];
  496. return (Math.abs(a0 - b0) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
  497. Math.abs(a1 - b1) <= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)));
  498. }
  499. /**
  500. * Alias for {@link vec2.length}
  501. * @function
  502. */
  503. export const len = length;
  504. /**
  505. * Alias for {@link vec2.subtract}
  506. * @function
  507. */
  508. export const sub = subtract;
  509. /**
  510. * Alias for {@link vec2.multiply}
  511. * @function
  512. */
  513. export const mul = multiply;
  514. /**
  515. * Alias for {@link vec2.divide}
  516. * @function
  517. */
  518. export const div = divide;
  519. /**
  520. * Alias for {@link vec2.distance}
  521. * @function
  522. */
  523. export const dist = distance;
  524. /**
  525. * Alias for {@link vec2.squaredDistance}
  526. * @function
  527. */
  528. export const sqrDist = squaredDistance;
  529. /**
  530. * Alias for {@link vec2.squaredLength}
  531. * @function
  532. */
  533. export const sqrLen = squaredLength;
  534. /**
  535. * Perform some operation over an array of vec2s.
  536. *
  537. * @param {Array} a the array of vectors to iterate over
  538. * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed
  539. * @param {Number} offset Number of elements to skip at the beginning of the array
  540. * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
  541. * @param {Function} fn Function to call for each vector in the array
  542. * @param {Object} [arg] additional argument to pass to fn
  543. * @returns {Array} a
  544. * @function
  545. */
  546. export const forEach = (function() {
  547. let vec = create();
  548. return function(a, stride, offset, count, fn, arg) {
  549. let i, l;
  550. if(!stride) {
  551. stride = 2;
  552. }
  553. if(!offset) {
  554. offset = 0;
  555. }
  556. if(count) {
  557. l = Math.min((count * stride) + offset, a.length);
  558. } else {
  559. l = a.length;
  560. }
  561. for(i = offset; i < l; i += stride) {
  562. vec[0] = a[i]; vec[1] = a[i+1];
  563. fn(vec, vec, arg);
  564. a[i] = vec[0]; a[i+1] = vec[1];
  565. }
  566. return a;
  567. };
  568. })();