heap-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.
- package/dist/data-structures/base/iterable-base.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -13
- package/dist/data-structures/binary-tree/binary-tree.js +19 -49
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +0 -16
- package/dist/data-structures/binary-tree/tree-multimap.js +1 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/data-structures/graph/abstract-graph.js +3 -2
- package/dist/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/data-structures/hash/hash-map.js +2 -2
- package/dist/data-structures/heap/heap.js +2 -3
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/matrix/index.d.ts +0 -2
- package/dist/data-structures/matrix/index.js +0 -2
- package/dist/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/data-structures/matrix/matrix.js +400 -15
- package/dist/data-structures/queue/deque.d.ts +2 -2
- package/dist/data-structures/queue/deque.js +5 -7
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/utils/utils.d.ts +1 -0
- package/dist/utils/utils.js +6 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -1
- package/src/data-structures/base/iterable-base.ts +7 -10
- package/src/data-structures/binary-tree/avl-tree.ts +15 -8
- package/src/data-structures/binary-tree/binary-tree.ts +57 -74
- package/src/data-structures/binary-tree/bst.ts +16 -13
- package/src/data-structures/binary-tree/rb-tree.ts +16 -10
- package/src/data-structures/binary-tree/tree-multimap.ts +11 -48
- package/src/data-structures/graph/abstract-graph.ts +13 -11
- package/src/data-structures/graph/directed-graph.ts +1 -3
- package/src/data-structures/graph/map-graph.ts +6 -1
- package/src/data-structures/graph/undirected-graph.ts +3 -6
- package/src/data-structures/hash/hash-map.ts +18 -16
- package/src/data-structures/heap/heap.ts +7 -10
- package/src/data-structures/heap/max-heap.ts +2 -1
- package/src/data-structures/heap/min-heap.ts +2 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
- package/src/data-structures/matrix/index.ts +0 -2
- package/src/data-structures/matrix/matrix.ts +442 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
- package/src/data-structures/queue/deque.ts +18 -39
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +7 -2
- package/src/types/common.ts +4 -4
- package/src/types/data-structures/base/base.ts +14 -3
- package/src/types/data-structures/base/index.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +4 -2
- package/src/types/data-structures/hash/hash-map.ts +3 -3
- package/src/types/data-structures/heap/heap.ts +2 -2
- package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/utils/utils.ts +7 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/data-structures/matrix/matrix2d.js +0 -199
- package/dist/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/data-structures/matrix/vector2d.js +0 -290
- package/src/data-structures/matrix/matrix2d.ts +0 -211
- package/src/data-structures/matrix/vector2d.ts +0 -315
|
@@ -1,6 +1,17 @@
|
|
|
1
|
-
import { IterableElementBase, IterableEntryBase } from
|
|
1
|
+
import { IterableElementBase, IterableEntryBase } from '../../../data-structures';
|
|
2
2
|
|
|
3
3
|
export type EntryCallback<K, V, R> = (value: V, key: K, index: number, container: IterableEntryBase<K, V>) => R;
|
|
4
4
|
export type ElementCallback<V, R> = (element: V, index: number, container: IterableElementBase<V>) => R;
|
|
5
|
-
export type ReduceEntryCallback<K, V, R> = (
|
|
6
|
-
|
|
5
|
+
export type ReduceEntryCallback<K, V, R> = (
|
|
6
|
+
accumulator: R,
|
|
7
|
+
value: V,
|
|
8
|
+
key: K,
|
|
9
|
+
index: number,
|
|
10
|
+
container: IterableEntryBase<K, V>
|
|
11
|
+
) => R;
|
|
12
|
+
export type ReduceElementCallback<V, R> = (
|
|
13
|
+
accumulator: R,
|
|
14
|
+
element: V,
|
|
15
|
+
index: number,
|
|
16
|
+
container: IterableElementBase<V>
|
|
17
|
+
) => R;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './base';
|
|
1
|
+
export * from './base';
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export type VertexKey = string | number;
|
|
2
2
|
|
|
3
|
-
export type DijkstraResult<V> =
|
|
3
|
+
export type DijkstraResult<V> =
|
|
4
|
+
| {
|
|
4
5
|
distMap: Map<V, number>;
|
|
5
6
|
distPaths?: Map<V, V[]>;
|
|
6
7
|
preMap: Map<V, V | undefined>;
|
|
@@ -8,4 +9,5 @@ export type DijkstraResult<V> = {
|
|
|
8
9
|
paths: V[][];
|
|
9
10
|
minDist: number;
|
|
10
11
|
minPath: V[];
|
|
11
|
-
}
|
|
12
|
+
}
|
|
13
|
+
| undefined;
|
|
@@ -7,7 +7,7 @@ export type HashMapLinkedNode<K, V> = {
|
|
|
7
7
|
|
|
8
8
|
export type HashMapOptions<K> = {
|
|
9
9
|
hashFn: (key: K) => string;
|
|
10
|
-
objHashFn: (key: K) => object
|
|
11
|
-
}
|
|
10
|
+
objHashFn: (key: K) => object;
|
|
11
|
+
};
|
|
12
12
|
|
|
13
|
-
export type HashMapStoreItem<K, V> = { key: K
|
|
13
|
+
export type HashMapStoreItem<K, V> = { key: K; value: V };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { Comparator } from
|
|
1
|
+
import { Comparator } from '../../common';
|
|
2
2
|
|
|
3
|
-
export type HeapOptions<T> = { comparator: Comparator<T> }
|
|
3
|
+
export type HeapOptions<T> = { comparator: Comparator<T> };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { HeapOptions } from
|
|
1
|
+
import { HeapOptions } from '../heap';
|
|
2
2
|
|
|
3
|
-
export type PriorityQueueOptions<T> = HeapOptions<T> & {}
|
|
3
|
+
export type PriorityQueueOptions<T> = HeapOptions<T> & {};
|
package/src/utils/utils.ts
CHANGED
|
@@ -98,4 +98,10 @@ export const isWeakKey = (input: unknown): input is object => {
|
|
|
98
98
|
return (inputType === 'object' && input !== null) || inputType === 'function';
|
|
99
99
|
};
|
|
100
100
|
|
|
101
|
-
export const calcMinUnitsRequired = (totalQuantity: number, unitSize: number) =>
|
|
101
|
+
export const calcMinUnitsRequired = (totalQuantity: number, unitSize: number) =>
|
|
102
|
+
Math.floor((totalQuantity + unitSize - 1) / unitSize);
|
|
103
|
+
|
|
104
|
+
export const roundFixed = (num: number, digit: number = 10) => {
|
|
105
|
+
const multiplier = Math.pow(10, digit);
|
|
106
|
+
return Math.round(num * multiplier) / multiplier;
|
|
107
|
+
};
|
|
@@ -1,107 +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
|
-
export declare class Matrix2D {
|
|
10
|
-
protected readonly _matrix: number[][];
|
|
11
|
-
/**
|
|
12
|
-
* The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
|
|
13
|
-
* or Vector2D object.
|
|
14
|
-
* @param {number[][] | Vector2D} [value] - The `value` parameter can be either a 2D array of numbers (`number[][]`) or
|
|
15
|
-
* an instance of the `Vector2D` class.
|
|
16
|
-
*/
|
|
17
|
-
constructor(value?: number[][] | Vector2D);
|
|
18
|
-
/**
|
|
19
|
-
* The function returns a 2D array with three empty arrays.
|
|
20
|
-
* @returns An empty 2-dimensional array with 3 empty arrays inside.
|
|
21
|
-
*/
|
|
22
|
-
static get empty(): number[][];
|
|
23
|
-
/**
|
|
24
|
-
* The above function returns a 3x3 identity matrix.
|
|
25
|
-
* @returns The method is returning a 2-dimensional array of numbers representing the identity matrix.
|
|
26
|
-
*/
|
|
27
|
-
static get identity(): number[][];
|
|
28
|
-
/**
|
|
29
|
-
* The function returns a two-dimensional array of numbers.
|
|
30
|
-
* @returns The getter method is returning the value of the private variable `_matrix`, which is a two-dimensional
|
|
31
|
-
* array of numbers.
|
|
32
|
-
*/
|
|
33
|
-
get m(): number[][];
|
|
34
|
-
/**
|
|
35
|
-
* The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
|
|
36
|
-
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
|
|
37
|
-
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a Matrix2D object.
|
|
38
|
-
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
39
|
-
*/
|
|
40
|
-
static add(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D;
|
|
41
|
-
/**
|
|
42
|
-
* The function subtracts two 2D matrices and returns the result as a new Matrix2D object.
|
|
43
|
-
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to subtract from.
|
|
44
|
-
* @param {Matrix2D} matrix2 - Matrix2D is a class representing a 2D matrix. It has a property `m` which is a 2D array
|
|
45
|
-
* representing the matrix elements.
|
|
46
|
-
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
47
|
-
*/
|
|
48
|
-
static subtract(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D;
|
|
49
|
-
/**
|
|
50
|
-
* The function multiplies two 2D matrices and returns the result as a new Matrix2D object.
|
|
51
|
-
* @param {Matrix2D} matrix1 - A 2D matrix represented by the Matrix2D class.
|
|
52
|
-
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a 2D matrix of size 3x3.
|
|
53
|
-
* @returns a new instance of the Matrix2D class, created using the result array.
|
|
54
|
-
*/
|
|
55
|
-
static multiply(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D;
|
|
56
|
-
/**
|
|
57
|
-
* The function multiplies each element of a 2D matrix by a given value and returns the resulting matrix.
|
|
58
|
-
* @param {Matrix2D} matrix - The `matrix` parameter is an instance of the `Matrix2D` class, which represents a 2D
|
|
59
|
-
* matrix. It contains a property `m` that is a 2D array representing the matrix elements.
|
|
60
|
-
* @param {number} value - The `value` parameter is a number that you want to multiply each element of the `matrix` by.
|
|
61
|
-
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
62
|
-
*/
|
|
63
|
-
static multiplyByValue(matrix: Matrix2D, value: number): Matrix2D;
|
|
64
|
-
/**
|
|
65
|
-
* The function multiplies a 2D matrix by a 2D vector and returns the result as a 2D vector.
|
|
66
|
-
* @param {Matrix2D} matrix - The parameter "matrix" is of type Matrix2D. It represents a 2-dimensional matrix.
|
|
67
|
-
* @param {Vector2D} vector - The "vector" parameter is a 2D vector, represented by an object of type Vector2D.
|
|
68
|
-
* @returns a Vector2D.
|
|
69
|
-
*/
|
|
70
|
-
static multiplyByVector(matrix: Matrix2D, vector: Vector2D): Vector2D;
|
|
71
|
-
/**
|
|
72
|
-
* The function returns a 2D matrix that scales and flips a vector around the center of a given width and height.
|
|
73
|
-
* @param {number} width - The width parameter represents the width of the view or the canvas. It is a number that
|
|
74
|
-
* specifies the width in pixels or any other unit of measurement.
|
|
75
|
-
* @param {number} height - The height parameter represents the height of the view or the canvas. It is used to
|
|
76
|
-
* calculate the centerY value, which is the vertical center of the view.
|
|
77
|
-
* @returns a Matrix2D object.
|
|
78
|
-
*/
|
|
79
|
-
static view(width: number, height: number): Matrix2D;
|
|
80
|
-
/**
|
|
81
|
-
* The function scales a matrix by a given factor.
|
|
82
|
-
* @param {number} factor - The factor parameter is a number that represents the scaling factor by which the matrix
|
|
83
|
-
* should be scaled.
|
|
84
|
-
* @returns the result of multiplying a new instance of Matrix2D by the given factor.
|
|
85
|
-
*/
|
|
86
|
-
static scale(factor: number): Matrix2D;
|
|
87
|
-
/**
|
|
88
|
-
* The function "rotate" takes an angle in radians and returns a 2D transformation matrix for rotating objects.
|
|
89
|
-
* @param {number} radians - The "radians" parameter is the angle in radians by which you want to rotate an object.
|
|
90
|
-
* @returns The code is returning a new instance of a Matrix2D object.
|
|
91
|
-
*/
|
|
92
|
-
static rotate(radians: number): Matrix2D;
|
|
93
|
-
/**
|
|
94
|
-
* The translate function takes a 2D vector and returns a 2D matrix that represents a translation transformation.
|
|
95
|
-
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D. It represents a 2D vector with components x
|
|
96
|
-
* and y, and an optional w component.
|
|
97
|
-
* @returns The method is returning a new instance of the Matrix2D class.
|
|
98
|
-
*/
|
|
99
|
-
static translate(vector: Vector2D): Matrix2D;
|
|
100
|
-
/**
|
|
101
|
-
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
102
|
-
* _matrix array.
|
|
103
|
-
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
104
|
-
* the first column of the matrix.
|
|
105
|
-
*/
|
|
106
|
-
toVector(): Vector2D;
|
|
107
|
-
}
|
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Matrix2D = 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
|
-
const vector2d_1 = require("./vector2d");
|
|
12
|
-
class Matrix2D {
|
|
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) {
|
|
20
|
-
if (typeof value === 'undefined') {
|
|
21
|
-
this._matrix = Matrix2D.identity;
|
|
22
|
-
}
|
|
23
|
-
else if (value instanceof vector2d_1.Vector2D) {
|
|
24
|
-
this._matrix = Matrix2D.identity;
|
|
25
|
-
this._matrix[0][0] = value.x;
|
|
26
|
-
this._matrix[1][0] = value.y;
|
|
27
|
-
this._matrix[2][0] = value.w;
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
this._matrix = value;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* The function returns a 2D array with three empty arrays.
|
|
35
|
-
* @returns An empty 2-dimensional array with 3 empty arrays inside.
|
|
36
|
-
*/
|
|
37
|
-
static get empty() {
|
|
38
|
-
return [[], [], []];
|
|
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() {
|
|
45
|
-
return [
|
|
46
|
-
[1, 0, 0],
|
|
47
|
-
[0, 1, 0],
|
|
48
|
-
[0, 0, 1]
|
|
49
|
-
];
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* The function returns a two-dimensional array of numbers.
|
|
53
|
-
* @returns The getter method is returning the value of the private variable `_matrix`, which is a two-dimensional
|
|
54
|
-
* array of numbers.
|
|
55
|
-
*/
|
|
56
|
-
get m() {
|
|
57
|
-
return this._matrix;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
|
|
61
|
-
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
|
|
62
|
-
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a Matrix2D object.
|
|
63
|
-
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
64
|
-
*/
|
|
65
|
-
static add(matrix1, matrix2) {
|
|
66
|
-
const result = Matrix2D.empty;
|
|
67
|
-
for (let i = 0; i < 3; i++) {
|
|
68
|
-
for (let j = 0; j < 3; j++) {
|
|
69
|
-
result[i][j] = matrix1.m[i][j] + matrix2.m[i][j];
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return new Matrix2D(result);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* The function subtracts two 2D matrices and returns the result as a new Matrix2D object.
|
|
76
|
-
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to subtract from.
|
|
77
|
-
* @param {Matrix2D} matrix2 - Matrix2D is a class representing a 2D matrix. It has a property `m` which is a 2D array
|
|
78
|
-
* representing the matrix elements.
|
|
79
|
-
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
80
|
-
*/
|
|
81
|
-
static subtract(matrix1, matrix2) {
|
|
82
|
-
const result = Matrix2D.empty;
|
|
83
|
-
for (let i = 0; i < 3; i++) {
|
|
84
|
-
for (let j = 0; j < 3; j++) {
|
|
85
|
-
result[i][j] = matrix1.m[i][j] - matrix2.m[i][j];
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return new Matrix2D(result);
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* The function multiplies two 2D matrices and returns the result as a new Matrix2D object.
|
|
92
|
-
* @param {Matrix2D} matrix1 - A 2D matrix represented by the Matrix2D class.
|
|
93
|
-
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a 2D matrix of size 3x3.
|
|
94
|
-
* @returns a new instance of the Matrix2D class, created using the result array.
|
|
95
|
-
*/
|
|
96
|
-
static multiply(matrix1, matrix2) {
|
|
97
|
-
const result = Matrix2D.empty;
|
|
98
|
-
for (let i = 0; i < 3; i++) {
|
|
99
|
-
for (let j = 0; j < 3; j++) {
|
|
100
|
-
result[i][j] = 0;
|
|
101
|
-
for (let k = 0; k < 3; k++) {
|
|
102
|
-
result[i][j] += matrix1.m[i][k] * matrix2.m[k][j];
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return new Matrix2D(result);
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* The function multiplies each element of a 2D matrix by a given value and returns the resulting matrix.
|
|
110
|
-
* @param {Matrix2D} matrix - The `matrix` parameter is an instance of the `Matrix2D` class, which represents a 2D
|
|
111
|
-
* matrix. It contains a property `m` that is a 2D array representing the matrix elements.
|
|
112
|
-
* @param {number} value - The `value` parameter is a number that you want to multiply each element of the `matrix` by.
|
|
113
|
-
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
114
|
-
*/
|
|
115
|
-
static multiplyByValue(matrix, value) {
|
|
116
|
-
const result = Matrix2D.empty;
|
|
117
|
-
for (let i = 0; i < 3; i++) {
|
|
118
|
-
for (let j = 0; j < 3; j++) {
|
|
119
|
-
result[i][j] = matrix.m[i][j] * value;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
return new Matrix2D(result);
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* The function multiplies a 2D matrix by a 2D vector and returns the result as a 2D vector.
|
|
126
|
-
* @param {Matrix2D} matrix - The parameter "matrix" is of type Matrix2D. It represents a 2-dimensional matrix.
|
|
127
|
-
* @param {Vector2D} vector - The "vector" parameter is a 2D vector, represented by an object of type Vector2D.
|
|
128
|
-
* @returns a Vector2D.
|
|
129
|
-
*/
|
|
130
|
-
static multiplyByVector(matrix, vector) {
|
|
131
|
-
const resultMatrix = Matrix2D.multiply(matrix, new Matrix2D(vector));
|
|
132
|
-
return resultMatrix.toVector();
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* The function returns a 2D matrix that scales and flips a vector around the center of a given width and height.
|
|
136
|
-
* @param {number} width - The width parameter represents the width of the view or the canvas. It is a number that
|
|
137
|
-
* specifies the width in pixels or any other unit of measurement.
|
|
138
|
-
* @param {number} height - The height parameter represents the height of the view or the canvas. It is used to
|
|
139
|
-
* calculate the centerY value, which is the vertical center of the view.
|
|
140
|
-
* @returns a Matrix2D object.
|
|
141
|
-
*/
|
|
142
|
-
static view(width, height) {
|
|
143
|
-
const scaleStep = 1; // Scale every vector * scaleStep
|
|
144
|
-
const centerX = width / 2;
|
|
145
|
-
const centerY = height / 2;
|
|
146
|
-
const flipX = Math.cos(Math.PI); // rotate 180deg / 3.14radian around X-axis
|
|
147
|
-
return new Matrix2D([
|
|
148
|
-
[scaleStep, 0, centerX],
|
|
149
|
-
[0, flipX * scaleStep, centerY],
|
|
150
|
-
[0, 0, 1]
|
|
151
|
-
]);
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* The function scales a matrix by a given factor.
|
|
155
|
-
* @param {number} factor - The factor parameter is a number that represents the scaling factor by which the matrix
|
|
156
|
-
* should be scaled.
|
|
157
|
-
* @returns the result of multiplying a new instance of Matrix2D by the given factor.
|
|
158
|
-
*/
|
|
159
|
-
static scale(factor) {
|
|
160
|
-
return Matrix2D.multiplyByValue(new Matrix2D(), factor);
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* The function "rotate" takes an angle in radians and returns a 2D transformation matrix for rotating objects.
|
|
164
|
-
* @param {number} radians - The "radians" parameter is the angle in radians by which you want to rotate an object.
|
|
165
|
-
* @returns The code is returning a new instance of a Matrix2D object.
|
|
166
|
-
*/
|
|
167
|
-
static rotate(radians) {
|
|
168
|
-
const cos = Math.cos(radians);
|
|
169
|
-
const sin = Math.sin(radians);
|
|
170
|
-
return new Matrix2D([
|
|
171
|
-
[cos, -sin, 0],
|
|
172
|
-
[sin, cos, 0],
|
|
173
|
-
[0, 0, 1]
|
|
174
|
-
]);
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* The translate function takes a 2D vector and returns a 2D matrix that represents a translation transformation.
|
|
178
|
-
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D. It represents a 2D vector with components x
|
|
179
|
-
* and y, and an optional w component.
|
|
180
|
-
* @returns The method is returning a new instance of the Matrix2D class.
|
|
181
|
-
*/
|
|
182
|
-
static translate(vector) {
|
|
183
|
-
return new Matrix2D([
|
|
184
|
-
[1, 0, vector.x],
|
|
185
|
-
[0, 1, vector.y],
|
|
186
|
-
[0, 0, vector.w]
|
|
187
|
-
]);
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
191
|
-
* _matrix array.
|
|
192
|
-
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
193
|
-
* the first column of the matrix.
|
|
194
|
-
*/
|
|
195
|
-
toVector() {
|
|
196
|
-
return new vector2d_1.Vector2D(this._matrix[0][0], this._matrix[1][0]);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
exports.Matrix2D = Matrix2D;
|
|
@@ -1,200 +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
|
-
export declare class Vector2D {
|
|
9
|
-
x: number;
|
|
10
|
-
y: number;
|
|
11
|
-
w: number;
|
|
12
|
-
constructor(x?: number, y?: number, w?: number);
|
|
13
|
-
/**
|
|
14
|
-
* The function checks if the x and y values of a point are both zero.
|
|
15
|
-
* @returns A boolean value indicating whether both the x and y properties of the object are equal to 0.
|
|
16
|
-
*/
|
|
17
|
-
get isZero(): boolean;
|
|
18
|
-
/**
|
|
19
|
-
* The above function calculates the length of a vector using the Pythagorean theorem.
|
|
20
|
-
* @returns The length of a vector, calculated using the Pythagorean theorem.
|
|
21
|
-
*/
|
|
22
|
-
get length(): number;
|
|
23
|
-
/**
|
|
24
|
-
* The function calculates the square of the length of a vector.
|
|
25
|
-
* @returns The method is returning the sum of the squares of the x and y values.
|
|
26
|
-
*/
|
|
27
|
-
get lengthSq(): number;
|
|
28
|
-
/**
|
|
29
|
-
* The "rounded" function returns a new Vector2D object with the x and y values rounded to the nearest whole number.
|
|
30
|
-
* @returns The method is returning a new instance of the Vector2D class with the x and y values rounded to the nearest
|
|
31
|
-
* whole number.
|
|
32
|
-
*/
|
|
33
|
-
get rounded(): Vector2D;
|
|
34
|
-
/**
|
|
35
|
-
* The function "add" takes two Vector2D objects as parameters and returns a new Vector2D object with the sum of their
|
|
36
|
-
* x and y components.
|
|
37
|
-
* @param {Vector2D} vector1 - The parameter `vector1` is an instance of the `Vector2D` class. It represents a
|
|
38
|
-
* 2-dimensional vector with an `x` and `y` component.
|
|
39
|
-
* @param {Vector2D} vector2 - The parameter "vector2" is of type Vector2D. It represents a 2-dimensional vector with
|
|
40
|
-
* an x and y component.
|
|
41
|
-
* @returns The method is returning a new instance of the Vector2D class with the x and y components of the two input
|
|
42
|
-
* vectors added together.
|
|
43
|
-
*/
|
|
44
|
-
static add(vector1: Vector2D, vector2: Vector2D): Vector2D;
|
|
45
|
-
/**
|
|
46
|
-
* The subtract function takes two Vector2D objects as parameters and returns a new Vector2D object with the x and y
|
|
47
|
-
* components subtracted.
|
|
48
|
-
* @param {Vector2D} vector1 - The parameter `vector1` is an instance of the `Vector2D` class, representing a
|
|
49
|
-
* 2-dimensional vector. It has properties `x` and `y` which represent the x and y components of the vector
|
|
50
|
-
* respectively.
|
|
51
|
-
* @param {Vector2D} vector2 - The parameter "vector2" is a Vector2D object. It represents the second vector that you
|
|
52
|
-
* want to subtract from the first vector.
|
|
53
|
-
* @returns The method is returning a new Vector2D object with the x and y components subtracted from vector1 and
|
|
54
|
-
* vector2.
|
|
55
|
-
*/
|
|
56
|
-
static subtract(vector1: Vector2D, vector2: Vector2D): Vector2D;
|
|
57
|
-
/**
|
|
58
|
-
* The function subtracts a given value from the x and y components of a Vector2D object and returns a new Vector2D
|
|
59
|
-
* object.
|
|
60
|
-
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
|
|
61
|
-
* x and y components.
|
|
62
|
-
* @param {number} value - The "value" parameter is a number that will be subtracted from both the x and y components
|
|
63
|
-
* of the "vector" parameter.
|
|
64
|
-
* @returns A new Vector2D object with the x and y values subtracted by the given value.
|
|
65
|
-
*/
|
|
66
|
-
static subtractValue(vector: Vector2D, value: number): Vector2D;
|
|
67
|
-
/**
|
|
68
|
-
* The function multiplies a Vector2D object by a given value.
|
|
69
|
-
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
|
|
70
|
-
* x and y components.
|
|
71
|
-
* @param {number} value - The "value" parameter is a number that represents the value by which the x and y components
|
|
72
|
-
* of the vector will be multiplied.
|
|
73
|
-
* @returns A new Vector2D object with the x and y values multiplied by the given value.
|
|
74
|
-
*/
|
|
75
|
-
static multiply(vector: Vector2D, value: number): Vector2D;
|
|
76
|
-
/**
|
|
77
|
-
* The function divides the x and y components of a Vector2D by a given value and returns a new Vector2D.
|
|
78
|
-
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
|
|
79
|
-
* x and y components.
|
|
80
|
-
* @param {number} value - The value parameter is a number that will be used to divide the x and y components of the
|
|
81
|
-
* vector.
|
|
82
|
-
* @returns A new instance of the Vector2D class with the x and y values divided by the given value.
|
|
83
|
-
*/
|
|
84
|
-
static divide(vector: Vector2D, value: number): Vector2D;
|
|
85
|
-
/**
|
|
86
|
-
* The function checks if two Vector2D objects are equal by comparing their x and y values.
|
|
87
|
-
* @param {Vector2D} vector1 - The parameter `vector1` is of type `Vector2D`, which represents a 2-dimensional vector.
|
|
88
|
-
* It has two properties: `x` and `y`, which represent the x and y components of the vector, respectively.
|
|
89
|
-
* @param {Vector2D} vector2 - The parameter "vector2" is of type Vector2D.
|
|
90
|
-
* @returns a boolean value, which indicates whether the two input vectors are equal or not.
|
|
91
|
-
*/
|
|
92
|
-
static equals(vector1: Vector2D, vector2: Vector2D): boolean;
|
|
93
|
-
/**
|
|
94
|
-
* The function checks if two Vector2D objects are equal within a specified rounding factor.
|
|
95
|
-
* @param {Vector2D} vector1 - The first vector to compare.
|
|
96
|
-
* @param {Vector2D} vector2 - The parameter "vector2" is a Vector2D object, which represents a 2-dimensional vector.
|
|
97
|
-
* It is used as one of the inputs for the "equalsRounded" function.
|
|
98
|
-
* @param [roundingFactor=12] - The roundingFactor parameter is used to determine the threshold for considering two
|
|
99
|
-
* vectors as equal. If the absolute difference in the x and y components of the vectors is less than the
|
|
100
|
-
* roundingFactor, the vectors are considered equal.
|
|
101
|
-
* @returns a boolean value.
|
|
102
|
-
*/
|
|
103
|
-
static equalsRounded(vector1: Vector2D, vector2: Vector2D, roundingFactor?: number): boolean;
|
|
104
|
-
/**
|
|
105
|
-
* 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
|
|
106
|
-
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D.
|
|
107
|
-
* @returns the normalized vector if its length is greater than a very small value (epsilon), otherwise it returns the
|
|
108
|
-
* original vector.
|
|
109
|
-
*/
|
|
110
|
-
static normalize(vector: Vector2D): Vector2D;
|
|
111
|
-
/**
|
|
112
|
-
* 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
|
|
113
|
-
* @param {Vector2D} vector - A 2D vector represented by the Vector2D class.
|
|
114
|
-
* @param {number} max - The `max` parameter is a number that represents the maximum length that the `vector` should
|
|
115
|
-
* have.
|
|
116
|
-
* @returns either the original vector or a truncated version of the vector, depending on whether the length of the
|
|
117
|
-
* vector is greater than the maximum value specified.
|
|
118
|
-
*/
|
|
119
|
-
static truncate(vector: Vector2D, max: number): Vector2D;
|
|
120
|
-
/**
|
|
121
|
-
* The function returns a new Vector2D object that is perpendicular to the input vector.The vector that is perpendicular to this one
|
|
122
|
-
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D.
|
|
123
|
-
* @returns A new Vector2D object is being returned.
|
|
124
|
-
*/
|
|
125
|
-
static perp(vector: Vector2D): Vector2D;
|
|
126
|
-
/**
|
|
127
|
-
* The reverse function takes a Vector2D object and returns a new Vector2D object with the negated x and y values.
|
|
128
|
-
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector. It
|
|
129
|
-
* has two properties: "x" and "y", which represent the x and y components of the vector, respectively.
|
|
130
|
-
* @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
|
|
131
|
-
*/
|
|
132
|
-
static reverse(vector: Vector2D): Vector2D;
|
|
133
|
-
/**
|
|
134
|
-
* The function takes a Vector2D object as input and returns a new Vector2D object with the absolute values of its x
|
|
135
|
-
* and y components.
|
|
136
|
-
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector. It
|
|
137
|
-
* has two properties: "x" and "y", which represent the x and y components of the vector, respectively.
|
|
138
|
-
* @returns The method is returning a new Vector2D object with the absolute values of the x and y components of the
|
|
139
|
-
* input vector.
|
|
140
|
-
*/
|
|
141
|
-
static abs(vector: Vector2D): Vector2D;
|
|
142
|
-
/**
|
|
143
|
-
* The dot function calculates the dot product of two 2D vectors.The dot product of v1 and v2
|
|
144
|
-
* @param {Vector2D} vector1 - The parameter `vector1` represents a 2D vector with its x and y components.
|
|
145
|
-
* @param {Vector2D} vector2 - The "vector2" parameter is a Vector2D object. It represents a two-dimensional vector
|
|
146
|
-
* with an x and y component.
|
|
147
|
-
* @returns The dot product of the two input vectors.
|
|
148
|
-
*/
|
|
149
|
-
static dot(vector1: Vector2D, vector2: Vector2D): number;
|
|
150
|
-
/**
|
|
151
|
-
* The function calculates the distance between two points in a two-dimensional space.
|
|
152
|
-
* @param {Vector2D} vector1 - The parameter `vector1` represents the first vector in 2D space, while `vector2`
|
|
153
|
-
* represents the second vector. Each vector has an `x` and `y` component, which represent their respective coordinates
|
|
154
|
-
* in the 2D space.
|
|
155
|
-
* @param {Vector2D} vector2 - The `vector2` parameter represents the second vector in the calculation of distance. It
|
|
156
|
-
* is an instance of the `Vector2D` class, which typically has properties `x` and `y` representing the coordinates of
|
|
157
|
-
* the vector in a 2D space.
|
|
158
|
-
* @returns The distance between vector1 and vector2.
|
|
159
|
-
*/
|
|
160
|
-
static distance(vector1: Vector2D, vector2: Vector2D): number;
|
|
161
|
-
/**
|
|
162
|
-
* The function calculates the squared distance between two 2D vectors.
|
|
163
|
-
* @param {Vector2D} vector1 - The parameter `vector1` represents the first vector, which is an instance of the
|
|
164
|
-
* `Vector2D` class. It contains the x and y coordinates of the vector.
|
|
165
|
-
* @param {Vector2D} vector2 - The `vector2` parameter represents the second vector in a two-dimensional space. It has
|
|
166
|
-
* properties `x` and `y` which represent the coordinates of the vector.
|
|
167
|
-
* @returns the square of the distance between the two input vectors.
|
|
168
|
-
*/
|
|
169
|
-
static distanceSq(vector1: Vector2D, vector2: Vector2D): number;
|
|
170
|
-
/**
|
|
171
|
-
* The sign function determines the sign of the cross product between two 2D vectors.
|
|
172
|
-
* (assuming the Y axis is pointing down, X axis to right like a Window app)
|
|
173
|
-
* @param {Vector2D} vector1 - The parameter `vector1` is of type `Vector2D`, which represents a 2-dimensional vector.
|
|
174
|
-
* It likely has properties `x` and `y` representing the x and y components of the vector, respectively.
|
|
175
|
-
* @param {Vector2D} vector2 - The above code defines a function called "sign" that takes two parameters: vector1 and
|
|
176
|
-
* vector2. Both vector1 and vector2 are of type Vector2D.
|
|
177
|
-
* @returns either -1 or 1. Returns positive if v2 is clockwise of this vector, negative if counterclockwise
|
|
178
|
-
*/
|
|
179
|
-
static sign(vector1: Vector2D, vector2: Vector2D): number;
|
|
180
|
-
/**
|
|
181
|
-
* The function calculates the angle between a given vector and the negative y-axis.
|
|
182
|
-
* @param {Vector2D} vector - The "vector" parameter is an instance of the Vector2D class, which represents a
|
|
183
|
-
* 2-dimensional vector. It has two properties: "x" and "y", which represent the x and y components of the vector,
|
|
184
|
-
* respectively.
|
|
185
|
-
* @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
|
|
186
|
-
*/
|
|
187
|
-
static angle(vector: Vector2D): number;
|
|
188
|
-
/**
|
|
189
|
-
* The function "random" generates a random Vector2D object with x and y values within the specified range.
|
|
190
|
-
* @param {number} maxX - The maxX parameter represents the maximum value for the x-coordinate of the random vector.
|
|
191
|
-
* @param {number} maxY - The `maxY` parameter represents the maximum value for the y-coordinate of the generated
|
|
192
|
-
* random vector.
|
|
193
|
-
* @returns a new instance of the Vector2D class with random x and y values.
|
|
194
|
-
*/
|
|
195
|
-
static random(maxX: number, maxY: number): Vector2D;
|
|
196
|
-
/**
|
|
197
|
-
* The function sets the values of x and y to zero.
|
|
198
|
-
*/
|
|
199
|
-
zero(): void;
|
|
200
|
-
}
|