Skip to content

Commit 4d59148

Browse files
committed
Updating all remaining classes for TS
1 parent ec41ff3 commit 4d59148

9 files changed

Lines changed: 438 additions & 1413 deletions

File tree

README.md

Lines changed: 0 additions & 949 deletions
Large diffs are not rendered by default.

src/Mat2.js renamed to src/Mat2.ts

Lines changed: 74 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,114 @@
1+
12
const identity = [
23
1, 0,
34
0, 1
45
];
56

6-
const identToIndex = function(v) {
7+
const identToIndex = function(v:string):number {
78
return ['a11', 'a12', 'a21', 'a22'].indexOf(v);
89
}
910

10-
const orDefault = function(v, ident) {
11+
const orDefault = function(v:any, ident:string):number {
1112
return isNaN(v) ? identity[identToIndex(ident)] : Number(v);
1213
}
1314

1415
class Mat2 {
15-
constructor(a11, a12, a21, a22) {
16+
constructor(...args:number[]) {
17+
const [a11, a12, a21, a22] = args;
1618
this.reset(a11, a12, a21, a22);
1719
}
1820

19-
reset(a11, a12, a21, a22) {
20-
if(a11 instanceof Array && a11.length >= 4) {
21-
this.a11 = orDefault(a11[0], 'a11');
22-
this.a12 = orDefault(a11[1], 'a12');
23-
this.a21 = orDefault(a11[2], 'a21');
24-
this.a22 = orDefault(a11[3], 'a22');
25-
} else {
26-
this.a11 = orDefault(a11, 'a11');
27-
this.a12 = orDefault(a12, 'a12');
28-
this.a21 = orDefault(a21, 'a21');
29-
this.a22 = orDefault(a22, 'a22');
30-
}
21+
reset(a11:number, a12:number, a21:number, a22:number):Mat2 {
22+
this.a11 = orDefault(a11, 'a11');
23+
this.a12 = orDefault(a12, 'a12');
24+
this.a21 = orDefault(a21, 'a21');
25+
this.a22 = orDefault(a22, 'a22');
26+
return this;
3127
}
3228

33-
resetToMat2(m) {
29+
resetToMat2(m:Mat2):Mat2 {
3430
this.a11 = m.a11;
3531
this.a12 = m.a12;
3632
this.a21 = m.a21;
3733
this.a22 = m.a22;
34+
return this;
3835
}
3936

40-
clone() {
37+
clone():Mat2 {
4138
return new Mat2(this.a11, this.a12, this.a21, this.a22);
4239
}
4340

44-
transpose() {
41+
transpose():Mat2 {
4542
const a12 = this.a12;
4643
this.a12 = this.a21;
4744
this.a21 = a12;
4845
return this;
4946
}
5047

51-
transposeNew() {
52-
return this.clone().transpose(m);
48+
transposeNew():Mat2 {
49+
return this.clone().transpose();
5350
}
5451

55-
add(m) {
56-
if(m instanceof Mat2) {
57-
this.a11 += m.a11;
58-
this.a12 += m.a12;
59-
this.a21 += m.a21;
60-
this.a22 += m.a22;
61-
}
52+
add(m:Mat2):Mat2 {
53+
this.a11 += m.a11;
54+
this.a12 += m.a12;
55+
this.a21 += m.a21;
56+
this.a22 += m.a22;
6257
return this;
6358
}
6459

65-
addNew(m) {
60+
addNew(m:Mat2):Mat2 {
6661
return this.clone().add(m);
6762
}
6863

6964
// @TODO: We might want to generalise this and allow any sort of matrix on these operations
7065

71-
subtract(m) {
72-
if(m instanceof Mat2) {
73-
this.a11 -= m.a11;
74-
this.a12 -= m.a12;
75-
this.a21 -= m.a21;
76-
this.a22 -= m.a22;
77-
}
66+
subtract(m:Mat2):Mat2 {
67+
this.a11 -= m.a11;
68+
this.a12 -= m.a12;
69+
this.a21 -= m.a21;
70+
this.a22 -= m.a22;
7871
return this;
7972
}
8073

81-
subtractNew(m) {
74+
subtractNew(m:Mat2):Mat2 {
8275
return this.clone().subtract(m);
8376
}
8477

85-
multiply(m) {
86-
if(m instanceof Mat2) {
87-
const o = this.clone();
88-
this.a11 = o.a11 * m.a11 + o.a21 * m.a12;
89-
this.a12 = o.a12 * m.a11 + o.a22 * m.a12;
90-
this.a21 = o.a11 * m.a21 + o.a21 * m.a22;
91-
this.a22 = o.a12 * m.a21 + o.a22 * m.a22;
92-
}
78+
multiply(m:Mat2):Mat2 {
79+
const o = this.clone();
80+
this.a11 = o.a11 * m.a11 + o.a21 * m.a12;
81+
this.a12 = o.a12 * m.a11 + o.a22 * m.a12;
82+
this.a21 = o.a11 * m.a21 + o.a21 * m.a22;
83+
this.a22 = o.a12 * m.a21 + o.a22 * m.a22;
9384
return this;
9485
}
9586

96-
multiplyNew(m) {
87+
multiplyNew(m:Mat2):Mat2 {
9788
return this.clone().multiply(m);
9889
}
9990

100-
multiplyScalar(s) {
91+
multiplyScalar(s:number):Mat2 {
10192
this.a11 *= s;
10293
this.a12 *= s;
10394
this.a21 *= s;
10495
this.a22 *= s;
10596
return this;
10697
}
10798

108-
multiplyScalarNew(s) {
99+
multiplyScalarNew(s:number):Mat2 {
109100
return this.clone().multiplyScalar(s);
110101
}
111102

112-
scale(s) {
103+
scale(s:number):Mat2 {
113104
return this.multiplyScalar(s);
114105
}
115106

116-
scaleNew(s) {
107+
scaleNew(s:number):Mat2 {
117108
return this.multiplyScalarNew(s);
118109
}
119110

120-
scaleByVec2(v) {
111+
scaleByVec2(v:any):Mat2 {
121112
if(v.array) v = v.array; // This just transforms a provided vector into to an array.
122113

123114
if(v instanceof Array) {
@@ -130,11 +121,11 @@ class Mat2 {
130121
return this;
131122
}
132123

133-
scaleByVec2New(v) {
124+
scaleByVec2New(v:any):Mat2 {
134125
return this.clone().scaleByVec2(v);
135126
}
136127

137-
rotate(r) {
128+
rotate(r:number):Mat2 {
138129
const o = this.clone();
139130
const s = Math.sin(r);
140131
const c = Math.cos(r);
@@ -145,11 +136,11 @@ class Mat2 {
145136
return this;
146137
}
147138

148-
rotateNew(r) {
139+
rotateNew(r:number):Mat2 {
149140
return this.clone().rotate(r);
150141
}
151142

152-
invert() {
143+
invert():Mat2 {
153144
const c = this.clone();
154145

155146
let det = this.determinant;
@@ -167,18 +158,16 @@ class Mat2 {
167158
return this;
168159
}
169160

170-
invertNew() {
161+
invertNew():Mat2 {
171162
return this.clone().invert();
172163
}
173164

174165
/**
175166
* Calculates the adjugate of a mat2
176167
*
177-
* @param {mat2} out the receiving matrix
178-
* @param {mat2} a the source matrix
179168
* @returns {mat2} out
180169
*/
181-
adjoint() {
170+
adjoint():Mat2 {
182171
const a11 = this.a11;
183172
this.a11 = this.a22;
184173
this.a12 = -this.a12;
@@ -188,11 +177,11 @@ class Mat2 {
188177
return this;
189178
}
190179

191-
adjointNew() {
192-
this.clone().adjoint();
180+
adjointNew():Mat2 {
181+
return this.clone().adjoint();
193182
}
194183

195-
toString() {
184+
toString():string {
196185
return `
197186
${this.a11}, ${this.a12},
198187
${this.a21}, ${this.a22}
@@ -209,15 +198,16 @@ class Mat2 {
209198
* @type {number}
210199
* @default 0
211200
*/
201+
#a11:number=0;
212202
set a11(v) {
213203
if(typeof v == 'number') {
214-
this._a11 = v;
204+
this.#a11 = v;
215205
} else {
216206
throw new TypeError('a11 should be a number');
217207
}
218208
}
219209
get a11() {
220-
return this._a11 || 0;
210+
return this.#a11 || 0;
221211
}
222212

223213
/**
@@ -226,15 +216,16 @@ class Mat2 {
226216
* @type {number}
227217
* @default 0
228218
*/
219+
#a12:number=0;
229220
set a12(v) {
230221
if(typeof v == 'number') {
231-
this._a12 = v;
222+
this.#a12 = v;
232223
} else {
233224
throw new TypeError('a12 should be a number');
234225
}
235226
}
236227
get a12() {
237-
return this._a12 || 0;
228+
return this.#a12 || 0;
238229
}
239230

240231
/**
@@ -243,15 +234,16 @@ class Mat2 {
243234
* @type {number}
244235
* @default 0
245236
*/
237+
#a21:number=0;
246238
set a21(v) {
247239
if(typeof v == 'number') {
248-
this._a21 = v;
240+
this.#a21 = v;
249241
} else {
250242
throw new TypeError('a21 should be a number');
251243
}
252244
}
253245
get a21() {
254-
return this._a21 || 0;
246+
return this.#a21 || 0;
255247
}
256248

257249
/**
@@ -260,18 +252,19 @@ class Mat2 {
260252
* @type {number}
261253
* @default 0
262254
*/
255+
#a22:number=0;
263256
set a22(v) {
264257
if(typeof v == 'number') {
265-
this._a22 = v;
258+
this.#a22 = v;
266259
} else {
267260
throw new TypeError('a22 should be a number');
268261
}
269262
}
270263
get a22() {
271-
return this._a22 || 0;
264+
return this.#a22 || 0;
272265
}
273266

274-
get determinant() {
267+
get determinant():number {
275268
return this.a11 * this.a21 - this.a21 * this.a12;
276269
}
277270

@@ -281,7 +274,7 @@ class Mat2 {
281274
*
282275
* @type {array}
283276
*/
284-
get array() {
277+
get array():number[] {
285278
return [
286279
this.a11, this.a12,
287280
this.a21, this.a22];
@@ -294,26 +287,31 @@ class Mat2 {
294287
*
295288
* @type {array}
296289
*/
297-
get columnArray() {
290+
get columnArray():number[] {
298291
return [
299292
this.a11, this.a21,
300293
this.a12, this.a22
301294
];
302295
}
303296

304-
static fromAngle(r) {
305-
let s = Math.sin(rad);
306-
let c = Math.cos(rad);
297+
static fromAngle(r:number):Mat2 {
298+
let s = Math.sin(r);
299+
let c = Math.cos(r);
307300

308301
return new Mat2(c, -s, s, c);
309302
}
310303

311-
static fromScalingVec2(v) {
304+
static fromScalingVec2(v:any):Mat2 {
312305
if(v.array) v = v.array; // This just transforms a provided vector into to an array.
313306

314307
if(v instanceof Array) {
315308
return new Mat2(v[0], 0, 0, v[1]);
316309
}
310+
return Mat2.identity();
311+
}
312+
313+
static identity():Mat2 {
314+
return new Mat2(...identity);
317315
}
318316
}
319317

0 commit comments

Comments
 (0)