/
Matrix2.js
402 lines (321 loc) · 7.72 KB
/
Matrix2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/**
* 2x2 transformations
*/
export default class Matrix2 {
/**
* Returns a instance of z-axis rotation
* @param {number} rad - The rotation in radians
* @param {Matrix2} [target] - The target instance
* @returns {Matrix2}
*/
static Rotation(rad, target) {
const sin = Math.sin(rad);
const cos = Math.cos(rad);
const n = [
cos, sin,
-sin, cos
];
if (target === undefined) target = new Matrix2(n);
else target.n = n;
return target;
}
/**
* Returns a instance of scale vector
* @param {Vector2} v - The source
* @param {Matrix2} [target] - The target instance
* @returns {Matrix2}
*/
static Scale(v, target) {
const n = [
v.n[0], 0.0,
0.0, v.n[1]
];
if (target === undefined) target = new Matrix2(n);
else target.n = n;
return target;
}
/**
* Returns a new instance of axes (x, y)
* @param {Vector2} x - The x-axis vector
* @param {Vector2} [y] - The y-axis vector
* @param {Matrix2} [target] - The target instance
* @returns {Matrix2}
*/
static Vector2(x, y, target) {
const xn = x.n, yn = y !== undefined ? y.n : [-xn[1], xn[0]];
const n = [].concat(xn, yn);
if (target === undefined) target = new Matrix2(n);
else target.n = n;
return target;
}
/**
* Returns a new instance of converted m
* The instance will be cropped to 2x2 by removing the third row & column of m
* @param {Matrix3} m - The source
* @param {Matrix2} [target] - The target instance
* @returns {Matrix2}
*/
static Matrix3(m, target) {
const n = m.n.slice(0, 5);
n.splice(2, 1);
if (target === undefined) target = new Matrix2(n);
else target.n = n;
return target;
}
/**
* Returns the sum of a and b (a+b)
* @param {Matrix2} a - The first summand
* @param {Matrix2} b - The second summand
* @param {Matrix2} [target] - The target instance
* @returns {Matrix2}
*/
static Add(a, b, target) {
return (target === undefined ? new Matrix2() : target).add(a, b);
}
/**
* Returns the difference of a and b (a-b)
* @param {Matrix2} a - The minuend
* @param {Matrix2} b - The subtrahend
* @param {Matrix2} [target] - The target instance
* @returns {Matrix2}
*/
static Subtract(a, b, target) {
return (target === undefined ? new Matrix2() : target).subtract(a, b);
}
/**
* Returns the concatenation of a and b (a*b)
* @param {Matrix2} a - The first matrix
* @param {Matrix2} b - The second matrix
* @param {Matrix2} [target] - The target instance
* @returns {Matrix2}
*/
static Multiply(a, b, target) {
return (target === undefined ? new Matrix2() : target).multiply(a, b);
}
/**
* Returns the inverse of m
* Returns null if m is assumed to be singular, the new instance otherwise
* @param {Matrix2} m - The source
* @param {Matrix2} [target] - The target instance
* @returns {Matrix2|null}
*/
static Inverse(m, target) {
if (target === undefined) target = new Matrix2();
return target.inverseOf(m) ? target : null;
}
/**
* Returns the transpose of m
* @param {Matrix2} m - The source
* @param {Matrix2} [target] - The target instance
* @returns {Matrix2}
*/
static Transpose(m, target) {
return (target === undefined ? new Matrix2() : target).transposeOf(m);
}
/**
* Returns a copy of m
* @param {Matrix2} m - The source
* @param {Matrix2} [target] - The target instance
* @returns {Matrix2}
*/
static Copy(m, target) {
return (target === undefined ? new Matrix2() : target).copyOf(m);
}
/**
* Returns true if a and b are equal, false otherwise (a==b)
* @param {Matrix2} a - The first matrix
* @param {Matrix2} b - The second matrix
* @returns {boolean}
*/
static isEQ(a, b) {
if (a === b) return true;
const an = a.n, bn = b.n;
for (var i = 0; i < 4; i++) {
if (an[i] !== bn[i]) return false;
}
return true;
}
/**
* Creates a new instance
* @param {number[]} [n] - Array representing 2x2 column-major ordered components
* Arrays of length !== 4 will return the identity matrix
*/
constructor(n) {
/**
* The array representation
* The 4 column-major ordered components
* n[0]:n00 n[2]:n01
* n[1]:n10 n[3]:n11
* @type {number[]}
*/
this.n = (n && n.constructor === Array && n.length === 4 ? n : [1.0, 0.0, 0.0, 1.0]);
}
/**
* Redefines the instance
* @param {number[]} [n] - Array representing the 2x2 column-major ordered compoents
* Array of length !== 4 will return the identity matrix
* @returns {Matrix2}
*/
define(n) {
this.constructor.call(this, n);
return this;
}
/**
* row 0, col0, {@link Matrix2#n}[0]
* @type {number}
*/
get n00() {
return this.n[0];
}
set n00(n) {
this.n[0] = n;
}
/**
* row 0, col1, {@link Matrix2#n}[2]
* @type {number}
*/
get n01() {
return this.n[2];
}
set n01(n) {
this.n[2] = n;
}
/**
* row 1, col0, {@link Matrix2#n}[1]
* @type {number}
*/
get n10() {
return this.n[1];
}
set n10(n) {
this.n[1] = n;
}
/**
* row 1, col1, {@link Matrix2#n}[3]
* @type {number}
*/
get n11() {
return this.n[3];
}
set n11(n) {
this.n[3] = n;
}
/**
* The determinant
* @type {number}
*/
get determinant() {
return this.n[0] * this.n[3] - this.n[2] * this.n[1];
}
/**
* The sum of a and b (a+b)
* @param {Matrix2} a - The first summand
* @param {Matrix2} b - The second summand
* @returns {Matrix2}
*/
add(a, b) {
const n = this.n, an = a.n, bn = b.n;
for (var i = 0; i < 4; i++) n[i] = an[i] + bn[i];
return this;
}
/**
* The difference of a and b (a-b)
* @param {Matrix2} a - The minuend
* @param {Matrix2} b - The subtrahend
* @returns {Matrix2}
*/
subtract(a, b) {
const n = this.n, an = a.n, bn = b.n;
for (var i = 0; i < 4; i++) n[i] = an[i] + bn[i];
return this;
}
/**
* The concatenation of a and b (a*b)
* @param {Matrix2} a - The first transform
* @param {Matrix2} b - The second transform
* @returns {Matrix2}
*/
multiply(a, b) {
const n = this.n, an = a.n, bn = b.n;
const a00 = an[0], a01 = an[2];
const a10 = an[1], a11 = an[3];
const b00 = bn[0], b01 = bn[2];
const b10 = bn[1], b11 = bn[3];
n[0] = a00 * b00 + a01 * b10;
n[2] = a00 * b01 + a01 * b11;
n[1] = a10 * b00 + a11 * b10;
n[3] = a10 * b01 + a11 * b11;
return this;
}
/**
* The inverse of m
* Beware: method is NOT chainable
* Returns false if m is assumed to be singular, true otherwise
* @param {Matrix2} m - The source
* @returns {Boolean}
*/
inverseOf(m) {
const n = this.n, mn = m.n;
const m00 = mn[0], m01 = mn[2];
const m10 = mn[1], m11 = mn[3];
let d = m00 * m11 - m01 * m10;
if (Math.abs(d) < 1.0e-10) return false;
d = 1.0 / d;
n[0] = d * m11, n[2] = -d * m01;
n[1] = -d * m10, n[3] = d * m00;
return true;
}
/**
* The transpose of m
* @param {Matrix2} m - The source
* @returns {Matrix2}
*/
transposeOf(m) {
const mn2 = m.n[2];
this.n[2] = m.n[1];
this.n[1] = mn2;
return this;
}
/**
* The copy of m
* @param {Matrix2} m - The source
* @returns {Matrix2}
*/
copyOf(m) {
this.n = m.n.slice(0, 4);
return this;
}
/**
* The inverse of the instance
* Returns false if the instance is assumed to singular, true otherwise
* @returns {boolean}
*/
invert() {
return this.inverseOf(this);
}
/**
* The transpose of the instance
* @returns {Matrix2}
*/
transpose() {
return this.transposeOf(this);
}
/**
* Returns a string representation of the instance
* @param {int} [digits=3] - The decimal digits
* @returns {string}
*/
toString(digits = 3) {
const str = this.n
.map((item, index, source) => (index % 2.0 === 0.0 ? "\n" : "\t") + item.toFixed(digits))
.join("");
return `[Matrix2]${ str }`;
}
/**
* Returns the {@link Matrix2#determinant} of the instance
* @returns {number}
*/
valueOf() {
return this.determinant;
}
}