-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.js
617 lines (528 loc) · 16.6 KB
/
Vector.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
import {WebSystemObject} from './WebSystemObject.js';
// basado en una vieja idea con algunas interfaces de https://gist.github.com/jjgrainger
export class Vector extends WebSystemObject {
constructor(...components) {
super();
this.dimmensions = [];
this.kind = 'planar';
if (arguments.length > 0) {
if (arguments[0] instanceof Vector) {
this.dimmensions = [].concat(arguments[0].dimmensions);
} else if (arguments[0] instanceof Array) {
this.dimmensions = arguments[0]; // done
} else if (arguments[0] instanceof String || typeof arguments[0] === 'string') {
this.asString = components; // done
} else {
this.dimmensions = arguments; // done
}
}
}
// Número de dimensiones u orden del vector
get order() {
return this.dimmensions.length;
}
set order(order) {
this.dimmensions.length = order;
}
// The obvious
get coords() {
return this.dimmensions;
}
set coords(d) {
this.dimmensions = d;
}
// alias for dommensions components
get x() {
return this.dimmensions[0];
}
set x(x) {
this.dimmensions[0] = x;
}
get y() {
return this.dimmensions[1];
}
set y(y) {
this.dimmensions[1] = y;
}
get z() {
return this.dimmensions[2];
}
set z(z) {
this.dimmensions[2] = z;
}
get t() {
return this.dimmensions[3];
}
set t(t) {
this.dimmensions[3] = t;
}
// return the angle of the vector in radians
get direction() {
if (this.order >= 2) {
return Math.atan2(this.y, this.x);
}
return 0;
};
// set the direction of the vector in radians
set direction(angle) {
if (this.order >= 1) {
if (this.order >= 2) {
this.x = Math.cos(angle) * this.magnitude;
this.y = Math.sin(angle) * this.magnitude;
return Math.atan2(this.y, this.x);
}
return Math.sign(this.x) * 2 * Math.PI;
}
};
// get the elevation of the second point of vector, respect the first
get elevation() {
let shadowSquare = this.x * this.x + this.y + this.y;
return Math.sqrt(Math.pow(this.norm, 2) - shadowSquare);
};
// set the elevation of the ... keeping the direction.
set elevation(elevation) {
this.norm = Math.sqrt(this.norm * this.norm - this.elevation * this.elevation + elevation * elevation);
};
// get the elevation of the second point of vector, respect the first
get colattitude() {
return Math.PI / 2 - this.elevation;
};
// set the elevation of the ... keeping the direction.
set colattitude(colattitude) {
this.elevation = colattitude - Math.PI / 2;
};
// Module, length, or magnitude of vector (in spanglish: number of dimmensions).
get norm() {
let catetsSquares = 0;
this.dimmensions.forEach(component => catetsSquares += component * component);
return Math.sqrt(catetsSquares);
};
// set the magnitude of the vector
set norm(module) {
let before = this.norm;
this.dimmensions = this.dimmensions.map(component => component * module / before);
};
// common alias
get magnitude() {
return this.norm;
}
set magnitude(module) {
this.norm = module;
};
get module() {
return this.norm;
}
set module(module) {
this.norm = module;
};
// en of alias
get asString() {
return '(' + [...this.dimmensions].join(', ') + ')';
};
set asString(v) {
let tmp = String(v).trim();
if (tmp.length < 2 || (tmp.length > 2 && tmp[0] !== '(' && tmp[tmp.length - 1] !== ')')) {
throw new Error(`"${v}", do not looks alike a vector, don't seems to me.`);
} else {
tmp = tmp.replaceAll(' ', '');
let elementos = tmp.substring(1, tmp.length - 1).split(',');
this.dimmensions = [].concat(elementos.map(x => Number(x)));
}
}
get asObject() {
let result = {};
if (this.order >= 1) {
if (this.kind === 'geographical') {
result['lon'] = this.x;
} else {
result['x'] = this.x;
}
}
if (this.order >= 2) {
if (this.kind === 'geographical') {
result['lat'] = this.y;
} else {
result['y'] = this.y;
}
}
if (this.order >= 3) {
if (this.kind === 'geographical') {
result['alt'] = this.z;
} else {
result['z'] = this.z;
}
}
if (this.order >= 4) {
result['t'] = this.t;
}
if (this.order > 4) {
for (let index = 0; index < this.order; index++) {
result['d' + (index + 1).toFixed(0)] = this.dimmensions[index];
}
}
return result;
};
set asObject(o) {
let result = {};
for (const key in o) {
if (key === 'lat' || key === 'latitude' || key === 'lon' || key === 'longitude' || key === 'alt' || key === 'altitude') {
this.kind = 'geographical';
}
if (o.hasOwnProperty(key)) {
this.setComponent(key, o[key]);
}
}
};
isD(n) {
return this.order === n;
}
is2D() {
return this.order === 2;
}
is3D() {
return this.order === 3;
}
clone() {
return new Vector(...this.dimmensions);
}
scale(s) {
this.dimmensions.forEach((c, i, a) => a[i] = c * s);
return this;
};
get length() {
return this.norm;
};
set length(v) {
this.norm = v;
};
lengthSq() {
return Math.sqrt(this.norm);
};
// get components by argument
getComponent(name) {
switch (name) {
case 'lon':
case 'longitude':
case 'x':
return this.x;
case 'lat':
case 'lattitude':
case 'y':
return this.y;
case 'z':
return this.z;
case 't':
return this.t;
default: {
if (name.length < 2 || (name.length >= 2 && name[0] !== 'd')) {
let result = this.dimmensions[Number(name.substring(1))];
return result ? result : 0;
}
}
}
}
// for monday: need to rotate a point, (and whena i said a point, i meam: not a vector) over a multidimensional axis
// need to know indexed properties on javascript
setComponent(name, value) {
switch (name) {
case 'lon': {
this.x = value;
this.kind = 'geographical';
return;
}
case 'longitude': {
this.x = value;
this.kind = 'geographical';
return;
}
case 'lat': {
this.y = value;
this.kind = 'geographical';
return;
}
case 'latitude': {
this.y = value;
this.kind = 'geographical';
return;
}
case 'x': {
this.x = value;
return;
}
case 'y': {
this.y = value;
return;
}
case 'alt': {
this.z = value;
this.kind = 'geographical';
return;
}
case 'z': {
this.z = value;
return;
}
case 't': {
this.t = value;
return;
}
default: {
if (name.length < 2 || (name.length >= 2 && name[0] !== 'd')) {
this.dimmensions[Number(name.substring(1))] = value;
}
}
}
}
// return unitary vector, same direction but module equals 1.
unitary() {
let result = this.clone();
if (result.module === 0) {
console.log('Warning: the vector is null, probably cannot identify a direction.');
}
return this.division(this.module);
}
normalize() {
let m = this.norm;
this.dimmensions = this.dimmensions.map((dimension) => dimension / m);
return this;
}
// As point abstraction: return the distance % me and 2nd component of a given one.
distance(anotherVector, vectorialSpace = null) {
let metric;
if (!vectorialSpace) { // Métrica (o norma) euclidiana si no se especifica alguna otra.
metric = (p1, p2) => {
let result = 0;
for (let i = 0; i < Math.min(p1.order, p2.order); i++) {
result += Math.pow(p1.dimmensions[i] - p2.dimmensions[i], 2);
}
return Math.sqrt(result);
};
} else {
metric = vectorialSpace.metric;
}
return metric(this, anotherVector);
}
// this is the angle with another vector .
angle(anotherVector) {
// Ley de los cosenos c¹ = a² + b² - 2ab cos α
let a_normal = this.unitary();
let b_normal = anotherVector.unitary();
let c_normal = a_normal.distance(b_normal);
// than a = 1, b = 1, c is above module
return Math.acos((2 - Math.pow(c_normal.module * c_normal.module, 2)) / 2);
// Otra forma de encontrar el ángulo entre dos vectores mediante el producto escalar,
// igual por definición a: |a|·|b| cos α = ∑ (i = 1 to n) ai · bi, de allí que
// cos α = [ ∑ (i = 1 to n) ai · bi ] / [ |a|·|b| ]
// por tanto α = Acos([ ∑ (i = 1 to n) ai · bi ] / [ |a|·|b| ])
// as is showed in the next item
}
angleTo2D(v) {
var dx = v.x - this.x,
dy = v.y - this.y;
return Math.atan2(dy, dx);
}
lerp2D(v, t) {
this.x += (v.x - this.x) * t;
this.y += (v.y - this.y) * t;
return this;
}
// Producto escalar (igual por definición a: A × Β = | A | | B | Cos θ).
dotProduct({components}) {
return components.reduce((acc, component, index) => acc + component * this.component[index], 0);
}
/* todo Producto cruzado, A × Β = | A | | B | Sin θ
El resultado es otra cantidad vectorial. El vector resultante es perpendicular a ambos vectores.
Su dirección se puede determinar utilizando la regla de la mano derecha.
Se deben considerar las siguientes reglas al calcular el producto cruzado:
i × j = k
J × k = i
k × i = j
Donde i, j y k son los vectores unitarios en las direcciones x, y y z, respectivamente.
*/
crossProduct({components}) {
return components.reduce((acc, component, index) => acc + component * this.component[index], 0);
}
// Solamente para vectores 3D, probar con este...
crossProduct3D({components}) {
return new Vector(this.component[1] * components[2] - this.component[2] * components[1], this.component[2] * components[0] - this.component[0] * components[2], this.component[0] * components[1] - this.component[1] * components[0]);
}
haveSameDirectionWith(other, EPSILON = 0.00000001) {
const areEqual = (one, other, epsilon = EPSILON) => Math.abs(one - other) < epsilon;
const dotProduct = this.normalize().dotProduct(other.normalize());
return areEqual(dotProduct, 1);
}
haveOppositeDirectionTo(other, EPSILON = 0.00000001) {
const areEqual = (one, other, epsilon = EPSILON) => Math.abs(one - other) < epsilon;
const dotProduct = this.normalize().dotProduct(other.normalize());
return areEqual(dotProduct, -1);
}
isPerpendicularTo(other, EPSILON = 0.00000001) {
const areEqual = (one, other, epsilon = EPSILON) => Math.abs(one - other) < epsilon;
const dotProduct = this.normalize().dotProduct(other.normalize());
return areEqual(dotProduct, 0);
}
// return the acimut of the vector in radians
getAcimut() {
if (this.order >= 2) {
return Math.PI / 2 - Math.atan2(this.y, this.x);
}
};
// set the acimut of the vector in radians
setAcimut(angle) {
let magnitude = this.magnitude;
this.x = Math.cos(Math.PI / 2 - angle) * magnitude;
this.y = Math.sin(Math.PI / 2 - angle) * magnitude;
};
// add two vectors together and return a new one
addition(v2) {
let result = this.clone();
for (let i = 0; i < Math.min(this.order, v2.order); i++) {
result.dimmensions[i] = this.dimmensions[i] + v2.dimmensions[i];
}
return result;
};
// subtract two vectors together and return a new one
subtraction(v2) {
let result = this.clone();
for (let i = 0; i < Math.min(this.order, v2.order); i++) {
result.dimmensions[i] = this.dimmensions[i] - v2.dimmensions[i];
}
return result;
};
// add a vector to this one
additMe(v2) {
for (let i = 0; i < Math.min(this.order, v2.order); i++) {
this.dimmensions[i] = this.dimmensions[i] + v2.dimmensions[i];
}
};
// subtract me a vectors
subtractMe(v2) {
for (let i = 0; i < Math.min(this.order, v2.order); i++) {
this.dimmensions[i] = this.dimmensions[i] - v2.dimmensions[i];
}
};
// multiply this vector by a scalar and return a new one
multiplication(scalar) {
let result = this.clone();
for (let i = 0; i < this.order; i++) {
result.dimmensions[i] *= scalar;
}
return result;
};
// multiply this vector by the scalar
multiplyMe(scalar) {
for (let i = 0; i < this.order; i++) {
this.dimmensions[i] *= scalar;
}
};
// scale this vector
division(scalar) {
let result = this.clone();
for (let i = 0; i < this.order; i++) {
result.dimmensions[i] /= scalar;
}
return result;
};
// scale this vector
divideMe(scalar) {
for (let i = 0; i < this.order; i++) {
this.dimmensions[i] /= scalar;
}
};
// opposite to this vector
opposition(scalar) {
let result = this.clone();
for (let i = 0; i < this.order; i++) {
result.dimmensions[i] = -this.dimmensions[i];
}
return result;
};
// oppose this vector
opposeMe(scalar) {
for (let i = 0; i < this.order; i++) {
this.dimmensions[i] = -this.dimmensions[i];
}
};
// probar
avancePolar(direccion, pasos) {
// Avanzar cierta cantidad de pasos Plano XY, en esa dirección (en grados).
if (direccion instanceof Vector) {
return this.avancePolar(this.angle(direccion), pasos);
}
this.x = this.x + pasos * Math.cos((direccion * Math.PI) / 180);
this.y = this.y + pasos * Math.sin((direccion * Math.PI) / 180);
return this;
}
// probar
avanzarHacia(punto, pasos = 1, desviacion = 0) {
// Avanzar cierta cantidad de pasos Plano XY, en esa dirección (en grados).
let direccion = this.angle(punto) + desviacion;
this.x = this.x + pasos * Math.cos((direccion * Math.PI) / 180);
this.y = this.y + pasos * Math.sin((direccion * Math.PI) / 180);
return this;
}
// probar
alejarseDe(punto, pasos = 1, desviacion = 0) {
// Avanzar cierta cantidad de pasos Plano XY, en esa dirección (en grados).
let direccion = this.angle(punto) + desviacion;
this.x = this.x + pasos * Math.cos((direccion * Math.PI) / 180);
this.y = this.y + pasos * Math.sin((direccion * Math.PI) / 180);
return this;
}
// Rotaciones en los tres ejes de coordenadas
rotarEnEjeZ(angulo, centro = new Vector(0, 0, 0)) { // done, validated
// Rotación en el plano XY (con eje Z).
let ax = this.x - centro.x;
let ay = this.y - centro.y;
let az = this.z - centro.z;
angulo = (angulo * Math.PI) / 180;
let tmpx = ax * Math.cos(angulo) - ay * Math.sin(angulo);
let tmpy = ax * Math.sin(angulo) + ay * Math.cos(angulo);
let tmpz = az;
this.x = tmpx + centro.x;
this.y = tmpy + centro.y;
this.z = tmpz + centro.z;
return this;
}
// validar
rotarEnEjeX(angulo, centro = new Vector(0, 0, 0)) {
// Rotación en el plano ZY (con eje X).
let ax = this.x - centro.x;
let ay = this.y - centro.y;
let az = this.z - centro.z;
angulo = (angulo * Math.PI) / 180;
let tmpx = ax;
let tmpy = ay * Math.cos(angulo) - az * Math.sin(angulo);
let tmpz = ay * Math.sin(angulo) + az * Math.cos(angulo);
this.x = tmpx + centro.x;
this.y = tmpy + centro.y;
this.z = tmpz + centro.z;
return this;
}
// validar
rotarEnEjeY(angulo, centro = new Vector(0, 0, 0)) {
// Rotación en el plano ZX (con eje Y).
let ax = this.x - centro.x;
let ay = this.y - centro.y;
let az = this.z - centro.z;
angulo = (angulo * Math.PI) / 180;
let tmpy = ay;
let tmpx = ax * Math.cos(angulo) + az * Math.sin(angulo);
let tmpz = -ax * Math.sin(angulo) + az * Math.cos(angulo);
this.x = tmpx + centro.x;
this.y = tmpy + centro.y;
this.z = tmpz + centro.z;
return this;
}
// Rotación (tomando como eje cualquier vector)
rotarEnEjeArbitrario(eje, angulo) {
// Vectores canónicos...
let ejeX = new Vector(1, 0, 0);
let ejeY = new Vector(0, 1, 0);
let ejeZ = new Vector(0, 0, 1);
// Bastan dos, para qué el tercero (Rho es ángulo con eje z, y sita con eje x)
let rho = ejeX.angle(eje);
let sita = ejeY.angle(eje);
// terminar
}
}