This repository was archived by the owner on Nov 13, 2023. It is now read-only.
forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.ts
More file actions
302 lines (293 loc) · 7.92 KB
/
Vector.ts
File metadata and controls
302 lines (293 loc) · 7.92 KB
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
import { Vector3 } from "@minecraft/server";
/**
* @typedef {[number, number, number]} Vector3Array
*/
type Vector3Array = [number, number, number]
/**
* Contains a description of a vector.
* @implements {Vector3}
*/
export class Vector {
/**
* X component of this vector.
* @type {number}
*/
x: number;
/**
* Y component of this vector.
* @type {number}
*/
y: number;
/**
* Z component of this vector.
* @type {number}
*/
z: number;
/**
* A constant vector that represents (0, 0, -1).
* @readonly
*/
static back = new this(0, 0, -1);
/**
* A constant vector that represents (0, -1, 0).
* @readonly
*/
static down = new this(0, -1, 0);
/**
* A constant vector that represents (0, 0, 1).
* @readonly
*/
static forward = new this(0, 0, 1);
/**
* A constant vector that represents (-1, 0, 0).
* @readonly
*/
static left = new this(-1, 0, 0);
/**
* A constant vector that represents (1, 1, 1).
* @readonly
*/
static one = new this(1, 1, 1);
/**
* A constant vector that represents (1, 0, 0).
* @readonly
*/
static right = new this(1, 0, 0);
/**
* A constant vector that represents (0, 1, 0).
* @readonly
*/
static up = new this(0, 1, 0);
/**
* A constant vector that represents (0, 0, 0).
* @readonly
*/
static zero = new this(0, 0, 0);
/**
* @remarks
* Creates a new instance of an abstract vector.
* @param {number} x
* X component of the vector.
* @param {number} y
* Y component of the vector.
* @param {number} z
* Z component of the vector.
*/
constructor(x: number, y: number, z: number) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* @remarks
* Compares this vector and another vector to one another.
* @param {Vector} other
* Other vector to compare this vector to.
* @returns {boolean}
* True if the two vectors are equal.
*/
equals(other: Vector): boolean {
if (this.x === other.x && this.y === other.y && this.z === other.z)
return true;
else return false;
}
/**
* @remarks
* Retur
* @returns {number}ns the length of this vector.
*/
length(): number {
return Math.hypot(this.x, this.y, this.z);
}
/**
* @remarks
* Returns the
* @returns {number}squared length of this vector.
*/
lengthSquared(): number {
return this.x ** 2 + this.y ** 2 + this.z ** 2;
}
/**
* @remarks
* Returns this vector as a normalized vector.
* @returns {Vector}
*/
normalized(): Vector {
const magnitude = this.length();
const DirectionX = this.x / magnitude;
const DirectionY = this.y / magnitude;
const DirectionZ = this.z / magnitude;
return new Vector(DirectionX, DirectionY, DirectionZ);
}
/**
* @remarks
* Returns the addition of these vectors.
* @param {Vector3} a
* @param {Vector3} b
* @returns {Vector}
*/
static add(a: Vector3, b: Vector3): Vector {
const vector = new Vector(a.x, a.y, a.z);
vector.x += b.x;
vector.y += b.y;
vector.z += b.z;
return vector;
}
/**
* @remarks
* Returns the cross product of these two vectors.
* @param {Vector3} a
* @param {Vector3} b
* @returns {Vector}
*/
static cross(a: Vector3, b: Vector3): Vector {
return new Vector(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
);
}
/**
* @remarks
* Returns the distance between two vectors.
* @param {Vector3} a
* @param {Vector3} b
* @returns {number}
*/
static distance(a: Vector3, b: Vector3): number {
const dx = b.x - a.x;
const dy = b.y - a.y;
const dz = b.z - a.z;
const distance = Math.hypot(dx, dy, dz);
return distance;
}
/**
* @remarks
* Returns the component-wise division of these vectors.
* @param {Vector3} a
* @param {Vector3 | number} b
* @returns {Vector}
*/
static divide(a: Vector3, b: Vector3 | number): Vector {
const vector = new Vector(a.x, a.y, a.z);
if (typeof b === "number") {
vector.x /= b;
vector.y /= b;
vector.z /= b;
} else {
vector.x /= b.x;
vector.y /= b.y;
vector.z /= b.z;
}
return vector;
}
/**
* @remarks
* Returns the linear interpolation between a and b using t as
* the control.
* @param {Vector3} a
* @param {Vector3} b
* @param {number} t
* @returns {Vector}
*/
static lerp(a: Vector3, b: Vector3, t: number): Vector {
const dest = new Vector(a.x, a.y, a.z);
dest.x += (b.x - a.x) * t;
dest.y += (b.y - a.y) * t;
dest.z += (b.z - a.z) * t;
return dest;
}
/**
* @remarks
* Returns a vector that is made from the largest components of
* two vectors.
* @param {Vector3} a
* @param {Vector3} b
* @returns {Vector}
*/
static max(a: Vector3, b: Vector3): Vector {
const vectors = [a, b];
const arr = vectors.map(({ x, y, z }) => new Vector(x, y, z).length());
const max = Math.max(...arr);
const index = arr.indexOf(max);
const vector3 = vectors[index];
return new Vector(vector3.x, vector3.y, vector3.z);
}
/**
* @remarks
* Returns a vector that is made from the smallest components
* of two vectors.
* @param {Vector3} a
* @param {Vector3} b
* @returns {Vector}
*/
static min(a: Vector3, b: Vector3): Vector {
const vectors = [a, b];
const arr = vectors.map(({ x, y, z }) => new Vector(x, y, z).length());
const min = Math.min(...arr);
const index = arr.indexOf(min);
const vector3 = vectors[index];
return new Vector(vector3.x, vector3.y, vector3.z);
}
/**
* @remarks
* Returns the component-wise product of these vectors.
* @param {Vector3} a
* @param {Vector3 | number} b
* @returns {Vector}
*/
static multiply(a: Vector3, b: Vector3 | number): Vector {
const vector = new Vector(a.x, a.y, a.z);
if (typeof b === "number") {
vector.x *= b;
vector.y *= b;
vector.z *= b;
} else {
vector.x *= b.x;
vector.y *= b.y;
vector.z *= b.z;
}
return vector;
}
/**
* @remarks
* Returns the spherical linear interpolation between a and b
* using s as the control.
* @param {Vector3} a
* @param {Vector3} b
* @param {number} s
* @returns {Vector}
*/
static slerp(a: Vector3, b: Vector3, s: number): Vector {
/**
* @param {Vector3Array} a
* @param {Vector3Array} b
* @returns {number}
*/
function MathDot(a: Vector3Array, b: Vector3Array): number {
return a.map((x, i) => a[i] * b[i]).reduce((m, n) => m + n);
}
const θ = Math.acos(MathDot([a.x, a.y, a.z], [b.x, b.y, b.z]));
const factor1 = Math.sin(θ * (1 - s)) / Math.sin(θ);
const factor2 = Math.sin(θ * s) / Math.sin(θ);
return new Vector(
a.x * factor1 + b.x * factor2,
a.y * factor1 + b.y * factor2,
a.z * factor1 + b.z * factor2
);
}
/**
* @remarks
* Returns the subtraction of these vectors.
* @param {Vector3} a
* @param {Vector3} b
* @returns {Vector}
*/
static subtract(a: Vector3, b: Vector3): Vector {
const vector = new Vector(a.x, a.y, a.z);
vector.x -= b.x;
vector.y -= b.y;
vector.z -= b.z;
return vector;
}
}