-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmat2x3.js
188 lines (168 loc) · 3.33 KB
/
mat2x3.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
/** @module mat2x3 */
/**
* Returns a 2x3 identity matrix, a short form for a 3x3 matrix with the last row ignored.
*
* Row major memory layout:
*
* ```
* 0 1
* 2 3
* 4 5
* ```
*
* Equivalent to the column major OpenGL spec:
*
* ```
* 0 3
* 1 4
* 2 5
*
* m00 m10
* m01 m11
* m02 m12
* ```
* @returns {import("./types.js").mat2x3}
*/
export function create() {
// prettier-ignore
return [
1, 0,
0, 1,
0, 0
]
}
/**
* Sets a matrix to the identity matrix.
* @param {import("./types.js").mat2x3} a
* @returns {import("./types.js").mat2x3}
*/
export function identity(a) {
a[0] = a[3] = 1;
a[1] = a[2] = a[4] = a[5] = 0;
return a;
}
/**
* Returns a copy of a matrix.
* @param {import("./types.js").mat2x3} a
* @returns {import("./types.js").mat2x3}
*/
export function copy(a) {
return a.slice();
}
/**
* Sets a matrix from another matrix.
* @param {import("./types.js").mat2x3} a
* @param {import("./types.js").mat2x3} b
* @returns {import("./types.js").mat2x3}
*/
export function set(a, b) {
a[0] = b[0];
a[1] = b[1];
a[2] = b[2];
a[3] = b[3];
a[4] = b[4];
a[5] = b[5];
return a;
}
/**
* Compares two matrices.
* @param {import("./types.js").mat2x3} a
* @param {import("./types.js").mat2x3} b
* @returns {boolean}
*/
export function equals(a, b) {
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]
);
}
/**
* Multiplies two matrices.
* @param {import("./types.js").mat2x3} a
* @param {import("./types.js").mat2x3} b
* @returns {import("./types.js").mat2x3}
*/
export function mult(a, b) {
const a0 = a[0];
const a1 = a[1];
const a2 = a[2];
const a3 = a[3];
const a4 = a[4];
const a5 = a[5];
const b0 = b[0];
const b1 = b[1];
const b2 = b[2];
const b3 = b[3];
const b4 = b[4];
const b5 = b[5];
a[0] = a0 * b0 + a2 * b1;
a[1] = a1 * b0 + a3 * b1;
a[2] = a0 * b2 + a2 * b3;
a[3] = a1 * b2 + a3 * b3;
a[4] = a0 * b4 + a2 * b5 + a4;
a[5] = a1 * b4 + a3 * b5 + a5;
return a;
}
/**
* Translates a matrix by a vector.
* @param {import("./types.js").mat2x3} a
* @param {import("./types.js").vec2} v
* @returns {import("./types.js").mat2x3}
*/
export function translate(a, v) {
const a0 = a[0];
const a1 = a[1];
const a2 = a[2];
const a3 = a[3];
const a4 = a[4];
const a5 = a[5];
const x = v[0];
const y = v[1];
a[0] = a0;
a[1] = a1;
a[2] = a2;
a[3] = a3;
a[4] = a0 * x + a2 * y + a4;
a[5] = a1 * x + a3 * y + a5;
return a;
}
/**
* Rotates a matrix by an angle.
* @param {import("./types.js").mat2x3} a
* @param {import("./types.js").Radians} rad
* @returns {import("./types.js").mat2x3}
*/
export function rotate(a, rad) {
const a0 = a[0];
const a1 = a[1];
const a2 = a[2];
const a3 = a[3];
const s = Math.sin(rad);
const c = Math.cos(rad);
a[0] = a0 * c + a2 * s;
a[1] = a1 * c + a3 * s;
a[2] = a0 * -s + a2 * c;
a[3] = a1 * -s + a3 * c;
return a;
}
/**
* Scales a matrix by a vector.
* @param {import("./types.js").mat2x3} a
* @param {import("./types.js").vec2} v
* @returns {import("./types.js").mat2x3}
*/
export function scale(a, v) {
const a0 = a[0];
const a1 = a[1];
const a2 = a[2];
const a3 = a[3];
a[0] = a0 * v[0];
a[1] = a1 * v[0];
a[2] = a2 * v[1];
a[3] = a3 * v[1];
return a;
}