vec2.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. var 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 {ReadonlyVec2} a vector to clone
  23. * @returns {vec2} a new 2D vector
  24. */
  25. export function clone(a) {
  26. var 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. var 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 {ReadonlyVec2} 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 {ReadonlyVec2} a the first operand
  74. * @param {ReadonlyVec2} 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 {ReadonlyVec2} a the first operand
  87. * @param {ReadonlyVec2} 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 {ReadonlyVec2} a the first operand
  100. * @param {ReadonlyVec2} 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 {ReadonlyVec2} a the first operand
  113. * @param {ReadonlyVec2} 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 {ReadonlyVec2} 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 {ReadonlyVec2} 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 {ReadonlyVec2} a the first operand
  150. * @param {ReadonlyVec2} 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 {ReadonlyVec2} a the first operand
  163. * @param {ReadonlyVec2} 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 {ReadonlyVec2} 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 {ReadonlyVec2} 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 {ReadonlyVec2} a the first operand
  201. * @param {ReadonlyVec2} 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 {ReadonlyVec2} a the first operand
  214. * @param {ReadonlyVec2} 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.hypot(x, y);
  221. }
  222. /**
  223. * Calculates the squared euclidian distance between two vec2's
  224. *
  225. * @param {ReadonlyVec2} a the first operand
  226. * @param {ReadonlyVec2} 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 {ReadonlyVec2} 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.hypot(x, y);
  244. }
  245. /**
  246. * Calculates the squared length of a vec2
  247. *
  248. * @param {ReadonlyVec2} 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 {ReadonlyVec2} 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 {ReadonlyVec2} 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 {ReadonlyVec2} 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. }
  295. out[0] = a[0] * len;
  296. out[1] = a[1] * len;
  297. return out;
  298. }
  299. /**
  300. * Calculates the dot product of two vec2's
  301. *
  302. * @param {ReadonlyVec2} a the first operand
  303. * @param {ReadonlyVec2} 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 {ReadonlyVec2} a the first operand
  315. * @param {ReadonlyVec2} 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 {ReadonlyVec2} a the first operand
  329. * @param {ReadonlyVec2} 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 {ReadonlyVec2} a the vector to transform
  359. * @param {ReadonlyMat2} 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 {ReadonlyVec2} a the vector to transform
  374. * @param {ReadonlyMat2d} 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 {ReadonlyVec2} a the vector to transform
  390. * @param {ReadonlyMat3} 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 {ReadonlyVec2} a the vector to transform
  407. * @param {ReadonlyMat4} m matrix to transform with
  408. * @returns {vec2} out
  409. */
  410. export function transformMat4(out, a, m) {
  411. var x = a[0];
  412. var 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 {ReadonlyVec2} a The vec2 point to rotate
  421. * @param {ReadonlyVec2} b The origin of the rotation
  422. * @param {Number} rad The angle of rotation in radians
  423. * @returns {vec2} out
  424. */
  425. export function rotate(out, a, b, rad) {
  426. //Translate point to the origin
  427. var p0 = a[0] - b[0],
  428. p1 = a[1] - b[1],
  429. sinC = Math.sin(rad),
  430. cosC = Math.cos(rad); //perform rotation and translate to correct position
  431. out[0] = p0 * cosC - p1 * sinC + b[0];
  432. out[1] = p0 * sinC + p1 * cosC + b[1];
  433. return out;
  434. }
  435. /**
  436. * Get the angle between two 2D vectors
  437. * @param {ReadonlyVec2} a The first operand
  438. * @param {ReadonlyVec2} b The second operand
  439. * @returns {Number} The angle in radians
  440. */
  441. export function angle(a, b) {
  442. var x1 = a[0],
  443. y1 = a[1],
  444. x2 = b[0],
  445. y2 = b[1],
  446. // mag is the product of the magnitudes of a and b
  447. mag = Math.sqrt(x1 * x1 + y1 * y1) * Math.sqrt(x2 * x2 + y2 * y2),
  448. // mag &&.. short circuits if mag == 0
  449. cosine = mag && (x1 * x2 + y1 * y2) / mag; // Math.min(Math.max(cosine, -1), 1) clamps the cosine between -1 and 1
  450. return Math.acos(Math.min(Math.max(cosine, -1), 1));
  451. }
  452. /**
  453. * Set the components of a vec2 to zero
  454. *
  455. * @param {vec2} out the receiving vector
  456. * @returns {vec2} out
  457. */
  458. export function zero(out) {
  459. out[0] = 0.0;
  460. out[1] = 0.0;
  461. return out;
  462. }
  463. /**
  464. * Returns a string representation of a vector
  465. *
  466. * @param {ReadonlyVec2} a vector to represent as a string
  467. * @returns {String} string representation of the vector
  468. */
  469. export function str(a) {
  470. return "vec2(" + a[0] + ", " + a[1] + ")";
  471. }
  472. /**
  473. * Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===)
  474. *
  475. * @param {ReadonlyVec2} a The first vector.
  476. * @param {ReadonlyVec2} b The second vector.
  477. * @returns {Boolean} True if the vectors are equal, false otherwise.
  478. */
  479. export function exactEquals(a, b) {
  480. return a[0] === b[0] && a[1] === b[1];
  481. }
  482. /**
  483. * Returns whether or not the vectors have approximately the same elements in the same position.
  484. *
  485. * @param {ReadonlyVec2} a The first vector.
  486. * @param {ReadonlyVec2} b The second vector.
  487. * @returns {Boolean} True if the vectors are equal, false otherwise.
  488. */
  489. export function equals(a, b) {
  490. var a0 = a[0],
  491. a1 = a[1];
  492. var b0 = b[0],
  493. b1 = b[1];
  494. return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1));
  495. }
  496. /**
  497. * Alias for {@link vec2.length}
  498. * @function
  499. */
  500. export var len = length;
  501. /**
  502. * Alias for {@link vec2.subtract}
  503. * @function
  504. */
  505. export var sub = subtract;
  506. /**
  507. * Alias for {@link vec2.multiply}
  508. * @function
  509. */
  510. export var mul = multiply;
  511. /**
  512. * Alias for {@link vec2.divide}
  513. * @function
  514. */
  515. export var div = divide;
  516. /**
  517. * Alias for {@link vec2.distance}
  518. * @function
  519. */
  520. export var dist = distance;
  521. /**
  522. * Alias for {@link vec2.squaredDistance}
  523. * @function
  524. */
  525. export var sqrDist = squaredDistance;
  526. /**
  527. * Alias for {@link vec2.squaredLength}
  528. * @function
  529. */
  530. export var sqrLen = squaredLength;
  531. /**
  532. * Perform some operation over an array of vec2s.
  533. *
  534. * @param {Array} a the array of vectors to iterate over
  535. * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed
  536. * @param {Number} offset Number of elements to skip at the beginning of the array
  537. * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
  538. * @param {Function} fn Function to call for each vector in the array
  539. * @param {Object} [arg] additional argument to pass to fn
  540. * @returns {Array} a
  541. * @function
  542. */
  543. export var forEach = function () {
  544. var vec = create();
  545. return function (a, stride, offset, count, fn, arg) {
  546. var i, l;
  547. if (!stride) {
  548. stride = 2;
  549. }
  550. if (!offset) {
  551. offset = 0;
  552. }
  553. if (count) {
  554. l = Math.min(count * stride + offset, a.length);
  555. } else {
  556. l = a.length;
  557. }
  558. for (i = offset; i < l; i += stride) {
  559. vec[0] = a[i];
  560. vec[1] = a[i + 1];
  561. fn(vec, vec, arg);
  562. a[i] = vec[0];
  563. a[i + 1] = vec[1];
  564. }
  565. return a;
  566. };
  567. }();