-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
541 lines (443 loc) · 11.7 KB
/
index.js
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
'use strict';
const { freeze } = Object
const { iterator } = Symbol
const { sqrt, atan2, PI, round, cos, sin, sign, abs } = Math
const X = Symbol('AxisX')
const Y = Symbol('AxisY')
const Z = Symbol('AxisZ')
const EXEC = new Set(['add', 'sub', 'mul', 'div', 'pow', 'set'])
const { EPSILON } = Number
const isNum = (n) => (typeof n === "number" && isFinite(n))
const toRadian = (degree) => (degree * PI / 180)
const is2DVectorLike = (v) => (!!v && isNum(v.x) && isNum(v.y))
const is3DVectorLike = (v) => (is2DVectorLike(v) && isNum(v.z))
function notImp(name) {
throw new Error(`Method ${name} is not implemented.`)
}
function isIterable(value) {
return value
&& Array.isArray(value)
|| typeof value[iterator] === "function"
}
function valid2DVector(source) {
if (is2DVectorLike(source)) return true
throw new TypeError('Required a like 2D or 3D Vector style object.')
}
function valid3DVector(source) {
if (is3DVectorLike(source)) return true
throw new TypeError('Required a like 3D Vector style object.')
}
//#region IVector and AVector
export class IVector {
constructor(x = 0, y = x, z = x) {
this[X] = x || 0
this[Y] = y || 0
this[Z] = z || 0
}
get x() { return this[X] }
get y() { return this[Y] }
get z() { return this[Z] }
/** @return {IVector | AVector | Vector | Vector2 | Vector3} */
clone() { return new this.constructor(...this) }
copy() { return this.clone() }
print() {
console.table(this)
return this
}
/** @return {number[]} */
toArray() { return [...this] }
toJSON() { return { x: this.x, y: this.y, z: this.z } }
toString() {
return `[${this.constructor.name} ${this.x},${this.y},${this.z}]`
}
*[iterator]() {
yield this.x
yield this.y
yield this.z
}
/** @return {source is IVector} */
static isIVector(source) { return source instanceof IVector }
static one() { return new this(1) }
static zero() { return new this(0) }
static toString() {
return 'class ' + this.name + ' { [native code] }'
}
static from(source) {
if (isIterable(source)) {
return new this(...source)
}
if (is2DVectorLike(source)) {
return new this(source.x, source.y, source.z || 0)
}
return null
}
}
export class AVector extends IVector {
//#region Setter Getter
get x() { return this[X] }
get y() { return this[Y] }
get z() { return this[Z] }
set x(x) { notImp('set x()') }
set y(y) { notImp('set y()') }
set z(z) { notImp('set z()') }
//#endregion
//#region Public Methods
add(x, y, z) { notImp('add()') }
sub(x, y, z) { notImp('sub()') }
div(x, y, z) { notImp('div()') }
mul(x, y, z) { notImp('mul()') }
pow(x, y, z) { notImp('pow()') }
set(x, y, z) { notImp('set()') }
addVec(source) { notImp('addVec()') }
setVec(source) { notImp('setVec()') }
/**
* @param {'add'|'sub'|'mul'|'div'|'pow'|'set'} exec
* @param {IVector | { x: number, y: number, z: ?number }} source
*/
calc(exec, source) {
valid2DVector(source)
if (EXEC.has(exec = exec.toLocaleLowerCase().trim())) {
this[exec](source.x, source.y, source.z || 0)
}
return this
}
neg() { notImp('negative() or neg()') }
norm() { notImp('normalize() or norm()') }
clear() { notImp('clear()') }
negative() { return this.neg() }
normalize() { return this.norm() }
eps() { notImp("eps") }
dist(x = 0, y = x, z = x) {
return sqrt(
(this.x - x) ** 2 +
(this.y - y) ** 2 +
(this.z - z) ** 2
)
}
get2Dgedree() {
return (360 * round(180 * this.get2DRadian() / PI)) % 360
}
/** @param {IVector} source */
distance(source) {
valid2DVector(source)
return this.dist(source.x, source.y, source.z || 0)
}
get2DRadian() { return atan2(this.y, this.x) }
set2Ddegree(degree) { notImp('setAngle()') }
rotate2D(degree, center) { notImp('rotate()') }
setLen(len) { notImp('setLen()') }
len() { return AVector.len3D(this) }
toVector() { return new Vector(...this) }
isOK() { return is3DVectorLike(this) }
fix() {
if (!this.isOK()) {
if (!isNum(this.x)) this.x = 0
if (!isNum(this.y)) this.y = 0
if (!isNum(this.z)) this.z = 0
}
return this
}
/** @param {IVector} source */
same(source) {
valid2DVector(source)
return this.x === source.x
&& this.y === source.y
&& this.z === source.z
}
//#endregion
//#region Static Methods
/**
* @param {IVector} vecFrom
* @param {IVector} vecTo
*/
static distance2D(vecFrom, vecTo) {
valid2DVector(vecFrom)
valid2DVector(vecTo)
return sqrt(
(vecFrom.x - vecTo.x) ** 2
+ (vecFrom.y - vecTo.y) ** 2
)
}
/**
* @param {IVector} vecFrom
* @param {IVector} vecTo
*/
static distance3D(vecFrom, vecTo) {
valid3DVector(vecFrom)
valid3DVector(vecTo)
return sqrt(
(vecFrom.x - vecTo.x) ** 2
+ (vecFrom.y - vecTo.y) ** 2
+ (vecFrom.z - vecTo.z) ** 2
)
}
/** @return {source is AVector} */
static isAVector(source) { return source instanceof AVector }
/**
* @param {IVector} vecFrom
* @param {IVector} vecTo
*/
static direction2D(vecFrom, vecTo) {
valid2DVector(vecFrom)
valid2DVector(vecTo)
const d = Vector.distance2D(vecFrom, vecTo)
const x = (vecTo.x - vecFrom.x) / d
const y = (vecTo.y - vecFrom.y) / d
const z = 0
return new this(x, y, z)
}
/**
* @param {IVector} vecFrom
* @param {IVector} vecTo
*/
static direction3D(vecFrom, vecTo) {
valid3DVector(vecFrom)
valid3DVector(vecTo)
const d = Vector.distance3D(vecFrom, vecTo)
const x = (vecTo.x - vecFrom.x) / d
const y = (vecTo.y - vecFrom.y) / d
const z = (vecTo.z - vecFrom.z) / d
return new this(x, y, z)
}
/**
* @param {IVector} vec1
* @param {IVector} vec2
*/
static assign(vec1, vec2) {
valid2DVector(vec1)
valid2DVector(vec2)
const x = (vec1.x + vec2.x)
const y = (vec1.y + vec2.y)
const z = (vec1.z + vec2.z) || 0
return new this(x, y, z)
}
/**
* @param {IVector} source
* @param {IVector} [out]
*/
static normalize2D(source, out) {
valid2DVector(source)
const len = AVector.len2D(source) || 1
const x = source.x / len
const y = source.y / len
const z = 0
return AVector.isAVector(out) ? out.set(x, y, z) : new this(x, y, z)
}
/**
* @param {IVector} source
* @param {IVector} [out]
*/
static normalize3D(source, out) {
valid3DVector(source)
const len = AVector.len2D(source) || 1
const x = source.x / len
const y = source.y / len
const z = source.z / len
return AVector.isAVector(out) ? out.set(x, y, z) : new this(x, y, z)
}
static len2D(source) {
return sqrt(source.x ** 2 + source.y ** 2)
}
static len3D(source) {
return sqrt(source.x ** 2 + source.y ** 2 + source.z ** 2)
}
static dot2D(vec1, vec2) {
valid2DVector(vec1)
valid2DVector(vec2)
return vec1.x * vec2.x + vec1.y * vec2.y
}
static dot3D(vec1, vec2) {
valid3DVector(vec1)
valid3DVector(vec2)
return vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z
}
static cross2D(vec1, vec2) {
valid2DVector(vec1)
valid2DVector(vec2)
return (vec1.x * vec2.y) - (vec1.y * vec2.x)
}
static cross2D_3(vec1, vec2, vec3) {
valid2DVector(vec1)
valid2DVector(vec2)
valid2DVector(vec3)
return (vec2.x - vec1.x)
* (vec3.y - vec1.y)
- (vec2.y - vec1.y)
* (vec3.x - vec1.x)
}
static cross3DVec(vec1, vec2) {
valid3DVector(vec1)
valid3DVector(vec2)
const x = vec2.z * vec1.y - vec1.z * vec2.y
const y = vec2.x * vec1.z - vec1.x * vec2.z
const z = vec2.y * vec1.x - vec1.y * vec2.x
return new this(x, y, z)
}
// #endregion
}
//#endregion
//#region Vector
export class Vector extends AVector {
//#region Setter Getter
get x() { return this[X] }
get y() { return this[Y] }
get z() { return this[Z] }
set x(x) { this[X] = x || 0 }
set y(y) { this[Y] = y || 0 }
set z(z) { this[Z] = z || 0 }
//#endregion
//#region Public Methods
//#region Math
add(x = 0, y = x, z = x) {
this.x += x
this.y += y
this.z += z
return this
}
sub(x = 0, y = x, z = x) {
this.x -= x
this.y -= y
this.z -= z
return this
}
div(x = 1, y = x, z = x) {
this.x /= x
this.y /= y
this.z /= z
return this
}
mul(x = 1, y = x, z = x) {
this.x *= x
this.y *= y
this.z *= z
return this
}
pow(x = 1, y = x, z = x) {
this.x **= x
this.y **= y
this.z **= z
return this
}
set(x = 0, y = x, z = x) {
this.x = x
this.y = y
this.z = z
return this
}
//#endregion
//#region Math with Vector
/** @param {IVector} source */
setVec(source) {
this.x = source.x
this.y = source.y
this.z = source.z
return this
}
/** @param {IVector} source */
addVec(source) {
this.x += source.x
this.y += source.y
this.z += source.z
return this
}
//#endregion
//#region Angle
set2Ddegree(degree) {
if (degree === 0 || degree === 360) return this
const len = AVector.len2D(this)
const radian = toRadian(degree)
this.x = cos(radian) * len
this.y = sin(radian) * len
return this
}
/**
* @param {number} degree
* @param {IVector} center
*/
rotate2D(degree, center) {
if (degree === 0 || degree === 360) return this
if (is2DVectorLike(center)) this.calc("sub", center)
const radian = toRadian(degree)
const sin_ = sin(radian)
const cos_ = cos(radian)
const x = this.x * cos_ - this.y * sin_
const y = this.x * sin_ + this.y * cos_
this.x = x
this.y = y
if (is2DVectorLike(center)) this.addVec(center)
return this
}
//#endregion
/** @param {number} len */
setLen(len) { return this.norm().mul(len || 0) }
norm() { return this.div(this.len() || 1) }
len() { return AVector.len3D(this) }
neg() { return this.mul(-1) }
clear() { return this.set(0) }
abs() { return this.set(abs(this.x), abs(this.y), abs(this.z)) }
//! fix it
eps() {
let x = abs(this.x)
let y = abs(this.y)
let z = abs(this.z)
if (EPSILON > (x % 1)) x = x - (x % 1)
if (EPSILON > (y % 1)) y = y - (y % 1)
if (EPSILON > (z % 1)) z = z - (z % 1)
this.x = x * sign(this.x)
this.y = y * sign(this.y)
this.z = z * sign(this.z)
}
//#endregion
/** @return {source is Vector} */
static isVector(source) { return source instanceof Vector }
}
export class Vector2 extends Vector {
constructor(x, y) { super(x, y, 0) }
get x() { return this[X] }
get y() { return this[Y] }
get z() { return 0 }
set x(x) { this[X] = x || 0 }
set y(y) { this[Y] = y || 0 }
set z(z) { }
len() { return AVector.len2D(this) }
}
export class Vector3 extends Vector {
get x() { return this[X] }
get y() { return this[Y] }
get z() { return this[Z] }
set x(x) { this[X] = x || 0 }
set y(y) { this[Y] = y || 0 }
set z(z) { this[Z] = z || 0 }
len() { return AVector.len3D(this) }
}
//#endregion
//#region Constants
const ZERO = new AVector(0, 0, 0)
export const VECTOR_CONSTANTS = freeze({
ZERO,
ONE: new AVector(1, 1, 1),
RIGHT: new AVector(1, 0, 0),
BACK: new AVector(0, 1, 0),
BOTTOM: new AVector(0, 0, 1),
LEFT: new AVector(-1, 0, 0),
FORWARD: new AVector(0, -1, 0),
TOP: new AVector(0, 0, -1)
})
//#endregion
//#region Export functions
export function vec(x = 0, y = x, z = x) { return new Vector(x, y, z) }
vec.one = Vector.one
vec.zero = Vector.zero
vec.from = Vector.from
vec.self = Vector
export function v2(x = 0, y = x) { return new Vector2(x, y) }
v2.one = Vector2.one
v2.zero = Vector2.zero
v2.from = Vector2.from
v2.self = Vector2
export function v3(x = 0, y = x, z = x) { return new Vector3(x, y, z) }
v3.one = Vector3.one
v3.zero = Vector3.zero
v3.from = Vector3.from
v3.self = Vector3
//#endregion