-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.ts
363 lines (321 loc) · 8.91 KB
/
vector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* A 2D vector on a plane.
*/
export class Vector {
/**
* A (0, 0) vector
*/
public static get Zero() {
return new Vector(0, 0);
}
/**
* A (1, 1) vector
*/
public static get One() {
return new Vector(1, 1);
}
/**
* A (0.5, 0.5) vector
*/
public static get Half() {
return new Vector(0.5, 0.5);
}
/**
* A unit vector pointing up (0, -1)
*/
public static get Up() {
return new Vector(0, -1);
}
/**
* A unit vector pointing down (0, 1)
*/
public static get Down() {
return new Vector(0, 1);
}
/**
* A unit vector pointing left (-1, 0)
*/
public static get Left() {
return new Vector(-1, 0);
}
/**
* A unit vector pointing right (1, 0)
*/
public static get Right() {
return new Vector(1, 0);
}
/**
* Returns a vector of unit length in the direction of the specified angle in Radians.
* @param angle The angle to generate the vector
*/
public static fromAngle(angle: number) {
return new Vector(Math.cos(angle), Math.sin(angle));
}
/**
* Checks if vector is not null, undefined, or if any of its components are NaN or Infinity.
*/
public static isValid(vec: Vector) {
if (vec === null || vec === undefined) {
return false;
}
if (isNaN(vec.x) || isNaN(vec.y)) {
return false;
}
if (vec.x === Infinity || vec.y === Infinity || vec.x === -Infinity || vec.y === -Infinity) {
return false;
}
return true;
}
/**
* Calculates distance between two Vectors
* @param vec1
* @param vec2
*/
public static distance(vec1: Vector, vec2: Vector) {
return Math.sqrt(Math.pow(vec1.x - vec2.x, 2) + Math.pow(vec1.y - vec2.y, 2));
}
/**
* @param x X component of the Vector
* @param y Y component of the Vector
*/
constructor(x: number, y: number) {
this._x = x;
this._y = y;
}
protected _x = 0;
/**
* Get the x component of the vector
*/
public get x(): number {
return this._x;
}
/**
* Set the x component, THIS MUTATES the current vector. It is usually better to create a new vector.
* @warning **Be very careful setting components on shared vectors, mutating shared vectors can cause hard to find bugs**
*/
public set x(val: number) {
this._x = val;
}
protected _y = 0;
/**
* Get the y component of the vector
*/
public get y(): number {
return this._y;
}
/**
* Set the y component, THIS MUTATES the current vector. It is usually better to create a new vector.
* @warning **Be very careful setting components on shared vectors, mutating shared vectors can cause hard to find bugs**
*/
public set y(val: number) {
this._y = val;
}
/**
* Sets the x and y components at once, THIS MUTATES the current vector. It is usually better to create a new vector.
*
* @warning **Be very careful using this, mutating vectors can cause hard to find bugs**
*/
setTo(x: number, y: number) {
(this.x as number) = x;
(this.y as number) = y;
}
/**
* Compares this point against another and tests for equality
* @param vector The other point to compare to
* @param tolerance Amount of euclidean distance off we are willing to tolerate
*/
public equals(vector: Vector, tolerance: number = 0.001): boolean {
return Math.abs(this.x - vector.x) <= tolerance && Math.abs(this.y - vector.y) <= tolerance;
}
/**
* The distance to another vector. If no other Vector is specified, this will return the [[magnitude]].
* @param v The other vector. Leave blank to use origin vector.
*/
public distance(v?: Vector): number {
if (!v) {
v = Vector.Zero;
}
return Math.sqrt(Math.pow(this.x - v.x, 2) + Math.pow(this.y - v.y, 2));
}
public squareDistance(v?: Vector): number {
if (!v) {
v = Vector.Zero;
}
return Math.pow(this.x - v.x, 2) + Math.pow(this.y - v.y, 2);
}
/**
* The size (magnitude) of the Vector
*/
public get size(): number {
return this.distance();
}
/**
* Setting the size mutates the current vector
*
* @warning Can be used to set the size of the vector, **be very careful using this, mutating vectors can cause hard to find bugs**
*/
public set size(newLength: number) {
const v = this.normalize().scale(newLength);
this.setTo(v.x, v.y);
}
/**
* Normalizes a vector to have a magnitude of 1.
*/
public normalize(): Vector {
const d = this.distance();
if (d > 0) {
return new Vector(this.x / d, this.y / d);
} else {
return new Vector(0, 1);
}
}
/**
* Returns the average (midpoint) between the current point and the specified
*/
public average(vec: Vector): Vector {
return this.add(vec).scale(0.5);
}
/**
* Scales a vector's by a factor of size
* @param size The factor to scale the magnitude by
*/
public scale(scale: Vector): Vector;
public scale(size: number): Vector;
public scale(sizeOrScale: number | Vector): Vector {
if (sizeOrScale instanceof Vector) {
return new Vector(this.x * sizeOrScale.x, this.y * sizeOrScale.y);
} else {
return new Vector(this.x * sizeOrScale, this.y * sizeOrScale);
}
}
/**
* Adds one vector to another
* @param v The vector to add
*/
public add(v: Vector): Vector {
return new Vector(this.x + v.x, this.y + v.y);
}
/**
* Subtracts a vector from another, if you subtract vector `B.sub(A)` the resulting vector points from A -> B
* @param v The vector to subtract
*/
public sub(v: Vector): Vector {
return new Vector(this.x - v.x, this.y - v.y);
}
/**
* Adds one vector to this one modifying the original
* @param v The vector to add
* @warning Be very careful using this, mutating vectors can cause hard to find bugs
*/
public addEqual(v: Vector): Vector {
this.setTo(this.x + v.x, this.y + v.y);
return this;
}
/**
* Subtracts a vector from this one modifying the original
* @param v The vector to subtract
* @warning Be very careful using this, mutating vectors can cause hard to find bugs
*/
public subEqual(v: Vector): Vector {
this.setTo(this.x - v.x, this.y - v.y);
return this;
}
/**
* Scales this vector by a factor of size and modifies the original
* @warning Be very careful using this, mutating vectors can cause hard to find bugs
*/
public scaleEqual(size: number): Vector {
this.setTo(this.x * size, this.y * size);
return this;
}
/**
* Performs a dot product with another vector
* @param v The vector to dot
*/
public dot(v: Vector): number {
return this.x * v.x + this.y * v.y;
}
/**
* Performs a 2D cross product with scalar. 2D cross products with a scalar return a vector.
* @param v The scalar to cross
*/
public cross(v: number): Vector;
/**
* Performs a 2D cross product with another vector. 2D cross products return a scalar value not a vector.
* @param v The vector to cross
*/
public cross(v: Vector): number;
public cross(v: any): any {
if (v instanceof Vector) {
return this.x * v.y - this.y * v.x;
} else if (typeof v === 'number') {
return new Vector(v * this.y, -v * this.x);
}
}
static cross(num: number, vec: Vector): Vector {
return new Vector(-num * vec.y, num * vec.x);
}
/**
* Returns the perpendicular vector to this one
*/
public perpendicular(): Vector {
return new Vector(this.y, -this.x);
}
/**
* Returns the normal vector to this one, same as the perpendicular of length 1
*/
public normal(): Vector {
return this.perpendicular().normalize();
}
/**
* Negate the current vector
*/
public negate(): Vector {
return this.scale(-1);
}
/**
* Returns the angle of this vector.
*/
public toAngle(): number {
return Math.atan2(this.y, this.x);
}
/**
* Rotates the current vector around a point by a certain number of
* degrees in radians
*/
public rotate(angle: number, anchor?: Vector): Vector {
if (!anchor) {
anchor = new Vector(0, 0);
}
const sinAngle = Math.sin(angle);
const cosAngle = Math.cos(angle);
const x = cosAngle * (this.x - anchor.x) - sinAngle * (this.y - anchor.y) + anchor.x;
const y = sinAngle * (this.x - anchor.x) + cosAngle * (this.y - anchor.y) + anchor.y;
return new Vector(x, y);
}
/**
* Creates new vector that has the same values as the previous.
*/
public clone(): Vector {
return new Vector(this.x, this.y);
}
/**
* Returns a string representation of the vector.
*/
public toString(fixed?: number): string {
if (fixed) {
return `(${this.x.toFixed(fixed)}, ${this.y.toFixed(fixed)})`;
}
return `(${this.x}, ${this.y})`;
}
}
/**
* Shorthand for creating new Vectors - returns a new Vector instance with the
* provided X and Y components.
*
* @param x X component of the Vector
* @param y Y component of the Vector
*/
export function vec(x: number, y: number): Vector {
return new Vector(x, y);
}