Source: node_modules/gl-matrix/esm/mat2d.js

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