toosoon-utils 4.1.8 → 4.2.0
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 +97 -885
- package/lib/constants.d.ts +0 -1
- package/lib/constants.js +3 -1
- package/lib/extras/color-scale/ColorScale.d.ts +2 -2
- package/lib/extras/color-scale/ColorScale.js +2 -2
- package/lib/extras/curves/CatmullRomCurve.d.ts +20 -5
- package/lib/extras/curves/CatmullRomCurve.js +23 -5
- package/lib/extras/curves/CatmullRomCurve3.d.ts +101 -0
- package/lib/extras/curves/CatmullRomCurve3.js +122 -0
- package/lib/extras/curves/CubicBezierCurve.d.ts +20 -5
- package/lib/extras/curves/CubicBezierCurve.js +23 -5
- package/lib/extras/curves/CubicBezierCurve3.d.ts +101 -0
- package/lib/extras/curves/CubicBezierCurve3.js +122 -0
- package/lib/extras/curves/Curve.d.ts +23 -24
- package/lib/extras/curves/Curve.js +19 -26
- package/lib/extras/curves/EllipseCurve.d.ts +21 -5
- package/lib/extras/curves/EllipseCurve.js +55 -6
- package/lib/extras/curves/LineCurve.d.ts +34 -10
- package/lib/extras/curves/LineCurve.js +35 -13
- package/lib/extras/curves/LineCurve3.d.ts +87 -0
- package/lib/extras/curves/LineCurve3.js +108 -0
- package/lib/extras/curves/PolylineCurve.d.ts +9 -8
- package/lib/extras/curves/PolylineCurve.js +6 -6
- package/lib/extras/curves/PolylineCurve3.d.ts +28 -0
- package/lib/extras/curves/PolylineCurve3.js +39 -0
- package/lib/extras/curves/QuadraticBezierCurve.d.ts +19 -5
- package/lib/extras/curves/QuadraticBezierCurve.js +22 -5
- package/lib/extras/curves/QuadraticBezierCurve3.d.ts +84 -0
- package/lib/extras/curves/QuadraticBezierCurve3.js +102 -0
- package/lib/extras/curves/SplineCurve.d.ts +12 -8
- package/lib/extras/curves/SplineCurve.js +9 -6
- package/lib/extras/curves/SplineCurve3.d.ts +28 -0
- package/lib/extras/curves/SplineCurve3.js +41 -0
- package/lib/extras/curves/index.d.ts +6 -0
- package/lib/extras/curves/index.js +6 -0
- package/lib/extras/geometry/Matrix2.d.ts +1 -0
- package/lib/extras/geometry/Matrix2.js +230 -0
- package/lib/extras/geometry/Matrix4.d.ts +1 -0
- package/lib/extras/geometry/Matrix4.js +632 -0
- package/lib/extras/geometry/Vector.d.ts +42 -0
- package/lib/extras/geometry/Vector.js +1 -0
- package/lib/extras/geometry/Vector2.d.ts +480 -0
- package/lib/extras/geometry/Vector2.js +709 -0
- package/lib/extras/geometry/Vector3.d.ts +486 -0
- package/lib/extras/geometry/Vector3.js +765 -0
- package/lib/extras/geometry/index.d.ts +3 -0
- package/lib/extras/geometry/index.js +2 -0
- package/lib/extras/paths/Path.d.ts +24 -18
- package/lib/extras/paths/Path.js +48 -35
- package/lib/extras/paths/PathContext.d.ts +97 -67
- package/lib/extras/paths/PathContext.js +326 -183
- package/lib/extras/paths/PathSVG.d.ts +43 -31
- package/lib/extras/paths/PathSVG.js +68 -50
- package/lib/geometry.d.ts +0 -135
- package/lib/geometry.js +1 -219
- package/lib/maths.d.ts +54 -22
- package/lib/maths.js +77 -27
- package/lib/random.d.ts +12 -16
- package/lib/random.js +19 -27
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.d.ts +43 -1
- package/package.json +2 -1
|
@@ -0,0 +1,709 @@
|
|
|
1
|
+
import { EPSILON, PI } from '../../constants';
|
|
2
|
+
import { clamp, lerp } from '../../maths';
|
|
3
|
+
/**
|
|
4
|
+
* Utility class for manipulating a 2D vectors
|
|
5
|
+
*
|
|
6
|
+
* @exports
|
|
7
|
+
* @class Vector2
|
|
8
|
+
* @implements Vector
|
|
9
|
+
*/
|
|
10
|
+
export default class Vector2 {
|
|
11
|
+
isVector2 = true;
|
|
12
|
+
type = 'Vector2';
|
|
13
|
+
/**
|
|
14
|
+
* X-axis value of this vector
|
|
15
|
+
*/
|
|
16
|
+
x;
|
|
17
|
+
/**
|
|
18
|
+
* Y-axis value of this vector
|
|
19
|
+
*/
|
|
20
|
+
y;
|
|
21
|
+
/**
|
|
22
|
+
* @param {number} [x=0] X-axis value
|
|
23
|
+
* @param {number} [y=0] Y-axis value
|
|
24
|
+
*/
|
|
25
|
+
constructor(x = 0, y = 0) {
|
|
26
|
+
this.x = x;
|
|
27
|
+
this.y = y;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Set this vector values
|
|
31
|
+
*
|
|
32
|
+
* @param {number} x X-axis value
|
|
33
|
+
* @param {number} y Y-axis value
|
|
34
|
+
* @returns {this}
|
|
35
|
+
*/
|
|
36
|
+
set(x, y) {
|
|
37
|
+
this.x = x;
|
|
38
|
+
this.y = y;
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Set a given scalar value to all values of this vector
|
|
43
|
+
*
|
|
44
|
+
* @param {number} scalar Value to set for all vector values
|
|
45
|
+
* @returns {this}
|
|
46
|
+
*/
|
|
47
|
+
setScalar(scalar) {
|
|
48
|
+
this.x = scalar;
|
|
49
|
+
this.y = scalar;
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Set this vector X-axis value
|
|
54
|
+
*
|
|
55
|
+
* @param {number} x X-axis value to set
|
|
56
|
+
* @returns {this}
|
|
57
|
+
*/
|
|
58
|
+
setX(x) {
|
|
59
|
+
this.x = x;
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Set this vector Y-axis value
|
|
64
|
+
*
|
|
65
|
+
* @param {number} y Y-axis value to set
|
|
66
|
+
* @returns {this}
|
|
67
|
+
*/
|
|
68
|
+
setY(y) {
|
|
69
|
+
this.y = y;
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Set a given value of this vector
|
|
74
|
+
*
|
|
75
|
+
* @param {string|number} index `0` equals to `x`, `1` equals to `y`
|
|
76
|
+
* @param {number} value Value to set
|
|
77
|
+
* @returns {this}
|
|
78
|
+
*/
|
|
79
|
+
setValue(index, value) {
|
|
80
|
+
switch (index) {
|
|
81
|
+
case 'x':
|
|
82
|
+
case 0:
|
|
83
|
+
this.x = value;
|
|
84
|
+
break;
|
|
85
|
+
case 'y':
|
|
86
|
+
case 1:
|
|
87
|
+
this.y = value;
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Return a value from this vector
|
|
94
|
+
*
|
|
95
|
+
* @param {string|number} index `0` equals to `x`, `1` equals to `y`
|
|
96
|
+
* @returns {number}
|
|
97
|
+
*/
|
|
98
|
+
getValue(index) {
|
|
99
|
+
switch (index) {
|
|
100
|
+
case 'x':
|
|
101
|
+
case 0:
|
|
102
|
+
return this.x;
|
|
103
|
+
case 'y':
|
|
104
|
+
case 1:
|
|
105
|
+
return this.y;
|
|
106
|
+
default:
|
|
107
|
+
return NaN;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Add a given vector to this vector
|
|
112
|
+
*
|
|
113
|
+
* @param {Vector2|Point2} vector Vector to add
|
|
114
|
+
* @returns {this}
|
|
115
|
+
*/
|
|
116
|
+
add([x, y]) {
|
|
117
|
+
this.x += x;
|
|
118
|
+
this.y += y;
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Add a given scalar value to all values of this vector
|
|
123
|
+
*
|
|
124
|
+
* @param {number} scalar Scalar value to add
|
|
125
|
+
* @returns {this}
|
|
126
|
+
*/
|
|
127
|
+
addScalar(scalar) {
|
|
128
|
+
this.x += scalar;
|
|
129
|
+
this.y += scalar;
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Subtract a given vector to this vector
|
|
134
|
+
*
|
|
135
|
+
* @param {Vector2|Point2} vector Vector to subtract
|
|
136
|
+
* @returns {this}
|
|
137
|
+
*/
|
|
138
|
+
sub([x, y]) {
|
|
139
|
+
this.x -= x;
|
|
140
|
+
this.y -= y;
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Subtract a given scalar value to all values of this vector
|
|
145
|
+
*
|
|
146
|
+
* @param {number} scalar Scalar value to subtract
|
|
147
|
+
* @returns {this}
|
|
148
|
+
*/
|
|
149
|
+
subScalar(scalar) {
|
|
150
|
+
this.x -= scalar;
|
|
151
|
+
this.y -= scalar;
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Multiply a given vector to this vector
|
|
156
|
+
*
|
|
157
|
+
* @param {Vector2|Point2} vector Vector to multiply
|
|
158
|
+
* @returns {this}
|
|
159
|
+
*/
|
|
160
|
+
multiply([x, y]) {
|
|
161
|
+
this.x *= x;
|
|
162
|
+
this.y *= y;
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Multiply a given scalar value to all values of this vector
|
|
167
|
+
*
|
|
168
|
+
* @param {number} scalar Scalar value to multiply
|
|
169
|
+
* @returns {this}
|
|
170
|
+
*/
|
|
171
|
+
multiplyScalar(scalar) {
|
|
172
|
+
this.x *= scalar;
|
|
173
|
+
this.y *= scalar;
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Divide a given vector to this vector
|
|
178
|
+
*
|
|
179
|
+
* @param {Vector2|Point2} vector Vector to divide
|
|
180
|
+
* @returns {this}
|
|
181
|
+
*/
|
|
182
|
+
divide([x, y]) {
|
|
183
|
+
this.x /= x;
|
|
184
|
+
this.y /= y;
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Divide a given scalar value to all values of this vector
|
|
189
|
+
*
|
|
190
|
+
* @param {number} scalar Scalar value to multiply
|
|
191
|
+
* @returns {this}
|
|
192
|
+
*/
|
|
193
|
+
divideScalar(scalar) {
|
|
194
|
+
this.x /= scalar;
|
|
195
|
+
this.y /= scalar;
|
|
196
|
+
return this;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Set this vector values to the min values compared to a given vector
|
|
200
|
+
*
|
|
201
|
+
* @param {Vector2|Point2} vector Vector to compare values with
|
|
202
|
+
* @returns {this}
|
|
203
|
+
*/
|
|
204
|
+
min([x, y]) {
|
|
205
|
+
this.x = Math.min(this.x, x);
|
|
206
|
+
this.y = Math.min(this.y, y);
|
|
207
|
+
return this;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Set this vector values to the max values compared to a given vector
|
|
211
|
+
*
|
|
212
|
+
* @param {Vector2|Point2} vector Vector to compare values with
|
|
213
|
+
* @returns {this}
|
|
214
|
+
*/
|
|
215
|
+
max([x, y]) {
|
|
216
|
+
this.x = Math.max(this.x, x);
|
|
217
|
+
this.y = Math.max(this.y, y);
|
|
218
|
+
return this;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Clamp this vector values to given boundaries
|
|
222
|
+
*
|
|
223
|
+
* @param {Vector2|Point2} min Minimum boundaries
|
|
224
|
+
* @param {Vector2|Point2} max Maximum boundaries
|
|
225
|
+
* @returns {this}
|
|
226
|
+
*/
|
|
227
|
+
clamp([minX, minY], [maxX, maxY]) {
|
|
228
|
+
this.x = clamp(this.x, minX, maxX);
|
|
229
|
+
this.y = clamp(this.y, minY, maxY);
|
|
230
|
+
return this;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Clamp this vector values to given scalar values
|
|
234
|
+
*
|
|
235
|
+
* @param {Vector2|Point2} min Minimum scalar boundary
|
|
236
|
+
* @param {Vector2|Point2} max Maximum scalar boundary
|
|
237
|
+
* @returns {this}
|
|
238
|
+
*/
|
|
239
|
+
clampScalar(min, max) {
|
|
240
|
+
this.x = clamp(this.x, min, max);
|
|
241
|
+
this.y = clamp(this.y, min, max);
|
|
242
|
+
return this;
|
|
243
|
+
}
|
|
244
|
+
// public clampLength(min: number, max: number) {
|
|
245
|
+
// const length = this.length();
|
|
246
|
+
// return this.divideScalar(length || 1).multiplyScalar(clamp(length, min, max));
|
|
247
|
+
// }
|
|
248
|
+
/**
|
|
249
|
+
* Round down to the nearest integer value this vector values
|
|
250
|
+
*
|
|
251
|
+
* @returns {this}
|
|
252
|
+
*/
|
|
253
|
+
floor() {
|
|
254
|
+
this.x = Math.floor(this.x);
|
|
255
|
+
this.y = Math.floor(this.y);
|
|
256
|
+
return this;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Round up to the nearest integer value this vector values
|
|
260
|
+
*
|
|
261
|
+
* @returns {this}
|
|
262
|
+
*/
|
|
263
|
+
ceil() {
|
|
264
|
+
this.x = Math.ceil(this.x);
|
|
265
|
+
this.y = Math.ceil(this.y);
|
|
266
|
+
return this;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Round to the nearest integer value this vector values
|
|
270
|
+
*
|
|
271
|
+
* @returns {this}
|
|
272
|
+
*/
|
|
273
|
+
round() {
|
|
274
|
+
this.x = Math.round(this.x);
|
|
275
|
+
this.y = Math.round(this.y);
|
|
276
|
+
return this;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Remove any fractional digits of this vector values
|
|
280
|
+
*
|
|
281
|
+
* @returns {this}
|
|
282
|
+
*/
|
|
283
|
+
trunc() {
|
|
284
|
+
this.x = Math.trunc(this.x);
|
|
285
|
+
this.y = Math.trunc(this.y);
|
|
286
|
+
return this;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Set this vector values to their negative values
|
|
290
|
+
*
|
|
291
|
+
* @returns {this}
|
|
292
|
+
*/
|
|
293
|
+
negate() {
|
|
294
|
+
this.x = -this.x;
|
|
295
|
+
this.y = -this.y;
|
|
296
|
+
return this;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Rotate this vector around a given center by the given angle
|
|
300
|
+
*
|
|
301
|
+
* @param {Vector2|Point2} center Vector around which to rotate
|
|
302
|
+
* @param {number} angle Angle to rotate (in radians)
|
|
303
|
+
* @returns {this}
|
|
304
|
+
*/
|
|
305
|
+
rotateAround(center, angle) {
|
|
306
|
+
return this.set(...Vector2.rotate(this, center, angle));
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Interpolate this vector values between a given vector and this vector
|
|
310
|
+
*
|
|
311
|
+
* @param {Vector2|Point2} vector Vector to interpolate values towards
|
|
312
|
+
* @param {number} t Normalized time value to interpolate
|
|
313
|
+
* @returns {this}
|
|
314
|
+
*/
|
|
315
|
+
lerp([x, y], t) {
|
|
316
|
+
this.x += (x - this.x) * t;
|
|
317
|
+
this.y += (y - this.y) * t;
|
|
318
|
+
return this;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Convert this vector to a unit vector
|
|
322
|
+
*
|
|
323
|
+
* @returns {this}
|
|
324
|
+
*/
|
|
325
|
+
normalize() {
|
|
326
|
+
return this.divideScalar(this.length() || 1);
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Set this vector values to the same direction but with a given length
|
|
330
|
+
*
|
|
331
|
+
* @param {number} length Length value
|
|
332
|
+
* @returns {this}
|
|
333
|
+
*/
|
|
334
|
+
setLength(length) {
|
|
335
|
+
return this.normalize().multiplyScalar(length);
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Calculate the Euclidean length of this vector
|
|
339
|
+
*
|
|
340
|
+
* @returns {number} Computed Euclidean length
|
|
341
|
+
*/
|
|
342
|
+
length() {
|
|
343
|
+
return Vector2.length(this);
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Calculate the squared length of this vector
|
|
347
|
+
*
|
|
348
|
+
* @return {number} Computed squared length
|
|
349
|
+
*/
|
|
350
|
+
squaredLength() {
|
|
351
|
+
return Vector2.squaredLength(this);
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Calculate the Manhattan length of this vector
|
|
355
|
+
*
|
|
356
|
+
* @return {number} Computed Manhattan length
|
|
357
|
+
*/
|
|
358
|
+
manhattanLength() {
|
|
359
|
+
return Vector2.manhattanLength(this);
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Check if this vector is equal with a given vector
|
|
363
|
+
*
|
|
364
|
+
* @param {Vector2|Point2} vector Vector to check
|
|
365
|
+
* @returns {boolean} True if this vector is equal with the given vector, false otherwise
|
|
366
|
+
*/
|
|
367
|
+
equals(vector) {
|
|
368
|
+
return Vector2.equals(this, vector);
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Check if this vector is collinear with given vectors
|
|
372
|
+
*
|
|
373
|
+
* @param {Vector2|Point2} vector1 First vector to check
|
|
374
|
+
* @param {Vector2|Point2} vector2 Second vector to check
|
|
375
|
+
* @returns {boolean} True if this vector is collinear with the given vectors, false otherwise
|
|
376
|
+
*/
|
|
377
|
+
collinear(vector1, vector2) {
|
|
378
|
+
return Vector2.collinear(this, vector1, vector2);
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Calculate the dot product of a given vector with this vector
|
|
382
|
+
*
|
|
383
|
+
* @param {Vector2|Point2} vector Vector to compute the dot product with
|
|
384
|
+
* @returns {number} Computed dot product
|
|
385
|
+
*/
|
|
386
|
+
dot(vector) {
|
|
387
|
+
return Vector2.dot(this, vector);
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Calculate the cross product of a given vector with this vector
|
|
391
|
+
*
|
|
392
|
+
* @param {Vector2|Point2} vector Vector to compute the cross product with
|
|
393
|
+
* @returns {number} Computed cross product
|
|
394
|
+
*/
|
|
395
|
+
cross(vector) {
|
|
396
|
+
return Vector2.cross(this, vector);
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Calculate the angle of this vector with respect to the positive X-axis
|
|
400
|
+
*
|
|
401
|
+
* @returns {number} Computed angle (in radians)
|
|
402
|
+
*/
|
|
403
|
+
angle() {
|
|
404
|
+
return Vector2.angle(this);
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Calculate the angle between a given vector and this vector
|
|
408
|
+
*
|
|
409
|
+
* @param {Vector2|Point2} vector Vector to compute the angle with
|
|
410
|
+
* @returns {number} Computed angle (in radians)
|
|
411
|
+
*/
|
|
412
|
+
angleTo(vector) {
|
|
413
|
+
const denominator = Math.sqrt(this.squaredLength() * Vector2.squaredLength(vector));
|
|
414
|
+
if (denominator === 0)
|
|
415
|
+
return PI / 2;
|
|
416
|
+
const theta = this.dot(vector) / denominator;
|
|
417
|
+
return Math.acos(clamp(theta, -1, 1));
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Calculate the Euclidean distance from a given vector to this vector
|
|
421
|
+
*
|
|
422
|
+
* @param {Vector2|Point2} vector Vector to compute the distance to
|
|
423
|
+
* @returns {number} Computed Euclidean distance
|
|
424
|
+
*/
|
|
425
|
+
distanceTo(vector) {
|
|
426
|
+
return Vector2.distance(this, vector);
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Calculate the squared distance from a given vector to this vector
|
|
430
|
+
*
|
|
431
|
+
* @param {Vector2|Point2} vector Vector to compute the squared distance to
|
|
432
|
+
* @returns {number} Computed squared distance
|
|
433
|
+
*/
|
|
434
|
+
squaredDistanceTo(vector) {
|
|
435
|
+
return Vector2.squaredDistance(this, vector);
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Calculate the Manhattan distance from a given vector to this vector
|
|
439
|
+
*
|
|
440
|
+
* @param {Vector2|Point2} vector Vector to compute the Manhattan distance to
|
|
441
|
+
* @returns {number} Computed Manhattan distance
|
|
442
|
+
*/
|
|
443
|
+
manhattanDistanceTo(vector) {
|
|
444
|
+
return Vector2.manhattanDistance(this, vector);
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Return this vector values into an array
|
|
448
|
+
*
|
|
449
|
+
* @returns {Point2}
|
|
450
|
+
*/
|
|
451
|
+
toArray() {
|
|
452
|
+
return [this.x, this.y];
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Set this vector values from a given array
|
|
456
|
+
*
|
|
457
|
+
* @param {number[]} values Values to set
|
|
458
|
+
* @returns
|
|
459
|
+
*/
|
|
460
|
+
fromArray([x, y]) {
|
|
461
|
+
this.x = x;
|
|
462
|
+
this.y = y;
|
|
463
|
+
return this;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Set this vector values from given circular coordinates
|
|
467
|
+
*
|
|
468
|
+
* @param {number} angle Angle (in radians)
|
|
469
|
+
* @param {number} [radius] Radius of the circle
|
|
470
|
+
* @returns {this}
|
|
471
|
+
*/
|
|
472
|
+
fromCircularCoords(angle, radius) {
|
|
473
|
+
return this.set(...Vector2.fromCircularCoords(angle, radius));
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Copy the values of a given vector to this vector
|
|
477
|
+
*
|
|
478
|
+
* @param {Vector2|Point2} vector Vector to copy values from
|
|
479
|
+
* @returns {this}
|
|
480
|
+
*/
|
|
481
|
+
copy([x, y]) {
|
|
482
|
+
this.x = x;
|
|
483
|
+
this.y = y;
|
|
484
|
+
return this;
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Create a new 2D vector with copied values from this vector
|
|
488
|
+
*
|
|
489
|
+
* @returns {Vector2}
|
|
490
|
+
*/
|
|
491
|
+
clone() {
|
|
492
|
+
return new Vector2(this.x, this.y);
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* Add two vectors
|
|
496
|
+
*
|
|
497
|
+
* @param {Vector2|Point2} vector1 First vector
|
|
498
|
+
* @param {Vector2|Point2} vector2 Second vector
|
|
499
|
+
* @returns {Point2}
|
|
500
|
+
*/
|
|
501
|
+
static add([x1, y1], [x2, y2]) {
|
|
502
|
+
const x = x1 + x2;
|
|
503
|
+
const y = y1 + y2;
|
|
504
|
+
return [x, y];
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Subtract two vectors
|
|
508
|
+
*
|
|
509
|
+
* @param {Vector2|Point2} vector1 First vector
|
|
510
|
+
* @param {Vector2|Point2} vector2 Second vector
|
|
511
|
+
* @returns {Point2}
|
|
512
|
+
*/
|
|
513
|
+
static sub([x1, y1], [x2, y2]) {
|
|
514
|
+
const x = x1 - x2;
|
|
515
|
+
const y = y1 - y2;
|
|
516
|
+
return [x, y];
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Multiply two vectors
|
|
520
|
+
*
|
|
521
|
+
* @param {Vector2|Point2} vector1 First vector
|
|
522
|
+
* @param {Vector2|Point2} vector2 Second vector
|
|
523
|
+
* @returns {Point2}
|
|
524
|
+
*/
|
|
525
|
+
static multiply([x1, y1], [x2, y2]) {
|
|
526
|
+
const x = x1 * x2;
|
|
527
|
+
const y = y1 * y2;
|
|
528
|
+
return [x, y];
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Divide two vectors
|
|
532
|
+
*
|
|
533
|
+
* @param {Vector2|Point2} vector1 First vector
|
|
534
|
+
* @param {Vector2|Point2} vector2 Second vector
|
|
535
|
+
* @returns {Point2}
|
|
536
|
+
*/
|
|
537
|
+
static divide([x1, y1], [x2, y2]) {
|
|
538
|
+
const x = x1 / x2;
|
|
539
|
+
const y = y1 / y2;
|
|
540
|
+
return [x, y];
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Rotate a vector around a given center by a given angle
|
|
544
|
+
*
|
|
545
|
+
* @param {Vector2|Point2} vector Vector to rotate
|
|
546
|
+
* @param {Vector2|Point2} center Vector around which to rotate
|
|
547
|
+
* @param {number} angle Angle to rotate (in radians)
|
|
548
|
+
* @returns {Point2}
|
|
549
|
+
*/
|
|
550
|
+
static rotate([vx, vy], [cx, cy], angle) {
|
|
551
|
+
const cos = Math.cos(angle);
|
|
552
|
+
const sin = Math.sin(angle);
|
|
553
|
+
const ox = vx - cx;
|
|
554
|
+
const oy = vy - cy;
|
|
555
|
+
const x = cx + ox * cos - oy * sin;
|
|
556
|
+
const y = cy + ox * sin + oy * cos;
|
|
557
|
+
return [x, y];
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Interpolate a point between two vectors
|
|
561
|
+
*
|
|
562
|
+
* @param {number} t Normalized time value to interpolate
|
|
563
|
+
* @param {Vector2|Point2} min Minimum boundaries
|
|
564
|
+
* @param {Vector2|Point2} max Maximum boundaries
|
|
565
|
+
* @returns {Point2}
|
|
566
|
+
*/
|
|
567
|
+
static lerp(t, [x1, y1], [x2, y2]) {
|
|
568
|
+
const x = lerp(t, x1, x2);
|
|
569
|
+
const y = lerp(t, y1, y2);
|
|
570
|
+
return [x, y];
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Check if two vectors are equal to each other
|
|
574
|
+
*
|
|
575
|
+
* @param {Vector2|Point2} vector1 First vector
|
|
576
|
+
* @param {Vector2|Point2} vector2 Second vector
|
|
577
|
+
* @returns {boolean} True if the given vectors are equal, false otherwise
|
|
578
|
+
*/
|
|
579
|
+
static equals([x1, y1], [x2, y2]) {
|
|
580
|
+
return x1 === x2 && y1 === y2;
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Check if three vectors are collinear (aligned on the same line)
|
|
584
|
+
*
|
|
585
|
+
* @param {Vector2|Point2} vector1 First vector
|
|
586
|
+
* @param {Vector2|Point2} vector2 Second vector
|
|
587
|
+
* @param {Vector2|Point2} vector3 Third vector
|
|
588
|
+
* @returns {boolean} True if the given vectors are collinear, false otherwise
|
|
589
|
+
*/
|
|
590
|
+
static collinear([x1, y1], [x2, y2], [x3, y3]) {
|
|
591
|
+
return Math.abs((x2 - x1) * (y3 - y2) - (y2 - y1) * (x3 - x2)) <= EPSILON;
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Calculate the dot product of two vectors
|
|
595
|
+
*
|
|
596
|
+
* @param {Vector2|Point2} vector1 First vector
|
|
597
|
+
* @param {Vector2|Point2} vector2 Second vector
|
|
598
|
+
* @returns {number} Computed dot product
|
|
599
|
+
*/
|
|
600
|
+
static dot([x1, y1], [x2, y2]) {
|
|
601
|
+
return x1 * x2 + y1 * y2;
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Calculate the cross product of two vectors
|
|
605
|
+
*
|
|
606
|
+
* @param {Vector2|Point2} vector1 First vector
|
|
607
|
+
* @param {Vector2|Point2} vector2 Second vector
|
|
608
|
+
* @returns {number} Computed cross product
|
|
609
|
+
*/
|
|
610
|
+
static cross([x1, y1], [x2, y2]) {
|
|
611
|
+
return x1 * x2 - y1 * y2;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Calculate the angle of a given vector with respect to the positive X-axis
|
|
615
|
+
*
|
|
616
|
+
* @param {Vector2|Point2} vector Vector to compute angle from
|
|
617
|
+
* @returns {number} Computed angle (in radians)
|
|
618
|
+
*/
|
|
619
|
+
static angle([x, y]) {
|
|
620
|
+
return PI + Math.atan2(-y, -x);
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Calculate the Euclidean distance between two vectors
|
|
624
|
+
*
|
|
625
|
+
* @param {Vector2|Point2} vector1 First vector
|
|
626
|
+
* @param {Vector2|Point2} vector2 Second vector
|
|
627
|
+
* @returns {number} Computed Euclidean distance
|
|
628
|
+
*/
|
|
629
|
+
static distance(vector1, vector2) {
|
|
630
|
+
return Math.sqrt(Vector2.squaredDistance(vector1, vector2));
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Calculate the squared distance between two vectors
|
|
634
|
+
*
|
|
635
|
+
* @param {Vector2|Point2} vector1 First vector
|
|
636
|
+
* @param {Vector2|Point2} vector2 Second vector
|
|
637
|
+
* @returns {number} Computed squared distance
|
|
638
|
+
*/
|
|
639
|
+
static squaredDistance([x1, y1], [x2, y2]) {
|
|
640
|
+
const dx = x1 - x2;
|
|
641
|
+
const dy = y1 - y2;
|
|
642
|
+
return dx * dx + dy * dy;
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Calculate the Manhattan distance between two vectors
|
|
646
|
+
*
|
|
647
|
+
* @param {Vector2|Point2} vector1 First vector
|
|
648
|
+
* @param {Vector2|Point2} vector2 Second vector
|
|
649
|
+
* @return {number} Computed Manhattan distance
|
|
650
|
+
*/
|
|
651
|
+
static manhattanDistance([x1, y1], [x2, y2]) {
|
|
652
|
+
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* Calculate the Euclidean length of a vector
|
|
656
|
+
*
|
|
657
|
+
* @param {Vector2|Point2} vector Vector to compute Euclidean length from
|
|
658
|
+
* @returns {number} Computed Euclidean length
|
|
659
|
+
*/
|
|
660
|
+
static length(vector) {
|
|
661
|
+
return Math.sqrt(Vector2.squaredLength(vector));
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* Calculate the squared length of a vector
|
|
665
|
+
*
|
|
666
|
+
* @param {Vector2|Point2} vector Vector to compute squared length from
|
|
667
|
+
* @returns {number} Computed squared length
|
|
668
|
+
*/
|
|
669
|
+
static squaredLength([x, y]) {
|
|
670
|
+
return x * x + y * y;
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Calculate the Manhattan length of a vector
|
|
674
|
+
*
|
|
675
|
+
* @param {Vector2|Point2} vector Vector to compute Manhattan length from
|
|
676
|
+
* @return {number} Computed Manhattan length
|
|
677
|
+
*/
|
|
678
|
+
static manhattanLength([x, y]) {
|
|
679
|
+
return Math.abs(x) + Math.abs(y);
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Convert circular coordinates to a 2D point on the surface of a circle
|
|
683
|
+
*
|
|
684
|
+
* @param {number} angle Angle (in radians)
|
|
685
|
+
* @param {number} [radius=1] Radius of the circle
|
|
686
|
+
* @returns {Point2}
|
|
687
|
+
*/
|
|
688
|
+
static fromCircularCoords(angle, radius = 1) {
|
|
689
|
+
const x = radius * Math.cos(angle);
|
|
690
|
+
const y = radius * Math.sin(angle);
|
|
691
|
+
return [x, y];
|
|
692
|
+
}
|
|
693
|
+
set width(width) {
|
|
694
|
+
this.x = width;
|
|
695
|
+
}
|
|
696
|
+
get width() {
|
|
697
|
+
return this.x;
|
|
698
|
+
}
|
|
699
|
+
set height(height) {
|
|
700
|
+
this.y = height;
|
|
701
|
+
}
|
|
702
|
+
get height() {
|
|
703
|
+
return this.y;
|
|
704
|
+
}
|
|
705
|
+
*[Symbol.iterator]() {
|
|
706
|
+
yield this.x;
|
|
707
|
+
yield this.y;
|
|
708
|
+
}
|
|
709
|
+
}
|