mat2d.js.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: mat2d.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: mat2d.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>import * as glMatrix from "./common.js";
  20. /**
  21. * 2x3 Matrix
  22. * @module mat2d
  23. *
  24. * @description
  25. * A mat2d contains six elements defined as:
  26. * &lt;pre>
  27. * [a, c, tx,
  28. * b, d, ty]
  29. * &lt;/pre>
  30. * This is a short form for the 3x3 matrix:
  31. * &lt;pre>
  32. * [a, c, tx,
  33. * b, d, ty,
  34. * 0, 0, 1]
  35. * &lt;/pre>
  36. * The last row is ignored so the array is shorter and operations are faster.
  37. */
  38. /**
  39. * Creates a new identity mat2d
  40. *
  41. * @returns {mat2d} a new 2x3 matrix
  42. */
  43. export function create() {
  44. let out = new glMatrix.ARRAY_TYPE(6);
  45. if(glMatrix.ARRAY_TYPE != Float32Array) {
  46. out[1] = 0;
  47. out[2] = 0;
  48. out[4] = 0;
  49. out[5] = 0;
  50. }
  51. out[0] = 1;
  52. out[3] = 1;
  53. return out;
  54. }
  55. /**
  56. * Creates a new mat2d initialized with values from an existing matrix
  57. *
  58. * @param {mat2d} a matrix to clone
  59. * @returns {mat2d} a new 2x3 matrix
  60. */
  61. export function clone(a) {
  62. let out = new glMatrix.ARRAY_TYPE(6);
  63. out[0] = a[0];
  64. out[1] = a[1];
  65. out[2] = a[2];
  66. out[3] = a[3];
  67. out[4] = a[4];
  68. out[5] = a[5];
  69. return out;
  70. }
  71. /**
  72. * Copy the values from one mat2d to another
  73. *
  74. * @param {mat2d} out the receiving matrix
  75. * @param {mat2d} a the source matrix
  76. * @returns {mat2d} out
  77. */
  78. export function copy(out, a) {
  79. out[0] = a[0];
  80. out[1] = a[1];
  81. out[2] = a[2];
  82. out[3] = a[3];
  83. out[4] = a[4];
  84. out[5] = a[5];
  85. return out;
  86. }
  87. /**
  88. * Set a mat2d to the identity matrix
  89. *
  90. * @param {mat2d} out the receiving matrix
  91. * @returns {mat2d} out
  92. */
  93. export function identity(out) {
  94. out[0] = 1;
  95. out[1] = 0;
  96. out[2] = 0;
  97. out[3] = 1;
  98. out[4] = 0;
  99. out[5] = 0;
  100. return out;
  101. }
  102. /**
  103. * Create a new mat2d with the given values
  104. *
  105. * @param {Number} a Component A (index 0)
  106. * @param {Number} b Component B (index 1)
  107. * @param {Number} c Component C (index 2)
  108. * @param {Number} d Component D (index 3)
  109. * @param {Number} tx Component TX (index 4)
  110. * @param {Number} ty Component TY (index 5)
  111. * @returns {mat2d} A new mat2d
  112. */
  113. export function fromValues(a, b, c, d, tx, ty) {
  114. let out = new glMatrix.ARRAY_TYPE(6);
  115. out[0] = a;
  116. out[1] = b;
  117. out[2] = c;
  118. out[3] = d;
  119. out[4] = tx;
  120. out[5] = ty;
  121. return out;
  122. }
  123. /**
  124. * Set the components of a mat2d to the given values
  125. *
  126. * @param {mat2d} out the receiving matrix
  127. * @param {Number} a Component A (index 0)
  128. * @param {Number} b Component B (index 1)
  129. * @param {Number} c Component C (index 2)
  130. * @param {Number} d Component D (index 3)
  131. * @param {Number} tx Component TX (index 4)
  132. * @param {Number} ty Component TY (index 5)
  133. * @returns {mat2d} out
  134. */
  135. export function set(out, a, b, c, d, tx, ty) {
  136. out[0] = a;
  137. out[1] = b;
  138. out[2] = c;
  139. out[3] = d;
  140. out[4] = tx;
  141. out[5] = ty;
  142. return out;
  143. }
  144. /**
  145. * Inverts a mat2d
  146. *
  147. * @param {mat2d} out the receiving matrix
  148. * @param {mat2d} a the source matrix
  149. * @returns {mat2d} out
  150. */
  151. export function invert(out, a) {
  152. let aa = a[0], ab = a[1], ac = a[2], ad = a[3];
  153. let atx = a[4], aty = a[5];
  154. let det = aa * ad - ab * ac;
  155. if(!det){
  156. return null;
  157. }
  158. det = 1.0 / det;
  159. out[0] = ad * det;
  160. out[1] = -ab * det;
  161. out[2] = -ac * det;
  162. out[3] = aa * det;
  163. out[4] = (ac * aty - ad * atx) * det;
  164. out[5] = (ab * atx - aa * aty) * det;
  165. return out;
  166. }
  167. /**
  168. * Calculates the determinant of a mat2d
  169. *
  170. * @param {mat2d} a the source matrix
  171. * @returns {Number} determinant of a
  172. */
  173. export function determinant(a) {
  174. return a[0] * a[3] - a[1] * a[2];
  175. }
  176. /**
  177. * Multiplies two mat2d's
  178. *
  179. * @param {mat2d} out the receiving matrix
  180. * @param {mat2d} a the first operand
  181. * @param {mat2d} b the second operand
  182. * @returns {mat2d} out
  183. */
  184. export function multiply(out, a, b) {
  185. let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5];
  186. let b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5];
  187. out[0] = a0 * b0 + a2 * b1;
  188. out[1] = a1 * b0 + a3 * b1;
  189. out[2] = a0 * b2 + a2 * b3;
  190. out[3] = a1 * b2 + a3 * b3;
  191. out[4] = a0 * b4 + a2 * b5 + a4;
  192. out[5] = a1 * b4 + a3 * b5 + a5;
  193. return out;
  194. }
  195. /**
  196. * Rotates a mat2d by the given angle
  197. *
  198. * @param {mat2d} out the receiving matrix
  199. * @param {mat2d} a the matrix to rotate
  200. * @param {Number} rad the angle to rotate the matrix by
  201. * @returns {mat2d} out
  202. */
  203. export function rotate(out, a, rad) {
  204. let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5];
  205. let s = Math.sin(rad);
  206. let c = Math.cos(rad);
  207. out[0] = a0 * c + a2 * s;
  208. out[1] = a1 * c + a3 * s;
  209. out[2] = a0 * -s + a2 * c;
  210. out[3] = a1 * -s + a3 * c;
  211. out[4] = a4;
  212. out[5] = a5;
  213. return out;
  214. }
  215. /**
  216. * Scales the mat2d by the dimensions in the given vec2
  217. *
  218. * @param {mat2d} out the receiving matrix
  219. * @param {mat2d} a the matrix to translate
  220. * @param {vec2} v the vec2 to scale the matrix by
  221. * @returns {mat2d} out
  222. **/
  223. export function scale(out, a, v) {
  224. let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5];
  225. let v0 = v[0], v1 = v[1];
  226. out[0] = a0 * v0;
  227. out[1] = a1 * v0;
  228. out[2] = a2 * v1;
  229. out[3] = a3 * v1;
  230. out[4] = a4;
  231. out[5] = a5;
  232. return out;
  233. }
  234. /**
  235. * Translates the mat2d by the dimensions in the given vec2
  236. *
  237. * @param {mat2d} out the receiving matrix
  238. * @param {mat2d} a the matrix to translate
  239. * @param {vec2} v the vec2 to translate the matrix by
  240. * @returns {mat2d} out
  241. **/
  242. export function translate(out, a, v) {
  243. let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5];
  244. let v0 = v[0], v1 = v[1];
  245. out[0] = a0;
  246. out[1] = a1;
  247. out[2] = a2;
  248. out[3] = a3;
  249. out[4] = a0 * v0 + a2 * v1 + a4;
  250. out[5] = a1 * v0 + a3 * v1 + a5;
  251. return out;
  252. }
  253. /**
  254. * Creates a matrix from a given angle
  255. * This is equivalent to (but much faster than):
  256. *
  257. * mat2d.identity(dest);
  258. * mat2d.rotate(dest, dest, rad);
  259. *
  260. * @param {mat2d} out mat2d receiving operation result
  261. * @param {Number} rad the angle to rotate the matrix by
  262. * @returns {mat2d} out
  263. */
  264. export function fromRotation(out, rad) {
  265. let s = Math.sin(rad), c = Math.cos(rad);
  266. out[0] = c;
  267. out[1] = s;
  268. out[2] = -s;
  269. out[3] = c;
  270. out[4] = 0;
  271. out[5] = 0;
  272. return out;
  273. }
  274. /**
  275. * Creates a matrix from a vector scaling
  276. * This is equivalent to (but much faster than):
  277. *
  278. * mat2d.identity(dest);
  279. * mat2d.scale(dest, dest, vec);
  280. *
  281. * @param {mat2d} out mat2d receiving operation result
  282. * @param {vec2} v Scaling vector
  283. * @returns {mat2d} out
  284. */
  285. export function fromScaling(out, v) {
  286. out[0] = v[0];
  287. out[1] = 0;
  288. out[2] = 0;
  289. out[3] = v[1];
  290. out[4] = 0;
  291. out[5] = 0;
  292. return out;
  293. }
  294. /**
  295. * Creates a matrix from a vector translation
  296. * This is equivalent to (but much faster than):
  297. *
  298. * mat2d.identity(dest);
  299. * mat2d.translate(dest, dest, vec);
  300. *
  301. * @param {mat2d} out mat2d receiving operation result
  302. * @param {vec2} v Translation vector
  303. * @returns {mat2d} out
  304. */
  305. export function fromTranslation(out, v) {
  306. out[0] = 1;
  307. out[1] = 0;
  308. out[2] = 0;
  309. out[3] = 1;
  310. out[4] = v[0];
  311. out[5] = v[1];
  312. return out;
  313. }
  314. /**
  315. * Returns a string representation of a mat2d
  316. *
  317. * @param {mat2d} a matrix to represent as a string
  318. * @returns {String} string representation of the matrix
  319. */
  320. export function str(a) {
  321. return 'mat2d(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' +
  322. a[3] + ', ' + a[4] + ', ' + a[5] + ')';
  323. }
  324. /**
  325. * Returns Frobenius norm of a mat2d
  326. *
  327. * @param {mat2d} a the matrix to calculate Frobenius norm of
  328. * @returns {Number} Frobenius norm
  329. */
  330. export function frob(a) {
  331. return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + 1))
  332. }
  333. /**
  334. * Adds two mat2d's
  335. *
  336. * @param {mat2d} out the receiving matrix
  337. * @param {mat2d} a the first operand
  338. * @param {mat2d} b the second operand
  339. * @returns {mat2d} out
  340. */
  341. export function add(out, a, b) {
  342. out[0] = a[0] + b[0];
  343. out[1] = a[1] + b[1];
  344. out[2] = a[2] + b[2];
  345. out[3] = a[3] + b[3];
  346. out[4] = a[4] + b[4];
  347. out[5] = a[5] + b[5];
  348. return out;
  349. }
  350. /**
  351. * Subtracts matrix b from matrix a
  352. *
  353. * @param {mat2d} out the receiving matrix
  354. * @param {mat2d} a the first operand
  355. * @param {mat2d} b the second operand
  356. * @returns {mat2d} out
  357. */
  358. export function subtract(out, a, b) {
  359. out[0] = a[0] - b[0];
  360. out[1] = a[1] - b[1];
  361. out[2] = a[2] - b[2];
  362. out[3] = a[3] - b[3];
  363. out[4] = a[4] - b[4];
  364. out[5] = a[5] - b[5];
  365. return out;
  366. }
  367. /**
  368. * Multiply each element of the matrix by a scalar.
  369. *
  370. * @param {mat2d} out the receiving matrix
  371. * @param {mat2d} a the matrix to scale
  372. * @param {Number} b amount to scale the matrix's elements by
  373. * @returns {mat2d} out
  374. */
  375. export function multiplyScalar(out, a, b) {
  376. out[0] = a[0] * b;
  377. out[1] = a[1] * b;
  378. out[2] = a[2] * b;
  379. out[3] = a[3] * b;
  380. out[4] = a[4] * b;
  381. out[5] = a[5] * b;
  382. return out;
  383. }
  384. /**
  385. * Adds two mat2d's after multiplying each element of the second operand by a scalar value.
  386. *
  387. * @param {mat2d} out the receiving vector
  388. * @param {mat2d} a the first operand
  389. * @param {mat2d} b the second operand
  390. * @param {Number} scale the amount to scale b's elements by before adding
  391. * @returns {mat2d} out
  392. */
  393. export function multiplyScalarAndAdd(out, a, b, scale) {
  394. out[0] = a[0] + (b[0] * scale);
  395. out[1] = a[1] + (b[1] * scale);
  396. out[2] = a[2] + (b[2] * scale);
  397. out[3] = a[3] + (b[3] * scale);
  398. out[4] = a[4] + (b[4] * scale);
  399. out[5] = a[5] + (b[5] * scale);
  400. return out;
  401. }
  402. /**
  403. * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
  404. *
  405. * @param {mat2d} a The first matrix.
  406. * @param {mat2d} b The second matrix.
  407. * @returns {Boolean} True if the matrices are equal, false otherwise.
  408. */
  409. export function exactEquals(a, b) {
  410. return a[0] === b[0] &amp;&amp; a[1] === b[1] &amp;&amp; a[2] === b[2] &amp;&amp; a[3] === b[3] &amp;&amp; a[4] === b[4] &amp;&amp; a[5] === b[5];
  411. }
  412. /**
  413. * Returns whether or not the matrices have approximately the same elements in the same position.
  414. *
  415. * @param {mat2d} a The first matrix.
  416. * @param {mat2d} b The second matrix.
  417. * @returns {Boolean} True if the matrices are equal, false otherwise.
  418. */
  419. export function equals(a, b) {
  420. let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5];
  421. let b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5];
  422. return (Math.abs(a0 - b0) &lt;= glMatrix.EPSILON*Math.max(1.0, Math.abs(a0), Math.abs(b0)) &amp;&amp;
  423. Math.abs(a1 - b1) &lt;= glMatrix.EPSILON*Math.max(1.0, Math.abs(a1), Math.abs(b1)) &amp;&amp;
  424. Math.abs(a2 - b2) &lt;= glMatrix.EPSILON*Math.max(1.0, Math.abs(a2), Math.abs(b2)) &amp;&amp;
  425. Math.abs(a3 - b3) &lt;= glMatrix.EPSILON*Math.max(1.0, Math.abs(a3), Math.abs(b3)) &amp;&amp;
  426. Math.abs(a4 - b4) &lt;= glMatrix.EPSILON*Math.max(1.0, Math.abs(a4), Math.abs(b4)) &amp;&amp;
  427. Math.abs(a5 - b5) &lt;= glMatrix.EPSILON*Math.max(1.0, Math.abs(a5), Math.abs(b5)));
  428. }
  429. /**
  430. * Alias for {@link mat2d.multiply}
  431. * @function
  432. */
  433. export const mul = multiply;
  434. /**
  435. * Alias for {@link mat2d.subtract}
  436. * @function
  437. */
  438. export const sub = subtract;
  439. </code></pre>
  440. </article>
  441. </section>
  442. </div>
  443. <nav>
  444. <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>
  445. </nav>
  446. <br class="clear">
  447. <footer>
  448. 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)
  449. </footer>
  450. <script> prettyPrint(); </script>
  451. <script src="scripts/linenumber.js"> </script>
  452. </body>
  453. </html>