vec2.js.html 15 KB

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