Repository navigation
Expand file tree
/
Copy pathmatrix3.ts
More file actions
342 lines (307 loc) · 9.09 KB
/
Copy pathmatrix3.ts
File metadata and controls
342 lines (307 loc) · 9.09 KB
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
/*
Copyright (c) 2022 Alethea Katherine Flowers.
Published under the standard MIT License.
Full text available at: https://opensource.org/licenses/MIT
*/
import { Vec2 } from "./vec2";
import { Angle, type AngleLike } from "./angle";
type ElementArray = [
number,
number,
number,
number,
number,
number,
number,
number,
number,
];
/**
* A 3x3 transformation matrix
*/
export class Matrix3 {
elements: Float32Array;
/**
* Create a new Matrix
* @param elements the 9 matrix elements
*/
constructor(elements: ElementArray | Float32Array) {
if (elements.length != 9) {
throw new Error(`Matrix3 requires 9 elements, got ${elements}`);
}
this.elements = new Float32Array(elements);
}
/**
* Create a Matrix3 from a DOMMatrix
*/
static from_DOMMatrix(m: DOMMatrix): Matrix3 {
// prettier-ignore
return new Matrix3([
m.m11, m.m12, m.m14,
m.m21, m.m22, m.m24,
m.m41, m.m42, m.m44,
]);
}
/**
* Create a DOMMatrix from this Matrix3
*/
to_DOMMatrix(): DOMMatrix {
const e = this.elements;
// prettier-ignore
return new DOMMatrix([
e[0]!, e[3]!,
e[1]!, e[4]!,
e[6]!, e[7]!,
]);
}
/**
* Create a 4x4 DOMMatrix from this Matrix3
*/
to_4x4_DOMMatrix(): DOMMatrix {
const e = this.elements;
// prettier-ignore
return new DOMMatrix([
e[0]!, e[1]!, 0, e[2]!,
e[3]!, e[4]!, 0, e[5]!,
0, 0, 1, 0,
e[6]!, e[7]!, 0, 1
]);
}
/**
* @returns a new identity matrix
*/
static identity(): Matrix3 {
// prettier-ignore
return new Matrix3([
1, 0, 0,
0, 1, 0,
0, 0, 1,
]);
}
/**
* @returns a new matrix representing a 2D orthographic projection
*/
static orthographic(width: number, height: number): Matrix3 {
// prettier-ignore
return new Matrix3([
2 / width, 0, 0,
0, -2 / height, 0,
-1, 1, 1
]);
}
/**
* @returns a copy of this matrix
*/
copy() {
return new Matrix3(this.elements);
}
/**
* Update this matrix's elements
*/
set(elements: Float32Array | ElementArray) {
if (elements.length != 9) {
throw new Error(`Matrix3 requires 9 elements, got ${elements}`);
}
this.elements.set(elements);
}
/**
* Transform a vector by multiplying it with this matrix.
* @returns A new Vec2
*/
transform(vec: Vec2): Vec2 {
const x1 = this.elements[0 * 3 + 0]!;
const x2 = this.elements[0 * 3 + 1]!;
const y1 = this.elements[1 * 3 + 0]!;
const y2 = this.elements[1 * 3 + 1]!;
const z1 = this.elements[2 * 3 + 0]!;
const z2 = this.elements[2 * 3 + 1]!;
const px = vec.x;
const py = vec.y;
const x = px * x1 + py * y1 + z1;
const y = px * x2 + py * y2 + z2;
return new Vec2(x, y);
}
/**
* Transforms a list of vectors
* @yields new transformed vectors
*/
*transform_all(vecs: Iterable<Vec2>) {
for (const vec of vecs) {
yield this.transform(vec);
}
}
/**
* Transforms a list of vectors by a given matrix, which may be null.
*/
static transform_all(mat: Matrix3 | null, vecs: Vec2[]): Vec2[] {
if (!mat) {
return vecs;
}
return Array.from(mat.transform_all(vecs));
}
/**
* Multiply this matrix by another and store the result
* in this matrix.
* @returns this matrix
*/
multiply_self(b: Matrix3) {
const a00 = this.elements[0 * 3 + 0]!;
const a01 = this.elements[0 * 3 + 1]!;
const a02 = this.elements[0 * 3 + 2]!;
const a10 = this.elements[1 * 3 + 0]!;
const a11 = this.elements[1 * 3 + 1]!;
const a12 = this.elements[1 * 3 + 2]!;
const a20 = this.elements[2 * 3 + 0]!;
const a21 = this.elements[2 * 3 + 1]!;
const a22 = this.elements[2 * 3 + 2]!;
const b00 = b.elements[0 * 3 + 0]!;
const b01 = b.elements[0 * 3 + 1]!;
const b02 = b.elements[0 * 3 + 2]!;
const b10 = b.elements[1 * 3 + 0]!;
const b11 = b.elements[1 * 3 + 1]!;
const b12 = b.elements[1 * 3 + 2]!;
const b20 = b.elements[2 * 3 + 0]!;
const b21 = b.elements[2 * 3 + 1]!;
const b22 = b.elements[2 * 3 + 2]!;
this.elements[0] = b00 * a00 + b01 * a10 + b02 * a20;
this.elements[1] = b00 * a01 + b01 * a11 + b02 * a21;
this.elements[2] = b00 * a02 + b01 * a12 + b02 * a22;
this.elements[3] = b10 * a00 + b11 * a10 + b12 * a20;
this.elements[4] = b10 * a01 + b11 * a11 + b12 * a21;
this.elements[5] = b10 * a02 + b11 * a12 + b12 * a22;
this.elements[6] = b20 * a00 + b21 * a10 + b22 * a20;
this.elements[7] = b20 * a01 + b21 * a11 + b22 * a21;
this.elements[8] = b20 * a02 + b21 * a12 + b22 * a22;
return this;
}
/**
* Create a new matrix by multiplying this matrix with another
* @returns a new matrix
*/
multiply(b: Matrix3) {
return this.copy().multiply_self(b);
}
/**
* @returns A new matrix that is the inverse of this matrix
*/
inverse(): Matrix3 {
const a00 = this.elements[0 * 3 + 0]!;
const a01 = this.elements[0 * 3 + 1]!;
const a02 = this.elements[0 * 3 + 2]!;
const a10 = this.elements[1 * 3 + 0]!;
const a11 = this.elements[1 * 3 + 1]!;
const a12 = this.elements[1 * 3 + 2]!;
const a20 = this.elements[2 * 3 + 0]!;
const a21 = this.elements[2 * 3 + 1]!;
const a22 = this.elements[2 * 3 + 2]!;
const b01 = a22 * a11 - a12 * a21;
const b11 = -a22 * a10 + a12 * a20;
const b21 = a21 * a10 - a11 * a20;
const det = a00 * b01 + a01 * b11 + a02 * b21;
const inv_det = 1.0 / det;
return new Matrix3([
b01 * inv_det,
(-a22 * a01 + a02 * a21) * inv_det,
(a12 * a01 - a02 * a11) * inv_det,
b11 * inv_det,
(a22 * a00 - a02 * a20) * inv_det,
(-a12 * a00 + a02 * a10) * inv_det,
b21 * inv_det,
(-a21 * a00 + a01 * a20) * inv_det,
(a11 * a00 - a01 * a10) * inv_det,
]);
}
/**
* @returns A new matrix representing a 2D translation
*/
static translation(x: number, y: number): Matrix3 {
// prettier-ignore
return new Matrix3([
1, 0, 0,
0, 1, 0,
x, y, 1,
]);
}
/**
* Translate this matrix by the given amounts
* @returns this matrix
*/
translate_self(x: number, y: number) {
return this.multiply_self(Matrix3.translation(x, y));
}
/**
* Creates a new matrix representing this matrix translated by the given amount
* @returns a new matrix
*/
translate(x: number, y: number): Matrix3 {
return this.copy().translate_self(x, y);
}
/**
* @returns {Matrix3} A new matrix representing a 2D scale
*/
static scaling(x: number, y: number): Matrix3 {
// prettier-ignore
return new Matrix3([
x, 0, 0,
0, y, 0,
0, 0, 1,
]);
}
/**
* Scale this matrix by the given amounts
* @returns this matrix
*/
scale_self(x: number, y: number) {
return this.multiply_self(Matrix3.scaling(x, y));
}
/**
* Creates a new matrix representing this matrix scaled by the given amount
* @returns a new matrix
*/
scale(x: number, y: number): Matrix3 {
return this.copy().scale_self(x, y);
}
/**
* @returns A new matrix representing a 2D rotation
*/
static rotation(angle: AngleLike): Matrix3 {
const theta = new Angle(angle).radians;
const cos = Math.cos(theta);
const sin = Math.sin(theta);
// prettier-ignore
return new Matrix3([
cos, -sin, 0,
sin, cos, 0,
0, 0, 1
]);
}
/**
* Rotate this matrix by the given angle
* @returns this matrix
*/
rotate_self(angle: AngleLike) {
return this.multiply_self(Matrix3.rotation(angle));
}
/**
* Creates a new matrix representing this matrix rotated by the given angle
* @returns a new matrix
*/
rotate(angle: AngleLike) {
return this.copy().rotate_self(angle);
}
/**
* Returns the total translation (relative to identity) applied via this matrix.
*/
get absolute_translation() {
return this.transform(new Vec2(0, 0));
}
/**
* Retruns the total rotation (relative to identity) applied via this matrix.
*/
get absolute_rotation() {
const p0 = this.transform(new Vec2(0, 0));
const p1 = this.transform(new Vec2(1, 0));
const pn = p1.sub(p0);
return pn.angle.normalize();
}
}