@lakuna/umath 0.0.3 → 0.0.4
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/README.md +33 -0
- package/dist/algorithms/combinations.d.ts +2 -0
- package/dist/algorithms/combinations.d.ts.map +1 -0
- package/dist/algorithms/combinations.js +5 -0
- package/dist/algorithms/combinations.js.map +1 -0
- package/dist/algorithms/degreesToRadians.d.ts +2 -0
- package/dist/algorithms/degreesToRadians.d.ts.map +1 -0
- package/dist/algorithms/degreesToRadians.js +4 -0
- package/dist/algorithms/degreesToRadians.js.map +1 -0
- package/dist/algorithms/factorial.d.ts +2 -0
- package/dist/algorithms/factorial.d.ts.map +1 -0
- package/dist/algorithms/factorial.js +4 -0
- package/dist/algorithms/factorial.js.map +1 -0
- package/dist/algorithms/fibonacci.d.ts +2 -0
- package/dist/algorithms/fibonacci.d.ts.map +1 -0
- package/dist/algorithms/fibonacci.js +9 -0
- package/dist/algorithms/fibonacci.js.map +1 -0
- package/dist/algorithms/isPrime.d.ts +2 -0
- package/dist/algorithms/isPrime.d.ts.map +1 -0
- package/dist/algorithms/isPrime.js +12 -0
- package/dist/algorithms/isPrime.js.map +1 -0
- package/dist/algorithms/permutations.d.ts +2 -0
- package/dist/algorithms/permutations.d.ts.map +1 -0
- package/dist/algorithms/permutations.js +5 -0
- package/dist/algorithms/permutations.js.map +1 -0
- package/dist/algorithms/primeFactorization.d.ts +2 -0
- package/dist/algorithms/primeFactorization.d.ts.map +1 -0
- package/dist/algorithms/primeFactorization.js +15 -0
- package/dist/algorithms/primeFactorization.js.map +1 -0
- package/dist/algorithms/radiansToDegrees.d.ts +2 -0
- package/dist/algorithms/radiansToDegrees.d.ts.map +1 -0
- package/dist/algorithms/radiansToDegrees.js +4 -0
- package/dist/algorithms/radiansToDegrees.js.map +1 -0
- package/dist/algorithms/summation.d.ts.map +1 -1
- package/dist/algorithms/summation.js.map +1 -1
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -1
- package/dist/linear/DualQuaternion.d.ts +89 -0
- package/dist/linear/DualQuaternion.d.ts.map +1 -0
- package/dist/linear/DualQuaternion.js +530 -0
- package/dist/linear/DualQuaternion.js.map +1 -0
- package/dist/linear/Matrix.d.ts +18 -0
- package/dist/linear/Matrix.d.ts.map +1 -0
- package/dist/linear/Matrix.js +2 -0
- package/dist/linear/Matrix.js.map +1 -0
- package/dist/linear/Matrix2.d.ts +66 -0
- package/dist/linear/Matrix2.d.ts.map +1 -0
- package/dist/linear/Matrix2.js +240 -0
- package/dist/linear/Matrix2.js.map +1 -0
- package/dist/linear/Matrix3.d.ts +91 -0
- package/dist/linear/Matrix3.d.ts.map +1 -0
- package/dist/linear/Matrix3.js +533 -0
- package/dist/linear/Matrix3.js.map +1 -0
- package/dist/linear/Matrix4.d.ts +140 -0
- package/dist/linear/Matrix4.d.ts.map +1 -0
- package/dist/linear/Matrix4.js +1363 -0
- package/dist/linear/Matrix4.js.map +1 -0
- package/dist/linear/Quaternion.d.ts +80 -0
- package/dist/linear/Quaternion.d.ts.map +1 -0
- package/dist/linear/Quaternion.js +371 -0
- package/dist/linear/Quaternion.js.map +1 -0
- package/dist/linear/SquareMatrix.d.ts +7 -0
- package/dist/linear/SquareMatrix.d.ts.map +1 -0
- package/dist/linear/SquareMatrix.js +2 -0
- package/dist/linear/SquareMatrix.js.map +1 -0
- package/dist/linear/Vector.d.ts +30 -0
- package/dist/linear/Vector.d.ts.map +1 -0
- package/dist/linear/Vector.js +2 -0
- package/dist/linear/Vector.js.map +1 -0
- package/dist/linear/Vector2.d.ts +96 -0
- package/dist/linear/Vector2.d.ts.map +1 -0
- package/dist/linear/Vector2.js +293 -0
- package/dist/linear/Vector2.js.map +1 -0
- package/dist/linear/Vector3.d.ts +111 -0
- package/dist/linear/Vector3.d.ts.map +1 -0
- package/dist/linear/Vector3.js +466 -0
- package/dist/linear/Vector3.js.map +1 -0
- package/dist/linear/Vector4.d.ts +86 -0
- package/dist/linear/Vector4.d.ts.map +1 -0
- package/dist/linear/Vector4.js +348 -0
- package/dist/linear/Vector4.js.map +1 -0
- package/dist/types/AxisAngle.d.ts +6 -0
- package/dist/types/AxisAngle.d.ts.map +1 -0
- package/dist/types/AxisAngle.js +2 -0
- package/dist/types/AxisAngle.js.map +1 -0
- package/dist/types/FieldOfView.d.ts +7 -0
- package/dist/types/FieldOfView.d.ts.map +1 -0
- package/dist/types/FieldOfView.js +2 -0
- package/dist/types/FieldOfView.js.map +1 -0
- package/dist/utility/BigNumber.d.ts +1 -1
- package/dist/utility/BigNumber.d.ts.map +1 -1
- package/dist/utility/BigNumber.js +4 -1
- package/dist/utility/BigNumber.js.map +1 -1
- package/dist/utility/MagnitudeError.d.ts +4 -0
- package/dist/utility/MagnitudeError.d.ts.map +1 -0
- package/dist/utility/MagnitudeError.js +6 -0
- package/dist/utility/MagnitudeError.js.map +1 -0
- package/dist/utility/SingularMatrixError.d.ts +4 -0
- package/dist/utility/SingularMatrixError.d.ts.map +1 -0
- package/dist/utility/SingularMatrixError.js +6 -0
- package/dist/utility/SingularMatrixError.js.map +1 -0
- package/dist/utility/epsilon.d.ts +3 -0
- package/dist/utility/epsilon.d.ts.map +1 -0
- package/dist/utility/epsilon.js +2 -0
- package/dist/utility/epsilon.js.map +1 -0
- package/package.json +15 -7
- package/src/algorithms/combinations.ts +12 -0
- package/src/algorithms/degreesToRadians.ts +10 -0
- package/src/algorithms/factorial.ts +9 -0
- package/src/algorithms/fibonacci.ts +14 -0
- package/src/algorithms/greatestCommonDivisor.ts +2 -2
- package/src/algorithms/isPrime.ts +20 -0
- package/src/algorithms/permutations.ts +12 -0
- package/src/algorithms/primeFactorization.ts +21 -0
- package/src/algorithms/radiansToDegrees.ts +10 -0
- package/src/algorithms/summation.ts +4 -1
- package/src/index.ts +24 -0
- package/src/linear/DualQuaternion.ts +1213 -0
- package/src/linear/Matrix.ts +106 -0
- package/src/linear/Matrix2.ts +709 -0
- package/src/linear/Matrix3.ts +1194 -0
- package/src/linear/Matrix4.ts +2569 -0
- package/src/linear/Quaternion.ts +967 -0
- package/src/linear/SquareMatrix.ts +27 -0
- package/src/linear/Vector.ts +178 -0
- package/src/linear/Vector2.ts +979 -0
- package/src/linear/Vector3.ts +1320 -0
- package/src/linear/Vector4.ts +975 -0
- package/src/types/AxisAngle.ts +10 -0
- package/src/types/FieldOfView.ts +14 -0
- package/src/utility/BigNumber.ts +2 -1
- package/src/utility/MagnitudeError.ts +10 -0
- package/src/utility/SingularMatrixError.ts +13 -0
- package/src/utility/epsilon.ts +2 -0
|
@@ -0,0 +1,979 @@
|
|
|
1
|
+
import { epsilon, type Vector } from "@lakuna/umath";
|
|
2
|
+
import type { Matrix2Like } from "@lakuna/umath/Matrix2";
|
|
3
|
+
import type { Matrix3Like } from "@lakuna/umath/Matrix3";
|
|
4
|
+
import type { Matrix4Like } from "@lakuna/umath/Matrix4";
|
|
5
|
+
import Vector3, { type Vector3Like } from "@lakuna/umath/Vector3";
|
|
6
|
+
|
|
7
|
+
/** A quantity with magnitude and direction in two dimensions. */
|
|
8
|
+
export type Vector2Like = Vector2 | [number, number];
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Creates a vector with the given values.
|
|
12
|
+
* @param x The first component.
|
|
13
|
+
* @param y The second component.
|
|
14
|
+
* @param out The vector to store the result in.
|
|
15
|
+
* @returns A new vector.
|
|
16
|
+
*/
|
|
17
|
+
export function fromValues<T extends Vector2Like>(x: number, y: number, out: T): T {
|
|
18
|
+
out[0] = x;
|
|
19
|
+
out[1] = y;
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Determines whether two vectors are roughly equivalent.
|
|
25
|
+
* @param a The first vector.
|
|
26
|
+
* @param b The second vector.
|
|
27
|
+
* @returns Whether the vectors are equivalent.
|
|
28
|
+
*/
|
|
29
|
+
export function equals(a: Vector2Like, b: Vector2Like): boolean {
|
|
30
|
+
const a0: number = a[0];
|
|
31
|
+
const a1: number = a[1];
|
|
32
|
+
|
|
33
|
+
const b0: number = b[0];
|
|
34
|
+
const b1: number = b[1];
|
|
35
|
+
|
|
36
|
+
return Math.abs(a0 - b0) <= epsilon * Math.max(1, Math.abs(a0), Math.abs(b0))
|
|
37
|
+
&& Math.abs(a1 - b1) <= epsilon * Math.max(1, Math.abs(a1), Math.abs(b1));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Determines whether two vectors are exactly equivalent.
|
|
42
|
+
* @param a The first vector.
|
|
43
|
+
* @param b The second vector.
|
|
44
|
+
* @returns Whether the vectors are equivalent.
|
|
45
|
+
*/
|
|
46
|
+
export function exactEquals(a: Vector2Like, b: Vector2Like): boolean {
|
|
47
|
+
return a[0] == b[0]
|
|
48
|
+
&& a[1] == b[1];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Adds two vectors.
|
|
53
|
+
* @param a The augend.
|
|
54
|
+
* @param b The addend.
|
|
55
|
+
* @param out The vector to store the result in.
|
|
56
|
+
* @returns The sum.
|
|
57
|
+
*/
|
|
58
|
+
export function add<T extends Vector2Like>(a: Vector2Like, b: Vector2Like, out: T): T {
|
|
59
|
+
out[0] = a[0] + b[0];
|
|
60
|
+
out[1] = a[1] + b[1];
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Copies the values of one vector into another.
|
|
66
|
+
* @param vector The vector to copy.
|
|
67
|
+
* @param out The vector to store the result in.
|
|
68
|
+
* @returns The copy.
|
|
69
|
+
*/
|
|
70
|
+
export function copy<T extends Vector2Like>(vector: Vector2Like, out: T): T {
|
|
71
|
+
out[0] = vector[0];
|
|
72
|
+
out[1] = vector[1];
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Multiplies the components in one vector by the corresponding components in another.
|
|
78
|
+
* @param a The multiplicand.
|
|
79
|
+
* @param b The multiplier.
|
|
80
|
+
* @param out The vector to store the result in.
|
|
81
|
+
* @returns The product.
|
|
82
|
+
*/
|
|
83
|
+
export function multiply<T extends Vector2Like>(a: Vector2Like, b: Vector2Like, out: T): T {
|
|
84
|
+
out[0] = a[0] * b[0];
|
|
85
|
+
out[1] = a[1] * b[1];
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Divides one vector by another.
|
|
91
|
+
* @param a The dividend.
|
|
92
|
+
* @param b The divisor.
|
|
93
|
+
* @param out The vector to store the result in.
|
|
94
|
+
* @returns The quotient.
|
|
95
|
+
*/
|
|
96
|
+
export function divide<T extends Vector2Like>(a: Vector2Like, b: Vector2Like, out: T): T {
|
|
97
|
+
out[0] = a[0] / b[0];
|
|
98
|
+
out[1] = a[1] / b[1];
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Subtracts one vector from another.
|
|
104
|
+
* @param a The minuend.
|
|
105
|
+
* @param b The subtrahend.
|
|
106
|
+
* @param out The vector to store the result in.
|
|
107
|
+
* @returns The difference.
|
|
108
|
+
*/
|
|
109
|
+
export function subtract<T extends Vector2Like>(a: Vector2Like, b: Vector2Like, out: T): T {
|
|
110
|
+
out[0] = a[0] - b[0];
|
|
111
|
+
out[1] = a[1] - b[1];
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Rounds up the components of a vector.
|
|
117
|
+
* @param vector The vector.
|
|
118
|
+
* @param out The vector to store the result in.
|
|
119
|
+
* @returns The rounded vector.
|
|
120
|
+
*/
|
|
121
|
+
export function ceil<T extends Vector2Like>(vector: Vector2Like, out: T): T {
|
|
122
|
+
out[0] = Math.ceil(vector[0]);
|
|
123
|
+
out[1] = Math.ceil(vector[1]);
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Rounds down the components of a vector.
|
|
129
|
+
* @param vector The vector.
|
|
130
|
+
* @param out The vector to store the result in.
|
|
131
|
+
* @returns The rounded vector.
|
|
132
|
+
*/
|
|
133
|
+
export function floor<T extends Vector2Like>(vector: Vector2Like, out: T): T {
|
|
134
|
+
out[0] = Math.floor(vector[0]);
|
|
135
|
+
out[1] = Math.floor(vector[1]);
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Rounds the components of a vector.
|
|
141
|
+
* @param vector The vector.
|
|
142
|
+
* @param out The vector to store the result in.
|
|
143
|
+
* @returns The rounded vector.
|
|
144
|
+
*/
|
|
145
|
+
export function round<T extends Vector2Like>(vector: Vector2Like, out: T): T {
|
|
146
|
+
out[0] = Math.round(vector[0]);
|
|
147
|
+
out[1] = Math.round(vector[1]);
|
|
148
|
+
return out;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Returns the minimum of two vectors.
|
|
153
|
+
* @param a The first vector.
|
|
154
|
+
* @param b The second vector.
|
|
155
|
+
* @param out The vector to store the result in.
|
|
156
|
+
* @returns The minimum.
|
|
157
|
+
*/
|
|
158
|
+
export function min<T extends Vector2Like>(a: Vector2Like, b: Vector2Like, out: T): T {
|
|
159
|
+
out[0] = Math.min(a[0], b[0]);
|
|
160
|
+
out[1] = Math.min(a[1], b[1]);
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Returns the maximum of two vectors.
|
|
166
|
+
* @param a The first vector.
|
|
167
|
+
* @param b The second vector.
|
|
168
|
+
* @param out The vector to store the result in.
|
|
169
|
+
* @returns The minimum.
|
|
170
|
+
*/
|
|
171
|
+
export function max<T extends Vector2Like>(a: Vector2Like, b: Vector2Like, out: T): T {
|
|
172
|
+
out[0] = Math.max(a[0], b[0]);
|
|
173
|
+
out[1] = Math.max(a[1], b[1]);
|
|
174
|
+
return out;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Multiplies a vector by a scalar.
|
|
179
|
+
* @param vector The multiplier.
|
|
180
|
+
* @param scalar The multiplicand.
|
|
181
|
+
* @param out The vector to store the result in.
|
|
182
|
+
* @returns The product.
|
|
183
|
+
*/
|
|
184
|
+
export function scale<T extends Vector2Like>(vector: Vector2Like, scalar: number, out: T): T {
|
|
185
|
+
out[0] = vector[0] * scalar;
|
|
186
|
+
out[1] = vector[1] * scalar;
|
|
187
|
+
return out;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Adds two vectors after multiplying the latter by a scalar.
|
|
192
|
+
* @param a The augend.
|
|
193
|
+
* @param b The addend.
|
|
194
|
+
* @param scalar The multiplier.
|
|
195
|
+
* @param out The vector to store the result in.
|
|
196
|
+
* @returns The sum.
|
|
197
|
+
*/
|
|
198
|
+
export function scaleAndAdd<T extends Vector2Like>(a: Vector2Like, b: Vector2Like, scalar: number, out: T): T {
|
|
199
|
+
out[0] = a[0] + b[0] * scalar;
|
|
200
|
+
out[1] = a[1] + b[1] * scalar;
|
|
201
|
+
return out;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Calculates the Euclidean distance from one vector to another.
|
|
206
|
+
* @param a The first vector.
|
|
207
|
+
* @param b The second vector.
|
|
208
|
+
* @returns The distance.
|
|
209
|
+
* @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
|
|
210
|
+
*/
|
|
211
|
+
export function distance(a: Vector2Like, b: Vector2Like): number {
|
|
212
|
+
const x: number = b[0] - a[0];
|
|
213
|
+
const y: number = b[1] - a[1];
|
|
214
|
+
return Math.hypot(x, y);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Calculates the squared Euclidean distance from one vector to another.
|
|
219
|
+
* @param a The first vector.
|
|
220
|
+
* @param b The other vector.
|
|
221
|
+
* @returns The squared distance.
|
|
222
|
+
* @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
|
|
223
|
+
*/
|
|
224
|
+
export function squaredDistance(a: Vector2Like, b: Vector2Like): number {
|
|
225
|
+
const x: number = b[0] - a[0];
|
|
226
|
+
const y: number = b[1] - a[1];
|
|
227
|
+
return x * x + y * y;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* The magnitude (length) of a vector.
|
|
232
|
+
* @param vector The vector.
|
|
233
|
+
* @returns The magnitude.
|
|
234
|
+
*/
|
|
235
|
+
export function getMagnitude(vector: Vector2Like): number {
|
|
236
|
+
const x: number = vector[0];
|
|
237
|
+
const y: number = vector[1];
|
|
238
|
+
return Math.hypot(x, y);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* The squared magnitude (length) of a vector.
|
|
243
|
+
* @param vector The vector.
|
|
244
|
+
* @returns The squared magnitude.
|
|
245
|
+
*/
|
|
246
|
+
export function getSquaredMagnitude(vector: Vector2Like): number {
|
|
247
|
+
const x: number = vector[0];
|
|
248
|
+
const y: number = vector[1];
|
|
249
|
+
return x * x + y * y;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Negates a vector.
|
|
254
|
+
* @param vector The vector.
|
|
255
|
+
* @param out The vector to store the result in.
|
|
256
|
+
* @returns The negated vector.
|
|
257
|
+
*/
|
|
258
|
+
export function negate<T extends Vector2Like>(vector: Vector2Like, out: T): T {
|
|
259
|
+
out[0] = -vector[0];
|
|
260
|
+
out[1] = -vector[1];
|
|
261
|
+
return out;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Calculates the multiplicative inverse of the components of a vector.
|
|
266
|
+
* @param vector The vector.
|
|
267
|
+
* @param out The vector to store the result in.
|
|
268
|
+
* @returns The inverted vector.
|
|
269
|
+
*/
|
|
270
|
+
export function inverse<T extends Vector2Like>(vector: Vector2Like, out: T): T {
|
|
271
|
+
out[0] = 1 / vector[0];
|
|
272
|
+
out[1] = 1 / vector[1];
|
|
273
|
+
return out;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Normalizes a vector.
|
|
278
|
+
* @param vector The vector.
|
|
279
|
+
* @param out The vector to store the result in.
|
|
280
|
+
* @returns The normalized vector.
|
|
281
|
+
* @see [Unit vector](https://en.wikipedia.org/wiki/Unit_vector)
|
|
282
|
+
*/
|
|
283
|
+
export function normalize<T extends Vector2Like>(vector: Vector2Like, out: T): T {
|
|
284
|
+
const x: number = vector[0];
|
|
285
|
+
const y: number = vector[1];
|
|
286
|
+
|
|
287
|
+
let len: number = x * x + y * y;
|
|
288
|
+
if (len > 0) {
|
|
289
|
+
len = 1 / Math.sqrt(len);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
out[0] = x * len;
|
|
293
|
+
out[1] = y * len;
|
|
294
|
+
return out;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Calculates the dot product of two vectors.
|
|
299
|
+
* @param a The multiplicand.
|
|
300
|
+
* @param b The multiplier.
|
|
301
|
+
* @returns The dot product.
|
|
302
|
+
* @see [Dot product](https://en.wikipedia.org/wiki/Dot_product)
|
|
303
|
+
*/
|
|
304
|
+
export function dot(a: Vector2Like, b: Vector2Like): number {
|
|
305
|
+
return a[0] * b[0] + a[1] * b[1];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Calculates the cross product of two vectors.
|
|
310
|
+
* @param a The multiplicand.
|
|
311
|
+
* @param b The multiplier.
|
|
312
|
+
* @param out The vector to store the result in.
|
|
313
|
+
* @returns The cross product.
|
|
314
|
+
* @see [Cross product](https://en.wikipedia.org/wiki/Cross_product)
|
|
315
|
+
*/
|
|
316
|
+
export function cross<T extends Vector3Like>(a: Vector2Like, b: Vector2Like, out: T): T {
|
|
317
|
+
out[0] = 0;
|
|
318
|
+
out[1] = 0;
|
|
319
|
+
out[2] = a[0] * b[1] - a[1] * b[0];
|
|
320
|
+
return out;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Performs a linear interpolation between two vectors.
|
|
325
|
+
* @param a The first vector.
|
|
326
|
+
* @param b The second vector.
|
|
327
|
+
* @param t The interpolation amount (in `[0,1]`).
|
|
328
|
+
* @param out The vector to store the result in.
|
|
329
|
+
* @returns The interpolated vector.
|
|
330
|
+
* @see [Linear interpolation](https://en.wikipedia.org/wiki/Linear_interpolation)
|
|
331
|
+
*/
|
|
332
|
+
export function lerp<T extends Vector2Like>(a: Vector2Like, b: Vector2Like, t: number, out: T): T {
|
|
333
|
+
const ax: number = a[0];
|
|
334
|
+
const ay: number = a[1];
|
|
335
|
+
|
|
336
|
+
out[0] = ax + t * (b[0] - ax);
|
|
337
|
+
out[1] = ay + t * (b[1] - ay);
|
|
338
|
+
return out;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Sets a vector to a random value with the given magnitude.
|
|
343
|
+
* @param magnitude The magnitude.
|
|
344
|
+
* @param out The vector to store the result in.
|
|
345
|
+
* @returns The vector.
|
|
346
|
+
*/
|
|
347
|
+
export function random<T extends Vector2Like>(magnitude: number, out: T): T {
|
|
348
|
+
const r: number = Math.random() * 2 * Math.PI;
|
|
349
|
+
|
|
350
|
+
out[0] = Math.cos(r) * magnitude;
|
|
351
|
+
out[1] = Math.sin(r) * magnitude;
|
|
352
|
+
return out;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Transforms a vector by a two-by-two matrix.
|
|
357
|
+
* @param vector The vector.
|
|
358
|
+
* @param matrix The matrix.
|
|
359
|
+
* @param out The vector to store the result in.
|
|
360
|
+
* @returns The transformed vector.
|
|
361
|
+
* @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
|
|
362
|
+
*/
|
|
363
|
+
export function transformMatrix2<T extends Vector2Like>(vector: Vector2Like, matrix: Matrix2Like, out: T): T {
|
|
364
|
+
const x: number = vector[0];
|
|
365
|
+
const y: number = vector[1];
|
|
366
|
+
|
|
367
|
+
out[0] = matrix[0] * x + matrix[2] * y;
|
|
368
|
+
out[1] = matrix[1] * x + matrix[3] * y;
|
|
369
|
+
return out;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Transforms a vector by a three-by-three matrix.
|
|
374
|
+
* @param vector The vector.
|
|
375
|
+
* @param matrix The matrix.
|
|
376
|
+
* @param out The vector to store the result in.
|
|
377
|
+
* @returns The transformed vector.
|
|
378
|
+
* @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
|
|
379
|
+
*/
|
|
380
|
+
export function transformMatrix3<T extends Vector2Like>(vector: Vector2Like, matrix: Matrix3Like, out: T): T {
|
|
381
|
+
const x: number = vector[0] as number;
|
|
382
|
+
const y: number = vector[1] as number;
|
|
383
|
+
|
|
384
|
+
out[0] = matrix[0] * x + matrix[3] * y + matrix[6];
|
|
385
|
+
out[1] = matrix[1] * x + matrix[4] * y + matrix[7];
|
|
386
|
+
return out;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Transforms a vector by a four-by-four matrix.
|
|
391
|
+
* @param vector The vector.
|
|
392
|
+
* @param matrix The matrix.
|
|
393
|
+
* @param out The vector to store the result in.
|
|
394
|
+
* @returns The transformed vector.
|
|
395
|
+
* @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
|
|
396
|
+
*/
|
|
397
|
+
export function transformMatrix4<T extends Vector2Like>(vector: Vector2Like, matrix: Matrix4Like, out: T): T {
|
|
398
|
+
const x: number = vector[0] as number;
|
|
399
|
+
const y: number = vector[1] as number;
|
|
400
|
+
|
|
401
|
+
out[0] = matrix[0] * x + matrix[4] * y + matrix[12];
|
|
402
|
+
out[1] = matrix[1] * x + matrix[5] * y + matrix[13];
|
|
403
|
+
return out;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Rotates a vector.
|
|
408
|
+
* @param vector The vector.
|
|
409
|
+
* @param origin The origin of the rotation.
|
|
410
|
+
* @param radians The angle of rotation in radians.
|
|
411
|
+
* @param out The vector to store the result in.
|
|
412
|
+
* @returns The rotated vector.
|
|
413
|
+
*/
|
|
414
|
+
export function rotate<T extends Vector2Like>(vector: Vector2Like, origin: Vector2Like, radians: number, out: T): T {
|
|
415
|
+
const o0: number = origin[0];
|
|
416
|
+
const o1: number = origin[1];
|
|
417
|
+
|
|
418
|
+
const p0: number = vector[0] - o0;
|
|
419
|
+
const p1: number = vector[1] - o1;
|
|
420
|
+
|
|
421
|
+
const s: number = Math.sin(radians);
|
|
422
|
+
const c: number = Math.cos(radians);
|
|
423
|
+
|
|
424
|
+
out[0] = p0 * c - p1 * s + o0;
|
|
425
|
+
out[1] = p0 * s + p1 * c + o1;
|
|
426
|
+
return out;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Gets the angle from one vector to another in radians.
|
|
431
|
+
* @param a The first vector.
|
|
432
|
+
* @param b The second vector.
|
|
433
|
+
* @returns The angular distance.
|
|
434
|
+
*/
|
|
435
|
+
export function angle(a: Vector2Like, b: Vector2Like): number {
|
|
436
|
+
const x1: number = a[0];
|
|
437
|
+
const y1: number = a[1];
|
|
438
|
+
|
|
439
|
+
const x2: number = b[0];
|
|
440
|
+
const y2: number = b[1];
|
|
441
|
+
|
|
442
|
+
const magnitudeProduct: number = Math.sqrt(x1 * x1 + y1 * y1) * Math.sqrt(x2 * x2 + y2 * y2);
|
|
443
|
+
const c: number = magnitudeProduct && (x1 * x2 + y1 * y2) / magnitudeProduct;
|
|
444
|
+
return Math.acos(Math.min(Math.max(c, -1), 1));
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Sets a vector to the zero vector.
|
|
449
|
+
* @param out The vector to store the result in.
|
|
450
|
+
* @returns This vector.
|
|
451
|
+
*/
|
|
452
|
+
export function zero<T extends Vector2Like>(out: T): T {
|
|
453
|
+
out[0] = 0;
|
|
454
|
+
out[1] = 0;
|
|
455
|
+
return out;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* A quantity with magnitude and direction in two dimensions.
|
|
460
|
+
* @see [Euclidean vector](https://en.wikipedia.org/wiki/Euclidean_vector)
|
|
461
|
+
*/
|
|
462
|
+
export default class Vector2 extends Float32Array implements Vector {
|
|
463
|
+
/**
|
|
464
|
+
* Creates a vector with the given values.
|
|
465
|
+
* @param x The first component.
|
|
466
|
+
* @param y The second component.
|
|
467
|
+
* @returns A new vector.
|
|
468
|
+
*/
|
|
469
|
+
public static fromValues(x: number, y: number): Vector2;
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Creates a vector with the given values.
|
|
473
|
+
* @param x The first component.
|
|
474
|
+
* @param y The second component.
|
|
475
|
+
* @param out The vector to store the result in.
|
|
476
|
+
* @returns A new vector.
|
|
477
|
+
*/
|
|
478
|
+
public static fromValues<T extends Vector2Like>(x: number, y: number, out: T): T;
|
|
479
|
+
|
|
480
|
+
public static fromValues<T extends Vector2Like>(x: number, y: number, out: T = new Vector2() as T): T {
|
|
481
|
+
return fromValues(x, y, out);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Creates a two-dimensional zero vector.
|
|
486
|
+
* @see [Euclidean vector](https://en.wikipedia.org/wiki/Euclidean_vector)
|
|
487
|
+
*/
|
|
488
|
+
public constructor() {
|
|
489
|
+
super(2);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Determines whether this vector is roughly equivalent to another.
|
|
494
|
+
* @param vector The other vector.
|
|
495
|
+
* @returns Whether the vectors are equivalent.
|
|
496
|
+
*/
|
|
497
|
+
public equals(vector: Vector2Like): boolean {
|
|
498
|
+
return equals(this, vector);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Determines whether this vector is exactly equivalent to another.
|
|
503
|
+
* @param vector The other vector.
|
|
504
|
+
* @returns Whether the vectors are equivalent.
|
|
505
|
+
*/
|
|
506
|
+
public exactEquals(vector: Vector2Like): boolean {
|
|
507
|
+
return exactEquals(this, vector);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Adds another vector to this one.
|
|
512
|
+
* @param vector The other vector.
|
|
513
|
+
* @returns The sum of the vectors.
|
|
514
|
+
*/
|
|
515
|
+
public add(vector: Vector2Like): Vector2;
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Adds another vector to this one.
|
|
519
|
+
* @param vector The other vector.
|
|
520
|
+
* @param out The vector to store the result in.
|
|
521
|
+
* @returns The sum of the vectors.
|
|
522
|
+
*/
|
|
523
|
+
public add<T extends Vector2Like>(vector: Vector2Like, out: T): T;
|
|
524
|
+
|
|
525
|
+
public add<T extends Vector2Like>(vector: Vector2Like, out: T = new Vector2() as T): T {
|
|
526
|
+
return add(this, vector, out);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Creates a copy of this vector.
|
|
531
|
+
* @returns A copy of this vector.
|
|
532
|
+
*/
|
|
533
|
+
public clone(): Vector2 {
|
|
534
|
+
// TODO: `out` parameter.
|
|
535
|
+
return copy(this, new Vector2());
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Copies the values of another vector into this one.
|
|
540
|
+
* @param vector The vector to copy.
|
|
541
|
+
* @returns This vector.
|
|
542
|
+
*/
|
|
543
|
+
public copy(vector: Vector2Like): this {
|
|
544
|
+
return copy(vector, this);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Multiplies the components in this vector by the corresponding components in another.
|
|
549
|
+
* @param vector The other vector.
|
|
550
|
+
* @returns The product of the vectors.
|
|
551
|
+
*/
|
|
552
|
+
public multiply(vector: Vector2Like): Vector2;
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Multiplies the components in this vector by the corresponding components in another.
|
|
556
|
+
* @param vector The other vector.
|
|
557
|
+
* @param out The vector to store the result in.
|
|
558
|
+
* @returns The product of the vectors.
|
|
559
|
+
*/
|
|
560
|
+
public multiply<T extends Vector2Like>(vector: Vector2Like, out: T): T;
|
|
561
|
+
|
|
562
|
+
public multiply<T extends Vector2Like>(vector: Vector2Like, out: T = new Vector2() as T): T {
|
|
563
|
+
return multiply(this, vector, out);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Divides this vector by another.
|
|
568
|
+
* @param vector The other vector.
|
|
569
|
+
* @returns The quotient of the vectors.
|
|
570
|
+
*/
|
|
571
|
+
public divide(vector: Vector2Like): Vector2;
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Divides this vector by another.
|
|
575
|
+
* @param vector The other vector.
|
|
576
|
+
* @param out The vector to store the result in.
|
|
577
|
+
* @returns The quotient of the vectors.
|
|
578
|
+
*/
|
|
579
|
+
public divide<T extends Vector2Like>(vector: Vector2Like, out: T): T;
|
|
580
|
+
|
|
581
|
+
public divide<T extends Vector2Like>(vector: Vector2Like, out: T = new Vector2() as T): T {
|
|
582
|
+
return divide(this, vector, out);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Subtracts another vector from this one.
|
|
587
|
+
* @param vector The other vector.
|
|
588
|
+
* @returns The difference between the vectors.
|
|
589
|
+
*/
|
|
590
|
+
public subtract(vector: Vector2Like): Vector2;
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* Subtracts another vector from this one.
|
|
594
|
+
* @param vector The other vector.
|
|
595
|
+
* @param out The vector to store the result in.
|
|
596
|
+
* @returns The difference between the vectors.
|
|
597
|
+
*/
|
|
598
|
+
public subtract<T extends Vector2Like>(vector: Vector2Like, out: T): T;
|
|
599
|
+
|
|
600
|
+
public subtract<T extends Vector2Like>(vector: Vector2Like, out: T = new Vector2() as T): T {
|
|
601
|
+
return subtract(this, vector, out);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Rounds up the components of this vector.
|
|
606
|
+
* @returns The rounded vector.
|
|
607
|
+
*/
|
|
608
|
+
public ceil(): Vector2;
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Rounds up the components of this vector.
|
|
612
|
+
* @param out The vector to store the result in.
|
|
613
|
+
* @returns The rounded vector.
|
|
614
|
+
*/
|
|
615
|
+
public ceil<T extends Vector2Like>(out: T): T;
|
|
616
|
+
|
|
617
|
+
public ceil<T extends Vector2Like>(out: T = new Vector2() as T): T {
|
|
618
|
+
return ceil(this, out);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Rounds down the components of this vector.
|
|
623
|
+
* @returns The rounded vector.
|
|
624
|
+
*/
|
|
625
|
+
public floor(): Vector2;
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* Rounds down the components of this vector.
|
|
629
|
+
* @param out The vector to store the result in.
|
|
630
|
+
* @returns The rounded vector.
|
|
631
|
+
*/
|
|
632
|
+
public floor<T extends Vector2Like>(out: T): T;
|
|
633
|
+
|
|
634
|
+
public floor<T extends Vector2Like>(out: T = new Vector2() as T): T {
|
|
635
|
+
return floor(this, out);
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* Rounds the components of this vector.
|
|
640
|
+
* @returns The rounded vector.
|
|
641
|
+
*/
|
|
642
|
+
public round(): Vector2;
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* Rounds the components of this vector.
|
|
646
|
+
* @param out The vector to store the result in.
|
|
647
|
+
* @returns The rounded vector.
|
|
648
|
+
*/
|
|
649
|
+
public round<T extends Vector2Like>(out: T): T;
|
|
650
|
+
|
|
651
|
+
public round<T extends Vector2Like>(out: T = new Vector2() as T): T {
|
|
652
|
+
return round(this, out);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* Returns the minimum of this and another vector.
|
|
657
|
+
* @param vector The other vector.
|
|
658
|
+
* @returns The minimum.
|
|
659
|
+
*/
|
|
660
|
+
public min(vector: Vector2Like): Vector2;
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* Returns the minimum of this and another vector.
|
|
664
|
+
* @param vector The other vector.
|
|
665
|
+
* @param out The vector to store the result in.
|
|
666
|
+
* @returns The minimum.
|
|
667
|
+
*/
|
|
668
|
+
public min<T extends Vector2Like>(vector: Vector2Like, out: T): T;
|
|
669
|
+
|
|
670
|
+
public min<T extends Vector2Like>(vector: Vector2Like, out: T = new Vector2() as T): T {
|
|
671
|
+
return min(this, vector, out);
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
/**
|
|
675
|
+
* Returns the maximum of this and another vector.
|
|
676
|
+
* @param vector The other vector.
|
|
677
|
+
* @returns The maximum.
|
|
678
|
+
*/
|
|
679
|
+
public max(vector: Vector2Like): Vector2;
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* Returns the maximum of this and another vector.
|
|
683
|
+
* @param vector The other vector.
|
|
684
|
+
* @param out The vector to store the result in.
|
|
685
|
+
* @returns The maximum.
|
|
686
|
+
*/
|
|
687
|
+
public max<T extends Vector2Like>(vector: Vector2Like, out: T): T;
|
|
688
|
+
|
|
689
|
+
public max<T extends Vector2Like>(vector: Vector2Like, out: T = new Vector2() as T): T {
|
|
690
|
+
return max(this, vector, out);
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Scales this vector by a scalar.
|
|
695
|
+
* @param scalar The scalar.
|
|
696
|
+
* @returns The scaled vector.
|
|
697
|
+
*/
|
|
698
|
+
public scale(scalar: number): Vector2;
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* Scales this vector by a scalar.
|
|
702
|
+
* @param scalar The scalar.
|
|
703
|
+
* @param out The vector to store the result in.
|
|
704
|
+
* @returns The scaled vector.
|
|
705
|
+
*/
|
|
706
|
+
public scale<T extends Vector2Like>(scalar: number, out: T): T;
|
|
707
|
+
|
|
708
|
+
public scale<T extends Vector2Like>(scalar: number, out: T = new Vector2() as T): T {
|
|
709
|
+
return scale(this, scalar, out);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* Adds another vector to this one after scaling the other by a scalar.
|
|
714
|
+
* @param vector The other vector.
|
|
715
|
+
* @param scalar The scalar.
|
|
716
|
+
* @returns The sum.
|
|
717
|
+
*/
|
|
718
|
+
public scaleAndAdd(vector: Vector2Like, scalar: number): Vector2;
|
|
719
|
+
|
|
720
|
+
/**
|
|
721
|
+
* Adds another vector to this one after scaling the other by a scalar.
|
|
722
|
+
* @param vector The other vector.
|
|
723
|
+
* @param scalar The scalar.
|
|
724
|
+
* @param out The vector to store the result in.
|
|
725
|
+
* @returns The sum.
|
|
726
|
+
*/
|
|
727
|
+
public scaleAndAdd<T extends Vector2Like>(vector: Vector2Like, scalar: number, out: T): T;
|
|
728
|
+
|
|
729
|
+
public scaleAndAdd<T extends Vector2Like>(vector: Vector2Like, scalar: number, out: T = new Vector2() as T): T {
|
|
730
|
+
return scaleAndAdd(this, vector, scalar, out);
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* Calculates the Euclidean distance from this vector to another.
|
|
735
|
+
* @param vector The other vector.
|
|
736
|
+
* @returns The distance.
|
|
737
|
+
* @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
|
|
738
|
+
*/
|
|
739
|
+
public distance(vector: Vector2Like): number {
|
|
740
|
+
return distance(this, vector);
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/**
|
|
744
|
+
* Calculates the squared Euclidean distance from this vector to another.
|
|
745
|
+
* @param vector The other vector.
|
|
746
|
+
* @returns The squared distance.
|
|
747
|
+
* @see [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)
|
|
748
|
+
*/
|
|
749
|
+
public squaredDistance(vector: Vector2Like): number {
|
|
750
|
+
return squaredDistance(this, vector);
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/** The magnitude (length) of this vector. */
|
|
754
|
+
public get magnitude(): number {
|
|
755
|
+
return getMagnitude(this);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/** The squared magnitude (length) of this vector. */
|
|
759
|
+
public get squaredMagnitude(): number {
|
|
760
|
+
return getSquaredMagnitude(this);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Negates this vector.
|
|
765
|
+
* @returns The negated vector.
|
|
766
|
+
*/
|
|
767
|
+
public negate(): Vector2;
|
|
768
|
+
|
|
769
|
+
/**
|
|
770
|
+
* Negates this vector.
|
|
771
|
+
* @param out The vector to store the result in.
|
|
772
|
+
* @returns The negated vector.
|
|
773
|
+
*/
|
|
774
|
+
public negate<T extends Vector2Like>(out: T): T;
|
|
775
|
+
|
|
776
|
+
public negate<T extends Vector2Like>(out: T = new Vector2() as T): T {
|
|
777
|
+
return negate(this, out);
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* Calculates the multiplicative inverse of the components of this vector.
|
|
782
|
+
* @returns The inverted vector.
|
|
783
|
+
*/
|
|
784
|
+
public inverse(): Vector2;
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Calculates the multiplicative inverse of the components of this vector.
|
|
788
|
+
* @param out The vector to store the result in.
|
|
789
|
+
* @returns The inverted vector.
|
|
790
|
+
*/
|
|
791
|
+
public inverse<T extends Vector2Like>(out: T): T;
|
|
792
|
+
|
|
793
|
+
public inverse<T extends Vector2Like>(out: T = new Vector2() as T): T {
|
|
794
|
+
return inverse(this, out);
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* Normalizes this vector.
|
|
799
|
+
* @returns The normalized vector.
|
|
800
|
+
* @see [Unit vector](https://en.wikipedia.org/wiki/Unit_vector)
|
|
801
|
+
*/
|
|
802
|
+
public normalize(): Vector2;
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* Normalizes this vector.
|
|
806
|
+
* @param out The vector to store the result in.
|
|
807
|
+
* @returns The normalized vector.
|
|
808
|
+
* @see [Unit vector](https://en.wikipedia.org/wiki/Unit_vector)
|
|
809
|
+
*/
|
|
810
|
+
public normalize<T extends Vector2Like>(out: T): T;
|
|
811
|
+
|
|
812
|
+
public normalize<T extends Vector2Like>(out: T = new Vector2() as T): T {
|
|
813
|
+
return normalize(this, out);
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* Calculates the dot product of this and another vector.
|
|
818
|
+
* @param vector The other vector.
|
|
819
|
+
* @returns The dot product.
|
|
820
|
+
* @see [Dot product](https://en.wikipedia.org/wiki/Dot_product)
|
|
821
|
+
*/
|
|
822
|
+
public dot(vector: Vector2Like): number {
|
|
823
|
+
return dot(this, vector);
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* Calculates the cross product of this and another vector.
|
|
828
|
+
* @param vector The other vector.
|
|
829
|
+
* @returns The cross product.
|
|
830
|
+
* @see [Cross product](https://en.wikipedia.org/wiki/Cross_product)
|
|
831
|
+
*/
|
|
832
|
+
public cross(vector: Vector2Like): Vector3;
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* Calculates the cross product of this and another vector.
|
|
836
|
+
* @param vector The other vector.
|
|
837
|
+
* @param out The vector to store the result in.
|
|
838
|
+
* @returns The cross product.
|
|
839
|
+
* @see [Cross product](https://en.wikipedia.org/wiki/Cross_product)
|
|
840
|
+
*/
|
|
841
|
+
public cross<T extends Vector3Like>(vector: Vector2Like, out: T): T;
|
|
842
|
+
|
|
843
|
+
public cross<T extends Vector3Like>(vector: Vector2Like, out: T = new Vector3() as T): T {
|
|
844
|
+
return cross(this, vector, out);
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
/**
|
|
848
|
+
* Performs a linear interpolation between this and another vector.
|
|
849
|
+
* @param vector The other vector.
|
|
850
|
+
* @param t The interpolation amount (in `[0,1]`).
|
|
851
|
+
* @returns The interpolated vector.
|
|
852
|
+
* @see [Linear interpolation](https://en.wikipedia.org/wiki/Linear_interpolation)
|
|
853
|
+
*/
|
|
854
|
+
public lerp(vector: Vector2Like, t: number): Vector2;
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* Performs a linear interpolation between this and another vector.
|
|
858
|
+
* @param vector The other vector.
|
|
859
|
+
* @param t The interpolation amount (in `[0,1]`).
|
|
860
|
+
* @param out The vector to store the result in.
|
|
861
|
+
* @returns The interpolated vector.
|
|
862
|
+
* @see [Linear interpolation](https://en.wikipedia.org/wiki/Linear_interpolation)
|
|
863
|
+
*/
|
|
864
|
+
public lerp<T extends Vector2Like>(vector: Vector2Like, t: number, out: T): T;
|
|
865
|
+
|
|
866
|
+
public lerp<T extends Vector2Like>(vector: Vector2Like, t: number, out: T = new Vector2() as T): T {
|
|
867
|
+
return lerp(this, vector, t, out);
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
* Sets this vector to a random value with the given magnitude.
|
|
872
|
+
* @param magnitude The magnitude.
|
|
873
|
+
* @returns This vector.
|
|
874
|
+
*/
|
|
875
|
+
public random(magnitude = 1): this {
|
|
876
|
+
return random(magnitude, this);
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
/**
|
|
880
|
+
* Transforms this vector by a two-by-two matrix.
|
|
881
|
+
* @param matrix The matrix.
|
|
882
|
+
* @returns The transformed vector.
|
|
883
|
+
* @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
|
|
884
|
+
*/
|
|
885
|
+
public transformMatrix2(matrix: Matrix2Like): Vector2;
|
|
886
|
+
|
|
887
|
+
/**
|
|
888
|
+
* Transforms this vector by a two-by-two matrix.
|
|
889
|
+
* @param matrix The matrix.
|
|
890
|
+
* @param out The vector to store the result in.
|
|
891
|
+
* @returns The transformed vector.
|
|
892
|
+
* @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
|
|
893
|
+
*/
|
|
894
|
+
public transformMatrix2<T extends Vector2Like>(matrix: Matrix2Like, out: T): T;
|
|
895
|
+
|
|
896
|
+
public transformMatrix2<T extends Vector2Like>(matrix: Matrix2Like, out: T = new Vector2() as T): T {
|
|
897
|
+
return transformMatrix2(this, matrix, out);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* Transforms this vector by a three-by-three matrix.
|
|
902
|
+
* @param matrix The matrix.
|
|
903
|
+
* @returns The transformed vector.
|
|
904
|
+
* @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
|
|
905
|
+
*/
|
|
906
|
+
public transformMatrix3(matrix: Matrix3Like): Vector2;
|
|
907
|
+
|
|
908
|
+
/**
|
|
909
|
+
* Transforms this vector by a three-by-three matrix.
|
|
910
|
+
* @param matrix The matrix.
|
|
911
|
+
* @param out The vector to store the result in.
|
|
912
|
+
* @returns The transformed vector.
|
|
913
|
+
* @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
|
|
914
|
+
*/
|
|
915
|
+
public transformMatrix3<T extends Vector2Like>(matrix: Matrix3Like, out: T): T;
|
|
916
|
+
|
|
917
|
+
public transformMatrix3<T extends Vector2Like>(matrix: Matrix3Like, out: T = new Vector2() as T): T {
|
|
918
|
+
return transformMatrix3(this, matrix, out);
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
/**
|
|
922
|
+
* Transforms this vector by a four-by-four matrix.
|
|
923
|
+
* @param matrix The matrix.
|
|
924
|
+
* @returns The transformed vector.
|
|
925
|
+
* @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
|
|
926
|
+
*/
|
|
927
|
+
public transformMatrix4(matrix: Matrix4Like): Vector2;
|
|
928
|
+
|
|
929
|
+
/**
|
|
930
|
+
* Transforms this vector by a four-by-four matrix.
|
|
931
|
+
* @param matrix The matrix.
|
|
932
|
+
* @param out The vector to store the result in.
|
|
933
|
+
* @returns The transformed vector.
|
|
934
|
+
* @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
|
|
935
|
+
*/
|
|
936
|
+
public transformMatrix4<T extends Vector2Like>(matrix: Matrix4Like, out: T): T;
|
|
937
|
+
|
|
938
|
+
public transformMatrix4<T extends Vector2Like>(matrix: Matrix4Like, out: T = new Vector2() as T): T {
|
|
939
|
+
return transformMatrix4(this, matrix, out);
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/**
|
|
943
|
+
* Rotates this vector.
|
|
944
|
+
* @param origin The origin of the rotation.
|
|
945
|
+
* @param radians The angle of rotation in radians.
|
|
946
|
+
* @returns The rotated vector.
|
|
947
|
+
*/
|
|
948
|
+
public rotate(origin: Vector2Like, radians: number): Vector2;
|
|
949
|
+
|
|
950
|
+
/**
|
|
951
|
+
* Rotates this vector.
|
|
952
|
+
* @param origin The origin of the rotation.
|
|
953
|
+
* @param radians The angle of rotation in radians.
|
|
954
|
+
* @param out The vector to store the result in.
|
|
955
|
+
* @returns The rotated vector.
|
|
956
|
+
*/
|
|
957
|
+
public rotate<T extends Vector2Like>(origin: Vector2Like, radians: number, out: T): T;
|
|
958
|
+
|
|
959
|
+
public rotate<T extends Vector2Like>(origin: Vector2Like, radians: number, out: T = new Vector2() as T): T {
|
|
960
|
+
return rotate(this, origin, radians, out);
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
/**
|
|
964
|
+
* Gets the angle from this to another vector in radians.
|
|
965
|
+
* @param vector The other vector.
|
|
966
|
+
* @returns The angular distance.
|
|
967
|
+
*/
|
|
968
|
+
public angle(vector: Vector2Like): number {
|
|
969
|
+
return angle(this, vector);
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
/**
|
|
973
|
+
* Sets this to the zero vector.
|
|
974
|
+
* @returns This vector.
|
|
975
|
+
*/
|
|
976
|
+
public zero(): this {
|
|
977
|
+
return zero(this);
|
|
978
|
+
}
|
|
979
|
+
}
|