-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathmath.vector.ts
More file actions
5604 lines (5034 loc) · 210 KB
/
Copy pathmath.vector.ts
File metadata and controls
5604 lines (5034 loc) · 210 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
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Scalar } from "./math.scalar";
import { Epsilon } from './math.constants';
import { Viewport } from './math.viewport';
import { DeepImmutable, Nullable, FloatArray, float } from "../types";
import { ArrayTools } from '../Misc/arrayTools';
import { IPlaneLike } from './math.like';
import { _TypeStore } from '../Misc/typeStore';
import { Plane } from './math.plane';
import { PerformanceConfigurator } from '../Engines/performanceConfigurator';
/**
* Class representing a vector containing 2 coordinates
*/
export class Vector2 {
/**
* Creates a new Vector2 from the given x and y coordinates
* @param x defines the first coordinate
* @param y defines the second coordinate
*/
constructor(
/** defines the first coordinate */
public x: number = 0,
/** defines the second coordinate */
public y: number = 0) {
}
/**
* Gets a string with the Vector2 coordinates
* @returns a string with the Vector2 coordinates
*/
public toString(): string {
return "{X: " + this.x + " Y:" + this.y + "}";
}
/**
* Gets class name
* @returns the string "Vector2"
*/
public getClassName(): string {
return "Vector2";
}
/**
* Gets current vector hash code
* @returns the Vector2 hash code as a number
*/
public getHashCode(): number {
let hash = this.x | 0;
hash = (hash * 397) ^ (this.y | 0);
return hash;
}
// Operators
/**
* Sets the Vector2 coordinates in the given array or Float32Array from the given index.
* @param array defines the source array
* @param index defines the offset in source array
* @returns the current Vector2
*/
public toArray(array: FloatArray, index: number = 0): Vector2 {
array[index] = this.x;
array[index + 1] = this.y;
return this;
}
/**
* Update the current vector from an array
* @param array defines the destination array
* @param index defines the offset in the destination array
* @returns the current Vector3
*/
public fromArray(array: FloatArray, index: number = 0): Vector2 {
Vector2.FromArrayToRef(array, index, this);
return this;
}
/**
* Copy the current vector to an array
* @returns a new array with 2 elements: the Vector2 coordinates.
*/
public asArray(): number[] {
var result = new Array<number>();
this.toArray(result, 0);
return result;
}
/**
* Sets the Vector2 coordinates with the given Vector2 coordinates
* @param source defines the source Vector2
* @returns the current updated Vector2
*/
public copyFrom(source: DeepImmutable<Vector2>): Vector2 {
this.x = source.x;
this.y = source.y;
return this;
}
/**
* Sets the Vector2 coordinates with the given floats
* @param x defines the first coordinate
* @param y defines the second coordinate
* @returns the current updated Vector2
*/
public copyFromFloats(x: number, y: number): Vector2 {
this.x = x;
this.y = y;
return this;
}
/**
* Sets the Vector2 coordinates with the given floats
* @param x defines the first coordinate
* @param y defines the second coordinate
* @returns the current updated Vector2
*/
public set(x: number, y: number): Vector2 {
return this.copyFromFloats(x, y);
}
/**
* Add another vector with the current one
* @param otherVector defines the other vector
* @returns a new Vector2 set with the addition of the current Vector2 and the given one coordinates
*/
public add(otherVector: DeepImmutable<Vector2>): Vector2 {
return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
}
/**
* Sets the "result" coordinates with the addition of the current Vector2 and the given one coordinates
* @param otherVector defines the other vector
* @param result defines the target vector
* @returns the unmodified current Vector2
*/
public addToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
result.x = this.x + otherVector.x;
result.y = this.y + otherVector.y;
return this;
}
/**
* Set the Vector2 coordinates by adding the given Vector2 coordinates
* @param otherVector defines the other vector
* @returns the current updated Vector2
*/
public addInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
this.x += otherVector.x;
this.y += otherVector.y;
return this;
}
/**
* Gets a new Vector2 by adding the current Vector2 coordinates to the given Vector3 x, y coordinates
* @param otherVector defines the other vector
* @returns a new Vector2
*/
public addVector3(otherVector: Vector3): Vector2 {
return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
}
/**
* Gets a new Vector2 set with the subtracted coordinates of the given one from the current Vector2
* @param otherVector defines the other vector
* @returns a new Vector2
*/
public subtract(otherVector: Vector2): Vector2 {
return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
}
/**
* Sets the "result" coordinates with the subtraction of the given one from the current Vector2 coordinates.
* @param otherVector defines the other vector
* @param result defines the target vector
* @returns the unmodified current Vector2
*/
public subtractToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
result.x = this.x - otherVector.x;
result.y = this.y - otherVector.y;
return this;
}
/**
* Sets the current Vector2 coordinates by subtracting from it the given one coordinates
* @param otherVector defines the other vector
* @returns the current updated Vector2
*/
public subtractInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
this.x -= otherVector.x;
this.y -= otherVector.y;
return this;
}
/**
* Multiplies in place the current Vector2 coordinates by the given ones
* @param otherVector defines the other vector
* @returns the current updated Vector2
*/
public multiplyInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
this.x *= otherVector.x;
this.y *= otherVector.y;
return this;
}
/**
* Returns a new Vector2 set with the multiplication of the current Vector2 and the given one coordinates
* @param otherVector defines the other vector
* @returns a new Vector2
*/
public multiply(otherVector: DeepImmutable<Vector2>): Vector2 {
return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
}
/**
* Sets "result" coordinates with the multiplication of the current Vector2 and the given one coordinates
* @param otherVector defines the other vector
* @param result defines the target vector
* @returns the unmodified current Vector2
*/
public multiplyToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
result.x = this.x * otherVector.x;
result.y = this.y * otherVector.y;
return this;
}
/**
* Gets a new Vector2 set with the Vector2 coordinates multiplied by the given floats
* @param x defines the first coordinate
* @param y defines the second coordinate
* @returns a new Vector2
*/
public multiplyByFloats(x: number, y: number): Vector2 {
return new Vector2(this.x * x, this.y * y);
}
/**
* Returns a new Vector2 set with the Vector2 coordinates divided by the given one coordinates
* @param otherVector defines the other vector
* @returns a new Vector2
*/
public divide(otherVector: Vector2): Vector2 {
return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
}
/**
* Sets the "result" coordinates with the Vector2 divided by the given one coordinates
* @param otherVector defines the other vector
* @param result defines the target vector
* @returns the unmodified current Vector2
*/
public divideToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
result.x = this.x / otherVector.x;
result.y = this.y / otherVector.y;
return this;
}
/**
* Divides the current Vector2 coordinates by the given ones
* @param otherVector defines the other vector
* @returns the current updated Vector2
*/
public divideInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
return this.divideToRef(otherVector, this);
}
/**
* Gets a new Vector2 with current Vector2 negated coordinates
* @returns a new Vector2
*/
public negate(): Vector2 {
return new Vector2(-this.x, -this.y);
}
/**
* Negate this vector in place
* @returns this
*/
public negateInPlace(): Vector2 {
this.x *= -1;
this.y *= -1;
return this;
}
/**
* Negate the current Vector2 and stores the result in the given vector "result" coordinates
* @param result defines the Vector3 object where to store the result
* @returns the current Vector2
*/
public negateToRef(result: Vector2): Vector2 {
return result.copyFromFloats(this.x * -1, this.y * -1);
}
/**
* Multiply the Vector2 coordinates by scale
* @param scale defines the scaling factor
* @returns the current updated Vector2
*/
public scaleInPlace(scale: number): Vector2 {
this.x *= scale;
this.y *= scale;
return this;
}
/**
* Returns a new Vector2 scaled by "scale" from the current Vector2
* @param scale defines the scaling factor
* @returns a new Vector2
*/
public scale(scale: number): Vector2 {
let result = new Vector2(0, 0);
this.scaleToRef(scale, result);
return result;
}
/**
* Scale the current Vector2 values by a factor to a given Vector2
* @param scale defines the scale factor
* @param result defines the Vector2 object where to store the result
* @returns the unmodified current Vector2
*/
public scaleToRef(scale: number, result: Vector2): Vector2 {
result.x = this.x * scale;
result.y = this.y * scale;
return this;
}
/**
* Scale the current Vector2 values by a factor and add the result to a given Vector2
* @param scale defines the scale factor
* @param result defines the Vector2 object where to store the result
* @returns the unmodified current Vector2
*/
public scaleAndAddToRef(scale: number, result: Vector2): Vector2 {
result.x += this.x * scale;
result.y += this.y * scale;
return this;
}
/**
* Gets a boolean if two vectors are equals
* @param otherVector defines the other vector
* @returns true if the given vector coordinates strictly equal the current Vector2 ones
*/
public equals(otherVector: DeepImmutable<Vector2>): boolean {
return otherVector && this.x === otherVector.x && this.y === otherVector.y;
}
/**
* Gets a boolean if two vectors are equals (using an epsilon value)
* @param otherVector defines the other vector
* @param epsilon defines the minimal distance to consider equality
* @returns true if the given vector coordinates are close to the current ones by a distance of epsilon.
*/
public equalsWithEpsilon(otherVector: DeepImmutable<Vector2>, epsilon: number = Epsilon): boolean {
return otherVector && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon) && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon);
}
/**
* Gets a new Vector2 from current Vector2 floored values
* @returns a new Vector2
*/
public floor(): Vector2 {
return new Vector2(Math.floor(this.x), Math.floor(this.y));
}
/**
* Gets a new Vector2 from current Vector2 floored values
* @returns a new Vector2
*/
public fract(): Vector2 {
return new Vector2(this.x - Math.floor(this.x), this.y - Math.floor(this.y));
}
// Properties
/**
* Gets the length of the vector
* @returns the vector length (float)
*/
public length(): number {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
/**
* Gets the vector squared length
* @returns the vector squared length (float)
*/
public lengthSquared(): number {
return (this.x * this.x + this.y * this.y);
}
// Methods
/**
* Normalize the vector
* @returns the current updated Vector2
*/
public normalize(): Vector2 {
var len = this.length();
if (len === 0) {
return this;
}
this.x /= len;
this.y /= len;
return this;
}
/**
* Gets a new Vector2 copied from the Vector2
* @returns a new Vector2
*/
public clone(): Vector2 {
return new Vector2(this.x, this.y);
}
// Statics
/**
* Gets a new Vector2(0, 0)
* @returns a new Vector2
*/
public static Zero(): Vector2 {
return new Vector2(0, 0);
}
/**
* Gets a new Vector2(1, 1)
* @returns a new Vector2
*/
public static One(): Vector2 {
return new Vector2(1, 1);
}
/**
* Gets a new Vector2 set from the given index element of the given array
* @param array defines the data source
* @param offset defines the offset in the data source
* @returns a new Vector2
*/
public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Vector2 {
return new Vector2(array[offset], array[offset + 1]);
}
/**
* Sets "result" from the given index element of the given array
* @param array defines the data source
* @param offset defines the offset in the data source
* @param result defines the target vector
*/
public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Vector2): void {
result.x = array[offset];
result.y = array[offset + 1];
}
/**
* Gets a new Vector2 located for "amount" (float) on the CatmullRom spline defined by the given four Vector2
* @param value1 defines 1st point of control
* @param value2 defines 2nd point of control
* @param value3 defines 3rd point of control
* @param value4 defines 4th point of control
* @param amount defines the interpolation factor
* @returns a new Vector2
*/
public static CatmullRom(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>, value3: DeepImmutable<Vector2>, value4: DeepImmutable<Vector2>, amount: number): Vector2 {
var squared = amount * amount;
var cubed = amount * squared;
var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
(((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
(((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
return new Vector2(x, y);
}
/**
* Returns a new Vector2 set with same the coordinates than "value" ones if the vector "value" is in the square defined by "min" and "max".
* If a coordinate of "value" is lower than "min" coordinates, the returned Vector2 is given this "min" coordinate.
* If a coordinate of "value" is greater than "max" coordinates, the returned Vector2 is given this "max" coordinate
* @param value defines the value to clamp
* @param min defines the lower limit
* @param max defines the upper limit
* @returns a new Vector2
*/
public static Clamp(value: DeepImmutable<Vector2>, min: DeepImmutable<Vector2>, max: DeepImmutable<Vector2>): Vector2 {
var x = value.x;
x = (x > max.x) ? max.x : x;
x = (x < min.x) ? min.x : x;
var y = value.y;
y = (y > max.y) ? max.y : y;
y = (y < min.y) ? min.y : y;
return new Vector2(x, y);
}
/**
* Returns a new Vector2 located for "amount" (float) on the Hermite spline defined by the vectors "value1", "value3", "tangent1", "tangent2"
* @param value1 defines the 1st control point
* @param tangent1 defines the outgoing tangent
* @param value2 defines the 2nd control point
* @param tangent2 defines the incoming tangent
* @param amount defines the interpolation factor
* @returns a new Vector2
*/
public static Hermite(value1: DeepImmutable<Vector2>, tangent1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>, tangent2: DeepImmutable<Vector2>, amount: number): Vector2 {
var squared = amount * amount;
var cubed = amount * squared;
var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
var part2 = (-2.0 * cubed) + (3.0 * squared);
var part3 = (cubed - (2.0 * squared)) + amount;
var part4 = cubed - squared;
var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
return new Vector2(x, y);
}
/**
* Returns a new Vector2 located for "amount" (float) on the linear interpolation between the vector "start" adn the vector "end".
* @param start defines the start vector
* @param end defines the end vector
* @param amount defines the interpolation factor
* @returns a new Vector2
*/
public static Lerp(start: DeepImmutable<Vector2>, end: DeepImmutable<Vector2>, amount: number): Vector2 {
var x = start.x + ((end.x - start.x) * amount);
var y = start.y + ((end.y - start.y) * amount);
return new Vector2(x, y);
}
/**
* Gets the dot product of the vector "left" and the vector "right"
* @param left defines first vector
* @param right defines second vector
* @returns the dot product (float)
*/
public static Dot(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): number {
return left.x * right.x + left.y * right.y;
}
/**
* Returns a new Vector2 equal to the normalized given vector
* @param vector defines the vector to normalize
* @returns a new Vector2
*/
public static Normalize(vector: DeepImmutable<Vector2>): Vector2 {
var newVector = vector.clone();
newVector.normalize();
return newVector;
}
/**
* Gets a new Vector2 set with the minimal coordinate values from the "left" and "right" vectors
* @param left defines 1st vector
* @param right defines 2nd vector
* @returns a new Vector2
*/
public static Minimize(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): Vector2 {
var x = (left.x < right.x) ? left.x : right.x;
var y = (left.y < right.y) ? left.y : right.y;
return new Vector2(x, y);
}
/**
* Gets a new Vecto2 set with the maximal coordinate values from the "left" and "right" vectors
* @param left defines 1st vector
* @param right defines 2nd vector
* @returns a new Vector2
*/
public static Maximize(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): Vector2 {
var x = (left.x > right.x) ? left.x : right.x;
var y = (left.y > right.y) ? left.y : right.y;
return new Vector2(x, y);
}
/**
* Gets a new Vector2 set with the transformed coordinates of the given vector by the given transformation matrix
* @param vector defines the vector to transform
* @param transformation defines the matrix to apply
* @returns a new Vector2
*/
public static Transform(vector: DeepImmutable<Vector2>, transformation: DeepImmutable<Matrix>): Vector2 {
let r = Vector2.Zero();
Vector2.TransformToRef(vector, transformation, r);
return r;
}
/**
* Transforms the given vector coordinates by the given transformation matrix and stores the result in the vector "result" coordinates
* @param vector defines the vector to transform
* @param transformation defines the matrix to apply
* @param result defines the target vector
*/
public static TransformToRef(vector: DeepImmutable<Vector2>, transformation: DeepImmutable<Matrix>, result: Vector2) {
const m = transformation.m;
var x = (vector.x * m[0]) + (vector.y * m[4]) + m[12];
var y = (vector.x * m[1]) + (vector.y * m[5]) + m[13];
result.x = x;
result.y = y;
}
/**
* Determines if a given vector is included in a triangle
* @param p defines the vector to test
* @param p0 defines 1st triangle point
* @param p1 defines 2nd triangle point
* @param p2 defines 3rd triangle point
* @returns true if the point "p" is in the triangle defined by the vertors "p0", "p1", "p2"
*/
public static PointInTriangle(p: DeepImmutable<Vector2>, p0: DeepImmutable<Vector2>, p1: DeepImmutable<Vector2>, p2: DeepImmutable<Vector2>) {
let a = 1 / 2 * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
let sign = a < 0 ? -1 : 1;
let s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
let t = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;
return s > 0 && t > 0 && (s + t) < 2 * a * sign;
}
/**
* Gets the distance between the vectors "value1" and "value2"
* @param value1 defines first vector
* @param value2 defines second vector
* @returns the distance between vectors
*/
public static Distance(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): number {
return Math.sqrt(Vector2.DistanceSquared(value1, value2));
}
/**
* Returns the squared distance between the vectors "value1" and "value2"
* @param value1 defines first vector
* @param value2 defines second vector
* @returns the squared distance between vectors
*/
public static DistanceSquared(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): number {
var x = value1.x - value2.x;
var y = value1.y - value2.y;
return (x * x) + (y * y);
}
/**
* Gets a new Vector2 located at the center of the vectors "value1" and "value2"
* @param value1 defines first vector
* @param value2 defines second vector
* @returns a new Vector2
*/
public static Center(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): Vector2 {
var center = value1.add(value2);
center.scaleInPlace(0.5);
return center;
}
/**
* Gets the shortest distance (float) between the point "p" and the segment defined by the two points "segA" and "segB".
* @param p defines the middle point
* @param segA defines one point of the segment
* @param segB defines the other point of the segment
* @returns the shortest distance
*/
public static DistanceOfPointFromSegment(p: DeepImmutable<Vector2>, segA: DeepImmutable<Vector2>, segB: DeepImmutable<Vector2>): number {
let l2 = Vector2.DistanceSquared(segA, segB);
if (l2 === 0.0) {
return Vector2.Distance(p, segA);
}
let v = segB.subtract(segA);
let t = Math.max(0, Math.min(1, Vector2.Dot(p.subtract(segA), v) / l2));
let proj = segA.add(v.multiplyByFloats(t, t));
return Vector2.Distance(p, proj);
}
}
/**
* Class used to store (x,y,z) vector representation
* A Vector3 is the main object used in 3D geometry
* It can represent etiher the coordinates of a point the space, either a direction
* Reminder: js uses a left handed forward facing system
*/
export class Vector3 {
private static _UpReadOnly = Vector3.Up() as DeepImmutable<Vector3>;
private static _ZeroReadOnly = Vector3.Zero() as DeepImmutable<Vector3>;
/** @hidden */
public _x: number;
/** @hidden */
public _y: number;
/** @hidden */
public _z: number;
/** @hidden */
public _isDirty = true;
/** Gets or sets the x coordinate */
public get x() {
return this._x;
}
public set x(value: number) {
this._x = value;
this._isDirty = true;
}
/** Gets or sets the y coordinate */
public get y() {
return this._y;
}
public set y(value: number) {
this._y = value;
this._isDirty = true;
}
/** Gets or sets the z coordinate */
public get z() {
return this._z;
}
public set z(value: number) {
this._z = value;
this._isDirty = true;
}
/**
* Creates a new Vector3 object from the given x, y, z (floats) coordinates.
* @param x defines the first coordinates (on X axis)
* @param y defines the second coordinates (on Y axis)
* @param z defines the third coordinates (on Z axis)
*/
constructor(
x: number = 0,
y: number = 0,
z: number = 0
) {
this._x = x;
this._y = y;
this._z = z;
}
/**
* Creates a string representation of the Vector3
* @returns a string with the Vector3 coordinates.
*/
public toString(): string {
return "{X: " + this._x + " Y:" + this._y + " Z:" + this._z + "}";
}
/**
* Gets the class name
* @returns the string "Vector3"
*/
public getClassName(): string {
return "Vector3";
}
/**
* Creates the Vector3 hash code
* @returns a number which tends to be unique between Vector3 instances
*/
public getHashCode(): number {
let hash = this._x | 0;
hash = (hash * 397) ^ (this._y | 0);
hash = (hash * 397) ^ (this._z | 0);
return hash;
}
// Operators
/**
* Creates an array containing three elements : the coordinates of the Vector3
* @returns a new array of numbers
*/
public asArray(): number[] {
var result: number[] = [];
this.toArray(result, 0);
return result;
}
/**
* Populates the given array or Float32Array from the given index with the successive coordinates of the Vector3
* @param array defines the destination array
* @param index defines the offset in the destination array
* @returns the current Vector3
*/
public toArray(array: FloatArray, index: number = 0): Vector3 {
array[index] = this._x;
array[index + 1] = this._y;
array[index + 2] = this._z;
return this;
}
/**
* Update the current vector from an array
* @param array defines the destination array
* @param index defines the offset in the destination array
* @returns the current Vector3
*/
public fromArray(array: FloatArray, index: number = 0): Vector3 {
Vector3.FromArrayToRef(array, index, this);
return this;
}
/**
* Converts the current Vector3 into a quaternion (considering that the Vector3 contains Euler angles representation of a rotation)
* @returns a new Quaternion object, computed from the Vector3 coordinates
*/
public toQuaternion(): Quaternion {
return Quaternion.RotationYawPitchRoll(this._y, this._x, this._z);
}
/**
* Adds the given vector to the current Vector3
* @param otherVector defines the second operand
* @returns the current updated Vector3
*/
public addInPlace(otherVector: DeepImmutable<Vector3>): Vector3 {
return this.addInPlaceFromFloats(otherVector._x, otherVector._y, otherVector._z);
}
/**
* Adds the given coordinates to the current Vector3
* @param x defines the x coordinate of the operand
* @param y defines the y coordinate of the operand
* @param z defines the z coordinate of the operand
* @returns the current updated Vector3
*/
public addInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
this.x += x;
this.y += y;
this.z += z;
return this;
}
/**
* Gets a new Vector3, result of the addition the current Vector3 and the given vector
* @param otherVector defines the second operand
* @returns the resulting Vector3
*/
public add(otherVector: DeepImmutable<Vector3>): Vector3 {
return new Vector3(this._x + otherVector._x, this._y + otherVector._y, this._z + otherVector._z);
}
/**
* Adds the current Vector3 to the given one and stores the result in the vector "result"
* @param otherVector defines the second operand
* @param result defines the Vector3 object where to store the result
* @returns the current Vector3
*/
public addToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
return result.copyFromFloats(this._x + otherVector._x, this._y + otherVector._y, this._z + otherVector._z);
}
/**
* Subtract the given vector from the current Vector3
* @param otherVector defines the second operand
* @returns the current updated Vector3
*/
public subtractInPlace(otherVector: DeepImmutable<Vector3>): Vector3 {
this.x -= otherVector._x;
this.y -= otherVector._y;
this.z -= otherVector._z;
return this;
}
/**
* Returns a new Vector3, result of the subtraction of the given vector from the current Vector3
* @param otherVector defines the second operand
* @returns the resulting Vector3
*/
public subtract(otherVector: DeepImmutable<Vector3>): Vector3 {
return new Vector3(this._x - otherVector._x, this._y - otherVector._y, this._z - otherVector._z);
}
/**
* Subtracts the given vector from the current Vector3 and stores the result in the vector "result".
* @param otherVector defines the second operand
* @param result defines the Vector3 object where to store the result
* @returns the current Vector3
*/
public subtractToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
return this.subtractFromFloatsToRef(otherVector._x, otherVector._y, otherVector._z, result);
}
/**
* Returns a new Vector3 set with the subtraction of the given floats from the current Vector3 coordinates
* @param x defines the x coordinate of the operand
* @param y defines the y coordinate of the operand
* @param z defines the z coordinate of the operand
* @returns the resulting Vector3
*/
public subtractFromFloats(x: number, y: number, z: number): Vector3 {
return new Vector3(this._x - x, this._y - y, this._z - z);
}
/**
* Subtracts the given floats from the current Vector3 coordinates and set the given vector "result" with this result
* @param x defines the x coordinate of the operand
* @param y defines the y coordinate of the operand
* @param z defines the z coordinate of the operand
* @param result defines the Vector3 object where to store the result
* @returns the current Vector3
*/
public subtractFromFloatsToRef(x: number, y: number, z: number, result: Vector3): Vector3 {
return result.copyFromFloats(this._x - x, this._y - y, this._z - z);
}
/**
* Gets a new Vector3 set with the current Vector3 negated coordinates
* @returns a new Vector3
*/
public negate(): Vector3 {
return new Vector3(-this._x, -this._y, -this._z);
}
/**
* Negate this vector in place
* @returns this
*/
public negateInPlace(): Vector3 {
this.x *= -1;
this.y *= -1;
this.z *= -1;
return this;
}
/**
* Negate the current Vector3 and stores the result in the given vector "result" coordinates
* @param result defines the Vector3 object where to store the result
* @returns the current Vector3
*/
public negateToRef(result: Vector3): Vector3 {
return result.copyFromFloats(this._x * -1, this._y * -1, this._z * -1);
}
/**
* Multiplies the Vector3 coordinates by the float "scale"
* @param scale defines the multiplier factor
* @returns the current updated Vector3
*/
public scaleInPlace(scale: number): Vector3 {
this.x *= scale;
this.y *= scale;
this.z *= scale;
return this;
}
/**
* Returns a new Vector3 set with the current Vector3 coordinates multiplied by the float "scale"
* @param scale defines the multiplier factor
* @returns a new Vector3
*/
public scale(scale: number): Vector3 {
return new Vector3(this._x * scale, this._y * scale, this._z * scale);
}
/**
* Multiplies the current Vector3 coordinates by the float "scale" and stores the result in the given vector "result" coordinates
* @param scale defines the multiplier factor
* @param result defines the Vector3 object where to store the result
* @returns the current Vector3
*/
public scaleToRef(scale: number, result: Vector3): Vector3 {
return result.copyFromFloats(this._x * scale, this._y * scale, this._z * scale);
}
/**
* Scale the current Vector3 values by a factor and add the result to a given Vector3
* @param scale defines the scale factor
* @param result defines the Vector3 object where to store the result
* @returns the unmodified current Vector3
*/
public scaleAndAddToRef(scale: number, result: Vector3): Vector3 {
return result.addInPlaceFromFloats(this._x * scale, this._y * scale, this._z * scale);
}
/**
* Projects the current vector3 to a plane along a ray starting from a specified origin and directed towards the point.
* @param origin defines the origin of the projection ray
* @param plane defines the plane to project to
* @returns the projected vector3
*/
public projectOnPlane(plane: Plane, origin: Vector3): Vector3 {
let result = Vector3.Zero();
this.projectOnPlaneToRef(plane, origin, result);
return result;
}
/**
* Projects the current vector3 to a plane along a ray starting from a specified origin and directed towards the point.
* @param origin defines the origin of the projection ray
* @param plane defines the plane to project to
* @param result defines the Vector3 where to store the result