-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.js
More file actions
439 lines (438 loc) · 12.9 KB
/
vector.js
File metadata and controls
439 lines (438 loc) · 12.9 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/** Version: 1.6
* #
* Changes:
* - static x, static y
* - static random, static randomX, static randomY
* - setLength, withLength
*
* Created by NemGamingAkos
*
* [ngakos.lol/libloader](https://www.ngakos.lol/libloader)
* */
class Vector {
constructor(x=0, y=x) {
this.x = x;
this.y = y;
}
/** A vector facing upwards */
static get up() {
return new Vector(0, -1);
}
/** A vector facing downwards */
static get down() {
return new Vector(0, 1);
}
/** A vector facing left */
static get left() {
return new Vector(-1, 0);
}
/** A vector facing right */
static get right() {
return new Vector(1, 0);
}
/** 1 degree in radian */
static get fok1rad() {
return 0.017453292519943295;
}
/** 15 degrees in radian */
static get fok15rad() {
return 0.2617993877991494;
}
/** 30 degrees in radian */
static get fok30rad() {
return 0.5235987755982988;
}
/** 45 degrees in radian */
static get fok45rad() {
return 0.7853981633974483;
}
/** 90 degrees in radian */
static get fok90rad() {
return 1.5707963267948966;
}
/** 135 degrees in radian */
static get fok135rad() {
return 2.356194490192345;
}
/** 180 degrees in radian */
static get fok180rad() {
return Math.PI;
}
/** Devide by this to get the degrees in radians */
static get fokToRadDev() {
return 57.29577951308232; // 1 radian = 57.29577951308232 degrees (180 / PI)
}
/** Multiply by this to get the degrees in radians */
static get fokToRad() {
return 0.017453292519943295; // 1 degrees = 0.017453292519943295 radians (PI / 180)
}
/** Devide by this to get the radians in degrees */
static get radToFokDev() {
return Vector.fokToRad;
}
/** Multiply by this to get the radians in degrees */
static get radToFok() {
return Vector.fokToRadDev
}
/** Hosszúság */
get length() {
return Math.hypot(this.x, this.y)
}
/** Írány megmarad, hosszúság -> 1 */
get normalized() {
let l = this.length;
return l === 0 ? Vector.null : Vector.as(this.x / l, this.y / l);
}
/** Returns it's values rounded */
get rounded() {
return new Vector(Math.round(this.x), Math.round(this.y));
}
/** Math.floor */
get floor() {
return new Vector(Math.floor(this.x), Math.floor(this.y));
}
/** Math.ceil */
get ceil() {
return new Vector(Math.ceil(this.x), Math.ceil(this.y));
}
/** this.copy() */
get self() {
return this.copy()
}
/** Vektor -> Radián */
get radian() {
return Math.atan2(this.y, this.x);
}
/** Vektor -> Fok */
get fok() {
return this.radian * Vector.radToFok;
}
/** Vektor relatívan lefelé */
get down() {
return Vector.as(this.x, this.y + 1);
}
/** Vektor relatívan felfelé */
get up() {
return Vector.as(this.x, this.y - 1);
}
/** Vektor relatívan balra */
get left() {
return Vector.as(this.x - 1, this.y);
}
/** Vektor relatívan jobbra */
get right() {
return Vector.as(this.x + 1, this.y);
}
/** Ugyan az-e, mint a Vector.null */
get isNull() {
return this.isSameAs(Vector.null);
}
/** Irány előre */
get dforward() {
return this.radian;
}
/** Irány balra */
get dleft() {
return this.radian - Vector.fok90rad;
}
/** new Vector(0, 0) */
static get null() {
return new Vector(0, 0)
}
/** Basically .new() */
static as(x=0, y=x) {
return new Vector(x, y);
}
/**
* Négyzet grid
* @deprecated the base class can now do the same
* */
static grid(n=0) {
return new Vector(n, n);
}
/** Returns a new Vector with x being `x` and y being 0 */
static x(x = 0) {
return new Vector(x, 0);
}
/** Returns a new Vector with x being 0 and y being `y` */
static y(y = 0) {
return new Vector(0, y);
}
/** X and Y are random */
static random(min=0, max=1) {
return new Vector(Math.random() * (max - min) + min, Math.random() * (max - min) + min);
}
/** X is random, Y is 1*/
static randomX(min=0, max=1) {
return new Vector(Math.random() * (max - min) + min, 1);
}
/** X is 1, Y is random */
static randomY(min=0, max=1) {
return new Vector(1, Math.random() * (max - min) + min);
}
/** Returns the given degrees in radians
* @param {number} [fok=0] degrees
*/
static fokToRadian(fok=0) {
return fok * Vector.fokToRad;
}
/**
* Returns the given radians in degrees
* @param {Number} radian radians
*/
static radianToFok(radian=0) {
return radian * Vector.radToFok;
}
/** Alias for parseJSON */
static fromJSON(json="") { return Vector.parseJSON(json); }
/** JSON -> Vektor */
static parseJSON(json="") {
if (typeof json == "string") json = JSON.parse(json);
return Vector.as(json["x"], json["y"]);
}
/** Alias for parseFok */
static fromFok(fok=0, ztz=false) { return Vector.parseFok(fok, ztz); }
/** Fok -> Vektor
* @param {boolean} [ztz=false] fok == 0 => Vector.null
*/
static parseFok(fok=0, ztz=false) {
if (ztz && fok == 0) return Vector.null;
let rad = (fok % 360) * (Math.PI / 180);
return new Vector(Math.cos(rad), Math.sin(rad))
}
/** Alias for parseRad */
static fromRad(rad=0, ztz=false) { return Vector.parseRad(rad, ztz); }
/** Radián -> Vektor
* @param {boolean} [ztz=false] rad == 0 => Vector.null
*/
static parseRad(rad=0, ztz=false) {
if (ztz && rad == 0) return Vector.null;
return new Vector(Math.cos(rad), Math.sin(rad))
}
/** JSON -> Vektor */
parseJSON(json) {
return this.setv(Vector.parseJSON(json));
}
/** Fok -> Vektor */
parseFok(fok=0) {
return this.setv(Vector.parseFok(fok))
}
/** Radián -> Vektor */
parseRad(rad=0) {
return this.setv(Vector.parseRad(rad))
}
/** Értékek megfordítása */
flip(x=true, y=true) {
if (x) this.x = -this.x;
if (y) this.y = -this.y;
return this;
}
/** X érték megfordítása */
flipX() {
this.x = -this.x;
return this;
}
/** Y érték megfordítása */
flipY() {
this.y = -this.y;
return this;
}
/** Normalizálja a vektort, irány megmarad, hosszúság -> 1 */
normalize() {
return this.setv(this.normalized);
}
/** Sets the length of the vector to `n` */
setLength(n=1) {
return this.normalize().scale(n);
}
/** Returns the vector as if it's length was `n` */
withLength(n=1) {
return this.self.setLength(n);
}
/** Méretezés */
scale(n=1) {
this.x *= n;
this.y *= n;
return this;
}
/** Returns the vector as if it was scaled to `n` */
scaled(n=1) {
return this.self.scale(n);
}
/** Hasonlóság */
similar(v2, threshold=0) {
return this.distanceTo(v2) <= threshold;
}
/** Távolság */
distanceTo(v2=Vector.null) {
const dx = this.x - v2.x;
const dy = this.y - v2.y;
return Math.hypot(dx, dy);
}
/** Irány, alapból radián */
directionTo(v2=Vector.null, rad=true) {
let val = Math.atan2(v2.y - this.y, v2.x - this.x)
return rad ? val : val * Vector.radToFok;
}
/** Vektor irány - fok */
directionToLeft(fok=0) {
return this.fok - fok;
}
/** Vektor irány + fok */
directionToRight(fok=0) {
return this.fok + fok;
}
/** Vektor == Vektor */
isSameAs(v2=this.copy()) {
return this.x == v2.x && this.y == v2.y;
}
/** Vissza ad egy új Vektor-t ennek az értékeivel */
copy() {
return new Vector(this.x, this.y)
}
/** Move Vector by values */
move(x=0, y=0) {
return this.add(Vector.as(x, y));
}
/** Move Vector by Vector */
movev(v2) {
return this.add(v2);
}
/** Rotate vector */
rotate(val, rad=true) {
if (!rad) val = (val % 360) * (Math.PI / 180);
let cos = Math.cos(val), sin = Math.sin(val);
return this.set(this.x * cos - this.y * sin, this.x * sin + this.y * cos);
}
/** Értékek beállítása */
set(x=0, y=0) {
this.x = x;
this.y = y;
return this;
}
/** Vektor = v2 */
setv(v2=Vector.null) {
this.x = v2.x;
this.y = v2.y;
return this;
}
/** Vektor irányba való elmozdulás */
moveInDirection(v2) {
this.moveTowards(v2.added(this.self));
}
/** Vektor irányába mozgás */
moveTowards(v2, speed=5, enableTeleport=true) {
if (enableTeleport && this.similar(v2, speed))
return this.setv(v2);
return this.setv(this.added(Vector.parseRad(this.directionTo(v2)).scale(speed)));
}
/** Rugós követés távolsággal */
lockWithDistance(v2, speed=10, distance=5, min=NaN, max=NaN) {
let spd = speed * Math.max(this.distanceTo(v2) * distance / 100, 0.5);
if (min) spd = Math.max(spd, min);
if (max) spd = Math.min(spd, max);
return this.moveTowards(v2, spd);
}
/** Tartja a maximum távolságot */
followWithDistance(v2, distance=5) {
let dist = this.distanceTo(v2);
if (dist <= distance) return this;
return this.moveTowards(v2, dist - distance, false);
}
/** Összeadás */
add(v2) {
if (typeof v2 === "string") v2 = Number.parseFloat(v2);
if (typeof v2 === "number") v2 = Vector.grid(v2);
return this.setv(this.added(v2));
}
/** Összeadás értéke */
added(v2) {
if (typeof v2 === "string") v2 = Number.parseFloat(v2);
if (typeof v2 === "number") v2 = Vector.grid(v2);
return new Vector(this.x + v2.x, this.y + v2.y);
}
/** Kivonás */
sub(v2) {
if (typeof v2 === "string") v2 = Number.parseFloat(v2);
if (typeof v2 === "number") v2 = Vector.grid(v2);
return this.setv(this.subbed(v2))
}
/** Kivonás értéke */
subbed(v2) {
if (typeof v2 === "string") v2 = Number.parseFloat(v2);
if (typeof v2 === "number") v2 = Vector.grid(v2);
return new Vector(this.x - v2.x, this.y - v2.y);
}
/** Szorzás */
mult(v2) {
if (typeof v2 === "string") v2 = Number.parseFloat(v2);
if (typeof v2 === "number") v2 = Vector.grid(v2);
return this.setv(this.multed(v2))
}
/** Szorás értéke */
multed(v2) {
if (typeof v2 === "string") v2 = Number.parseFloat(v2);
if (typeof v2 === "number") v2 = Vector.grid(v2);
return new Vector(this.x * v2.x, this.y * v2.y);
}
/** Osztás */
dev(v2) {
if (typeof v2 === "string") v2 = Number.parseFloat(v2);
if (typeof v2 === "number") v2 = Vector.grid(v2);
return this.setv(this.deved(v2))
}
/** Osztás értéke */
deved(v2) {
if (typeof v2 === "string") v2 = Number.parseFloat(v2);
if (typeof v2 === "number") v2 = Vector.grid(v2);
return new Vector(this.x / v2.x, this.y / v2.y);
}
/** Uses the modulo operator (%) */
modulo(v2) {
if (typeof v2 === "string") v2 = Number.parseFloat(v2);
if (typeof v2 === "number") v2 = Vector.grid(v2);
return this.setv(this.moduloed(v2));
}
/** The value as if .modulo was called */
moduloed(v2) {
if (typeof v2 === "string") v2 = Number.parseFloat(v2);
if (typeof v2 === "number") v2 = Vector.grid(v2);
return new Vector(this.x % v2.x, this.y % v2.y);
}
/** Checks if both of the values of the number are devisible by `n` */
isDivisibleBy(n=2) {
return this.x % n == 0 && this.y % n == 0;
}
/** Returns how manyeth cell is this in */
placeInGrid(n=16) {
return this.deved(n).floor;
}
/** Rounds itself to the nearest values which are divisible by `n` */
roundToDivision(n=16) {
let diffX = this.x % n;
if (diffX < n / 2) this.x -= diffX
else this.x -= diffX - n;
let diffY = this.y % n;
if (diffY < n / 2) this.y -= diffY
else this.y -= diffY - n;
return this;
}
/** Vektor -> String */
toString(split=";") {
return `${this.x}${split}${this.y}`
}
/** Visualize vector */
visualizev(pos=Vector.null) {
ctx.beginPath();
ctx.arc(pos.x, pos.y, this.length, 0, Math.PI * 2);
ctx.moveTo(pos.x, pos.y)
let np = this.added(pos)
ctx.lineTo(np.x, np.y);
ctx.closePath();
ctx.stroke();
}
/** Visualize vector with x, y */
visualize(x, y) {
this.visualizev(Vector.as(x, y));
}
}