bst-typed 1.49.4 → 1.49.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +1 -1
  2. package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -13
  3. package/dist/data-structures/binary-tree/binary-tree.js +19 -49
  4. package/dist/data-structures/binary-tree/tree-multimap.d.ts +0 -16
  5. package/dist/data-structures/binary-tree/tree-multimap.js +1 -43
  6. package/dist/data-structures/graph/abstract-graph.d.ts +1 -1
  7. package/dist/data-structures/graph/abstract-graph.js +3 -2
  8. package/dist/data-structures/hash/hash-map.d.ts +1 -1
  9. package/dist/data-structures/hash/hash-map.js +2 -2
  10. package/dist/data-structures/heap/heap.js +2 -3
  11. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  12. package/dist/data-structures/matrix/index.d.ts +0 -2
  13. package/dist/data-structures/matrix/index.js +0 -2
  14. package/dist/data-structures/matrix/matrix.d.ts +128 -10
  15. package/dist/data-structures/matrix/matrix.js +400 -15
  16. package/dist/data-structures/queue/deque.d.ts +2 -2
  17. package/dist/data-structures/queue/deque.js +5 -7
  18. package/dist/data-structures/queue/queue.d.ts +1 -1
  19. package/dist/types/data-structures/base/base.d.ts +1 -1
  20. package/dist/types/data-structures/heap/heap.d.ts +1 -1
  21. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  22. package/dist/utils/utils.d.ts +1 -0
  23. package/dist/utils/utils.js +6 -1
  24. package/package.json +2 -2
  25. package/src/data-structures/base/index.ts +1 -1
  26. package/src/data-structures/base/iterable-base.ts +7 -10
  27. package/src/data-structures/binary-tree/avl-tree.ts +15 -8
  28. package/src/data-structures/binary-tree/binary-tree.ts +57 -74
  29. package/src/data-structures/binary-tree/bst.ts +16 -13
  30. package/src/data-structures/binary-tree/rb-tree.ts +16 -10
  31. package/src/data-structures/binary-tree/tree-multimap.ts +11 -48
  32. package/src/data-structures/graph/abstract-graph.ts +13 -11
  33. package/src/data-structures/graph/directed-graph.ts +1 -3
  34. package/src/data-structures/graph/map-graph.ts +6 -1
  35. package/src/data-structures/graph/undirected-graph.ts +3 -6
  36. package/src/data-structures/hash/hash-map.ts +18 -16
  37. package/src/data-structures/heap/heap.ts +7 -10
  38. package/src/data-structures/heap/max-heap.ts +2 -1
  39. package/src/data-structures/heap/min-heap.ts +2 -1
  40. package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
  41. package/src/data-structures/matrix/index.ts +0 -2
  42. package/src/data-structures/matrix/matrix.ts +442 -13
  43. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
  44. package/src/data-structures/queue/deque.ts +18 -39
  45. package/src/data-structures/queue/queue.ts +1 -1
  46. package/src/interfaces/binary-tree.ts +7 -2
  47. package/src/types/common.ts +4 -4
  48. package/src/types/data-structures/base/base.ts +14 -3
  49. package/src/types/data-structures/base/index.ts +1 -1
  50. package/src/types/data-structures/graph/abstract-graph.ts +4 -2
  51. package/src/types/data-structures/hash/hash-map.ts +3 -3
  52. package/src/types/data-structures/heap/heap.ts +2 -2
  53. package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
  54. package/src/utils/utils.ts +7 -1
  55. package/dist/data-structures/matrix/matrix2d.d.ts +0 -107
  56. package/dist/data-structures/matrix/matrix2d.js +0 -199
  57. package/dist/data-structures/matrix/vector2d.d.ts +0 -200
  58. package/dist/data-structures/matrix/vector2d.js +0 -290
  59. package/src/data-structures/matrix/matrix2d.ts +0 -211
  60. package/src/data-structures/matrix/vector2d.ts +0 -315
@@ -1,290 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Vector2D = void 0;
4
- /**
5
- * data-structure-typed
6
- *
7
- * @author Tyler Zeng
8
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
9
- * @license MIT License
10
- */
11
- class Vector2D {
12
- constructor(x = 0, y = 0, w = 1 // needed for matrix multiplication
13
- ) {
14
- this.x = x;
15
- this.y = y;
16
- this.w = w;
17
- }
18
- /**
19
- * The function checks if the x and y values of a point are both zero.
20
- * @returns A boolean value indicating whether both the x and y properties of the object are equal to 0.
21
- */
22
- get isZero() {
23
- return this.x === 0 && this.y === 0;
24
- }
25
- /**
26
- * The above function calculates the length of a vector using the Pythagorean theorem.
27
- * @returns The length of a vector, calculated using the Pythagorean theorem.
28
- */
29
- get length() {
30
- return Math.sqrt(this.x * this.x + this.y * this.y);
31
- }
32
- /**
33
- * The function calculates the square of the length of a vector.
34
- * @returns The method is returning the sum of the squares of the x and y values.
35
- */
36
- get lengthSq() {
37
- return this.x * this.x + this.y * this.y;
38
- }
39
- /**
40
- * The "rounded" function returns a new Vector2D object with the x and y values rounded to the nearest whole number.
41
- * @returns The method is returning a new instance of the Vector2D class with the x and y values rounded to the nearest
42
- * whole number.
43
- */
44
- get rounded() {
45
- return new Vector2D(Math.round(this.x), Math.round(this.y));
46
- }
47
- /**
48
- * The function "add" takes two Vector2D objects as parameters and returns a new Vector2D object with the sum of their
49
- * x and y components.
50
- * @param {Vector2D} vector1 - The parameter `vector1` is an instance of the `Vector2D` class. It represents a
51
- * 2-dimensional vector with an `x` and `y` component.
52
- * @param {Vector2D} vector2 - The parameter "vector2" is of type Vector2D. It represents a 2-dimensional vector with
53
- * an x and y component.
54
- * @returns The method is returning a new instance of the Vector2D class with the x and y components of the two input
55
- * vectors added together.
56
- */
57
- static add(vector1, vector2) {
58
- return new Vector2D(vector1.x + vector2.x, vector1.y + vector2.y);
59
- }
60
- /**
61
- * The subtract function takes two Vector2D objects as parameters and returns a new Vector2D object with the x and y
62
- * components subtracted.
63
- * @param {Vector2D} vector1 - The parameter `vector1` is an instance of the `Vector2D` class, representing a
64
- * 2-dimensional vector. It has properties `x` and `y` which represent the x and y components of the vector
65
- * respectively.
66
- * @param {Vector2D} vector2 - The parameter "vector2" is a Vector2D object. It represents the second vector that you
67
- * want to subtract from the first vector.
68
- * @returns The method is returning a new Vector2D object with the x and y components subtracted from vector1 and
69
- * vector2.
70
- */
71
- static subtract(vector1, vector2) {
72
- return new Vector2D(vector1.x - vector2.x, vector1.y - vector2.y);
73
- }
74
- /**
75
- * The function subtracts a given value from the x and y components of a Vector2D object and returns a new Vector2D
76
- * object.
77
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
78
- * x and y components.
79
- * @param {number} value - The "value" parameter is a number that will be subtracted from both the x and y components
80
- * of the "vector" parameter.
81
- * @returns A new Vector2D object with the x and y values subtracted by the given value.
82
- */
83
- static subtractValue(vector, value) {
84
- return new Vector2D(vector.x - value, vector.y - value);
85
- }
86
- /**
87
- * The function multiplies a Vector2D object by a given value.
88
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
89
- * x and y components.
90
- * @param {number} value - The "value" parameter is a number that represents the value by which the x and y components
91
- * of the vector will be multiplied.
92
- * @returns A new Vector2D object with the x and y values multiplied by the given value.
93
- */
94
- static multiply(vector, value) {
95
- return new Vector2D(vector.x * value, vector.y * value);
96
- }
97
- /**
98
- * The function divides the x and y components of a Vector2D by a given value and returns a new Vector2D.
99
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
100
- * x and y components.
101
- * @param {number} value - The value parameter is a number that will be used to divide the x and y components of the
102
- * vector.
103
- * @returns A new instance of the Vector2D class with the x and y values divided by the given value.
104
- */
105
- static divide(vector, value) {
106
- return new Vector2D(vector.x / value, vector.y / value);
107
- }
108
- /**
109
- * The function checks if two Vector2D objects are equal by comparing their x and y values.
110
- * @param {Vector2D} vector1 - The parameter `vector1` is of type `Vector2D`, which represents a 2-dimensional vector.
111
- * It has two properties: `x` and `y`, which represent the x and y components of the vector, respectively.
112
- * @param {Vector2D} vector2 - The parameter "vector2" is of type Vector2D.
113
- * @returns a boolean value, which indicates whether the two input vectors are equal or not.
114
- */
115
- static equals(vector1, vector2) {
116
- return vector1.x === vector2.x && vector1.y === vector2.y;
117
- }
118
- /**
119
- * The function checks if two Vector2D objects are equal within a specified rounding factor.
120
- * @param {Vector2D} vector1 - The first vector to compare.
121
- * @param {Vector2D} vector2 - The parameter "vector2" is a Vector2D object, which represents a 2-dimensional vector.
122
- * It is used as one of the inputs for the "equalsRounded" function.
123
- * @param [roundingFactor=12] - The roundingFactor parameter is used to determine the threshold for considering two
124
- * vectors as equal. If the absolute difference in the x and y components of the vectors is less than the
125
- * roundingFactor, the vectors are considered equal.
126
- * @returns a boolean value.
127
- */
128
- static equalsRounded(vector1, vector2, roundingFactor = 12) {
129
- const vector = Vector2D.abs(Vector2D.subtract(vector1, vector2));
130
- if (vector.x < roundingFactor && vector.y < roundingFactor) {
131
- return true;
132
- }
133
- return false;
134
- }
135
- /**
136
- * The normalize function takes a vector as input and returns a normalized version of the vector.Normalizes the vector if it matches a certain condition
137
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D.
138
- * @returns the normalized vector if its length is greater than a very small value (epsilon), otherwise it returns the
139
- * original vector.
140
- */
141
- static normalize(vector) {
142
- const length = vector.length;
143
- if (length > 2.220446049250313e-16) {
144
- // Epsilon
145
- return Vector2D.divide(vector, length);
146
- }
147
- return vector;
148
- }
149
- /**
150
- * The function truncates a vector to a maximum length if it exceeds that length.Adjusts x and y so that the length of the vector does not exceed max
151
- * @param {Vector2D} vector - A 2D vector represented by the Vector2D class.
152
- * @param {number} max - The `max` parameter is a number that represents the maximum length that the `vector` should
153
- * have.
154
- * @returns either the original vector or a truncated version of the vector, depending on whether the length of the
155
- * vector is greater than the maximum value specified.
156
- */
157
- static truncate(vector, max) {
158
- if (vector.length > max) {
159
- return Vector2D.multiply(Vector2D.normalize(vector), max);
160
- }
161
- return vector;
162
- }
163
- /**
164
- * The function returns a new Vector2D object that is perpendicular to the input vector.The vector that is perpendicular to this one
165
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D.
166
- * @returns A new Vector2D object is being returned.
167
- */
168
- static perp(vector) {
169
- return new Vector2D(-vector.y, vector.x);
170
- }
171
- /**
172
- * The reverse function takes a Vector2D object and returns a new Vector2D object with the negated x and y values.
173
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector. It
174
- * has two properties: "x" and "y", which represent the x and y components of the vector, respectively.
175
- * @returns A new Vector2D object with the negated x and y values of the input vector. Returns the vector that is the reverse of this vector
176
- */
177
- static reverse(vector) {
178
- return new Vector2D(-vector.x, -vector.y);
179
- }
180
- /**
181
- * The function takes a Vector2D object as input and returns a new Vector2D object with the absolute values of its x
182
- * and y components.
183
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector. It
184
- * has two properties: "x" and "y", which represent the x and y components of the vector, respectively.
185
- * @returns The method is returning a new Vector2D object with the absolute values of the x and y components of the
186
- * input vector.
187
- */
188
- static abs(vector) {
189
- return new Vector2D(Math.abs(vector.x), Math.abs(vector.y));
190
- }
191
- /**
192
- * The dot function calculates the dot product of two 2D vectors.The dot product of v1 and v2
193
- * @param {Vector2D} vector1 - The parameter `vector1` represents a 2D vector with its x and y components.
194
- * @param {Vector2D} vector2 - The "vector2" parameter is a Vector2D object. It represents a two-dimensional vector
195
- * with an x and y component.
196
- * @returns The dot product of the two input vectors.
197
- */
198
- static dot(vector1, vector2) {
199
- return vector1.x * vector2.x + vector1.y * vector2.y;
200
- }
201
- // /**
202
- // * Transform vectors based on the current tranformation matrices: translation, rotation and scale
203
- // * @param vectors The vectors to transform
204
- // */
205
- // static transform(vector: Vector2D, transformation: Matrix2D): Vector2D {
206
- // return Matrix2D.multiplyByVector(transformation, vector)
207
- // }
208
- // /**
209
- // * Transform vectors based on the current tranformation matrices: translation, rotation and scale
210
- // * @param vectors The vectors to transform
211
- // */
212
- // static transformList(vectors: Vector2D[], transformation: Matrix2D): Vector2D[] {
213
- // return vectors.map(vector => Matrix2D.multiplyByVector(transformation, vector))
214
- // }
215
- /**
216
- * The function calculates the distance between two points in a two-dimensional space.
217
- * @param {Vector2D} vector1 - The parameter `vector1` represents the first vector in 2D space, while `vector2`
218
- * represents the second vector. Each vector has an `x` and `y` component, which represent their respective coordinates
219
- * in the 2D space.
220
- * @param {Vector2D} vector2 - The `vector2` parameter represents the second vector in the calculation of distance. It
221
- * is an instance of the `Vector2D` class, which typically has properties `x` and `y` representing the coordinates of
222
- * the vector in a 2D space.
223
- * @returns The distance between vector1 and vector2.
224
- */
225
- static distance(vector1, vector2) {
226
- const ySeparation = vector2.y - vector1.y;
227
- const xSeparation = vector2.x - vector1.x;
228
- return Math.sqrt(ySeparation * ySeparation + xSeparation * xSeparation);
229
- }
230
- /**
231
- * The function calculates the squared distance between two 2D vectors.
232
- * @param {Vector2D} vector1 - The parameter `vector1` represents the first vector, which is an instance of the
233
- * `Vector2D` class. It contains the x and y coordinates of the vector.
234
- * @param {Vector2D} vector2 - The `vector2` parameter represents the second vector in a two-dimensional space. It has
235
- * properties `x` and `y` which represent the coordinates of the vector.
236
- * @returns the square of the distance between the two input vectors.
237
- */
238
- static distanceSq(vector1, vector2) {
239
- const ySeparation = vector2.y - vector1.y;
240
- const xSeparation = vector2.x - vector1.x;
241
- return ySeparation * ySeparation + xSeparation * xSeparation;
242
- }
243
- /**
244
- * The sign function determines the sign of the cross product between two 2D vectors.
245
- * (assuming the Y axis is pointing down, X axis to right like a Window app)
246
- * @param {Vector2D} vector1 - The parameter `vector1` is of type `Vector2D`, which represents a 2-dimensional vector.
247
- * It likely has properties `x` and `y` representing the x and y components of the vector, respectively.
248
- * @param {Vector2D} vector2 - The above code defines a function called "sign" that takes two parameters: vector1 and
249
- * vector2. Both vector1 and vector2 are of type Vector2D.
250
- * @returns either -1 or 1. Returns positive if v2 is clockwise of this vector, negative if counterclockwise
251
- */
252
- static sign(vector1, vector2) {
253
- if (vector1.y * vector2.x > vector1.x * vector2.y) {
254
- return -1;
255
- }
256
- return 1;
257
- }
258
- /**
259
- * The function calculates the angle between a given vector and the negative y-axis.
260
- * @param {Vector2D} vector - The "vector" parameter is an instance of the Vector2D class, which represents a
261
- * 2-dimensional vector. It has two properties: "x" and "y", which represent the x and y components of the vector,
262
- * respectively.
263
- * @returns the angle between the given vector and the vector (0, -1) in radians.Returns the angle between origin and the given vector in radians
264
- */
265
- static angle(vector) {
266
- const origin = new Vector2D(0, -1);
267
- const radian = Math.acos(Vector2D.dot(vector, origin) / (vector.length * origin.length));
268
- return Vector2D.sign(vector, origin) === 1 ? Math.PI * 2 - radian : radian;
269
- }
270
- /**
271
- * The function "random" generates a random Vector2D object with x and y values within the specified range.
272
- * @param {number} maxX - The maxX parameter represents the maximum value for the x-coordinate of the random vector.
273
- * @param {number} maxY - The `maxY` parameter represents the maximum value for the y-coordinate of the generated
274
- * random vector.
275
- * @returns a new instance of the Vector2D class with random x and y values.
276
- */
277
- static random(maxX, maxY) {
278
- const randX = Math.floor(Math.random() * maxX - maxX / 2);
279
- const randY = Math.floor(Math.random() * maxY - maxY / 2);
280
- return new Vector2D(randX, randY);
281
- }
282
- /**
283
- * The function sets the values of x and y to zero.
284
- */
285
- zero() {
286
- this.x = 0;
287
- this.y = 0;
288
- }
289
- }
290
- exports.Vector2D = Vector2D;
@@ -1,211 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import { Vector2D } from './vector2d';
9
-
10
- export class Matrix2D {
11
- protected readonly _matrix: number[][];
12
-
13
- /**
14
- * The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
15
- * or Vector2D object.
16
- * @param {number[][] | Vector2D} [value] - The `value` parameter can be either a 2D array of numbers (`number[][]`) or
17
- * an instance of the `Vector2D` class.
18
- */
19
- constructor(value?: number[][] | Vector2D) {
20
- if (typeof value === 'undefined') {
21
- this._matrix = Matrix2D.identity;
22
- } else if (value instanceof Vector2D) {
23
- this._matrix = Matrix2D.identity;
24
- this._matrix[0][0] = value.x;
25
- this._matrix[1][0] = value.y;
26
- this._matrix[2][0] = value.w;
27
- } else {
28
- this._matrix = value;
29
- }
30
- }
31
-
32
- /**
33
- * The function returns a 2D array with three empty arrays.
34
- * @returns An empty 2-dimensional array with 3 empty arrays inside.
35
- */
36
- static get empty(): number[][] {
37
- return [[], [], []];
38
- }
39
-
40
- /**
41
- * The above function returns a 3x3 identity matrix.
42
- * @returns The method is returning a 2-dimensional array of numbers representing the identity matrix.
43
- */
44
- static get identity(): number[][] {
45
- return [
46
- [1, 0, 0],
47
- [0, 1, 0],
48
- [0, 0, 1]
49
- ];
50
- }
51
-
52
- /**
53
- * The function returns a two-dimensional array of numbers.
54
- * @returns The getter method is returning the value of the private variable `_matrix`, which is a two-dimensional
55
- * array of numbers.
56
- */
57
- get m(): number[][] {
58
- return this._matrix;
59
- }
60
-
61
- /**
62
- * The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
63
- * @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
64
- * @param {Matrix2D} matrix2 - The parameter `matrix2` is a Matrix2D object.
65
- * @returns a new instance of the Matrix2D class, which is created using the result array.
66
- */
67
- static add(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D {
68
- const result = Matrix2D.empty;
69
- for (let i = 0; i < 3; i++) {
70
- for (let j = 0; j < 3; j++) {
71
- result[i][j] = matrix1.m[i][j] + matrix2.m[i][j];
72
- }
73
- }
74
- return new Matrix2D(result);
75
- }
76
-
77
- /**
78
- * The function subtracts two 2D matrices and returns the result as a new Matrix2D object.
79
- * @param {Matrix2D} matrix1 - Matrix2D - The first matrix to subtract from.
80
- * @param {Matrix2D} matrix2 - Matrix2D is a class representing a 2D matrix. It has a property `m` which is a 2D array
81
- * representing the matrix elements.
82
- * @returns a new instance of the Matrix2D class, which is created using the result array.
83
- */
84
- static subtract(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D {
85
- const result = Matrix2D.empty;
86
- for (let i = 0; i < 3; i++) {
87
- for (let j = 0; j < 3; j++) {
88
- result[i][j] = matrix1.m[i][j] - matrix2.m[i][j];
89
- }
90
- }
91
- return new Matrix2D(result);
92
- }
93
-
94
- /**
95
- * The function multiplies two 2D matrices and returns the result as a new Matrix2D object.
96
- * @param {Matrix2D} matrix1 - A 2D matrix represented by the Matrix2D class.
97
- * @param {Matrix2D} matrix2 - The parameter `matrix2` is a 2D matrix of size 3x3.
98
- * @returns a new instance of the Matrix2D class, created using the result array.
99
- */
100
- static multiply(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D {
101
- const result = Matrix2D.empty;
102
- for (let i = 0; i < 3; i++) {
103
- for (let j = 0; j < 3; j++) {
104
- result[i][j] = 0;
105
- for (let k = 0; k < 3; k++) {
106
- result[i][j] += matrix1.m[i][k] * matrix2.m[k][j];
107
- }
108
- }
109
- }
110
- return new Matrix2D(result);
111
- }
112
-
113
- /**
114
- * The function multiplies each element of a 2D matrix by a given value and returns the resulting matrix.
115
- * @param {Matrix2D} matrix - The `matrix` parameter is an instance of the `Matrix2D` class, which represents a 2D
116
- * matrix. It contains a property `m` that is a 2D array representing the matrix elements.
117
- * @param {number} value - The `value` parameter is a number that you want to multiply each element of the `matrix` by.
118
- * @returns a new instance of the Matrix2D class, which is created using the result array.
119
- */
120
- static multiplyByValue(matrix: Matrix2D, value: number): Matrix2D {
121
- const result = Matrix2D.empty;
122
- for (let i = 0; i < 3; i++) {
123
- for (let j = 0; j < 3; j++) {
124
- result[i][j] = matrix.m[i][j] * value;
125
- }
126
- }
127
- return new Matrix2D(result);
128
- }
129
-
130
- /**
131
- * The function multiplies a 2D matrix by a 2D vector and returns the result as a 2D vector.
132
- * @param {Matrix2D} matrix - The parameter "matrix" is of type Matrix2D. It represents a 2-dimensional matrix.
133
- * @param {Vector2D} vector - The "vector" parameter is a 2D vector, represented by an object of type Vector2D.
134
- * @returns a Vector2D.
135
- */
136
- static multiplyByVector(matrix: Matrix2D, vector: Vector2D): Vector2D {
137
- const resultMatrix = Matrix2D.multiply(matrix, new Matrix2D(vector));
138
- return resultMatrix.toVector();
139
- }
140
-
141
- /**
142
- * The function returns a 2D matrix that scales and flips a vector around the center of a given width and height.
143
- * @param {number} width - The width parameter represents the width of the view or the canvas. It is a number that
144
- * specifies the width in pixels or any other unit of measurement.
145
- * @param {number} height - The height parameter represents the height of the view or the canvas. It is used to
146
- * calculate the centerY value, which is the vertical center of the view.
147
- * @returns a Matrix2D object.
148
- */
149
- static view(width: number, height: number): Matrix2D {
150
- const scaleStep = 1; // Scale every vector * scaleStep
151
- const centerX = width / 2;
152
- const centerY = height / 2;
153
- const flipX = Math.cos(Math.PI); // rotate 180deg / 3.14radian around X-axis
154
-
155
- return new Matrix2D([
156
- [scaleStep, 0, centerX],
157
- [0, flipX * scaleStep, centerY],
158
- [0, 0, 1]
159
- ]);
160
- }
161
-
162
- /**
163
- * The function scales a matrix by a given factor.
164
- * @param {number} factor - The factor parameter is a number that represents the scaling factor by which the matrix
165
- * should be scaled.
166
- * @returns the result of multiplying a new instance of Matrix2D by the given factor.
167
- */
168
- static scale(factor: number) {
169
- return Matrix2D.multiplyByValue(new Matrix2D(), factor);
170
- }
171
-
172
- /**
173
- * The function "rotate" takes an angle in radians and returns a 2D transformation matrix for rotating objects.
174
- * @param {number} radians - The "radians" parameter is the angle in radians by which you want to rotate an object.
175
- * @returns The code is returning a new instance of a Matrix2D object.
176
- */
177
- static rotate(radians: number) {
178
- const cos = Math.cos(radians);
179
- const sin = Math.sin(radians);
180
-
181
- return new Matrix2D([
182
- [cos, -sin, 0],
183
- [sin, cos, 0],
184
- [0, 0, 1]
185
- ]);
186
- }
187
-
188
- /**
189
- * The translate function takes a 2D vector and returns a 2D matrix that represents a translation transformation.
190
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D. It represents a 2D vector with components x
191
- * and y, and an optional w component.
192
- * @returns The method is returning a new instance of the Matrix2D class.
193
- */
194
- static translate(vector: Vector2D): Matrix2D {
195
- return new Matrix2D([
196
- [1, 0, vector.x],
197
- [0, 1, vector.y],
198
- [0, 0, vector.w]
199
- ]);
200
- }
201
-
202
- /**
203
- * The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
204
- * _matrix array.
205
- * @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
206
- * the first column of the matrix.
207
- */
208
- toVector(): Vector2D {
209
- return new Vector2D(this._matrix[0][0], this._matrix[1][0]);
210
- }
211
- }