mat2.js.html 11 KB

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