-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
2380 lines (2276 loc) · 64.7 KB
/
index.ts
File metadata and controls
2380 lines (2276 loc) · 64.7 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
/**
* Typescript standalone port of the awesome p5.js Vector utils
* Also included some unity utilities that I thought would be useful to have here
*
* Ported by: Benoit Lavoie-Lamer - iam@xposedbones.com
*/
enum constants {
TWO_PI = Math.PI * 2
}
/**
* A class to describe a two or three dimensional vector, specifically
* a Euclidean (also known as geometric) vector. A vector is an entity
* that has both magnitude and direction. The datatype, however, stores
* the components of the vector (x, y for 2D, and x, y, z for 3D). The magnitude
* and direction can be accessed via the methods <a href="https://p5js.org/reference/#/p5.Vector/mag">mag()</a> and <a href="https://p5js.org/reference/#/p5.Vector/heading">heading()</a>.
*
* In many of the p5.js examples, you will see <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a> used to describe a
* position, velocity, or acceleration. For example, if you consider a rectangle
* moving across the screen, at any given instant it has a position (a vector
* that points from the origin to its location), a velocity (the rate at which
* the object's position changes per time unit, expressed as a vector), and
* acceleration (the rate at which the object's velocity changes per time
* unit, expressed as a vector).
*
* Since vectors represent groupings of values, we cannot simply use
* traditional addition/multiplication/etc. Instead, we'll need to do some
* "vector" math, which is made easy by the methods inside the <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a> class.
*
* @class Vector
* @constructor
* @param {Number} [x] x component of the vector
* @param {Number} [y] y component of the vector
* @param {Number} [z] z component of the vector
* @example
* <div>
* <code>
* let v1 = createVector(40, 50);
* let v2 = createVector(40, 50);
*
* ellipse(v1.x, v1.y, 50, 50);
* ellipse(v2.x, v2.y, 50, 50);
* v1.add(v2);
* ellipse(v1.x, v1.y, 50, 50);
* </code>
* </div>
*
* @alt
* 2 white ellipses. One center-left the other bottom right and off canvas
*/
export class Vector {
constructor(public x: number, public y: number = 0, public z: number = 0) {}
//#region Static methods
// Helpers from unity that would be useful here
public static up(): Vector {
return new Vector(0,1,0);
};
public static down(): Vector {
return new Vector(0, -1, 0);
};
public static left(): Vector {
return new Vector(-1, 0, 0);
};
public static right(): Vector {
return new Vector(1, 0, 0);
};
public static forward(): Vector {
return new Vector(0, 0, 1);
};
public static back(): Vector {
return new Vector(0, 0, -1);
};
/**
* Make a new 2D vector from an angle
*
* @method fromAngle
* @static
* @param {Number} angle the desired angle, in radians (unaffected by <a href="https://p5js.org/reference/#/p5/angleMode">angleMode</a>)
* @param {Number} [length] the length of the new vector (defaults to 1)
* @return {Vector} the new <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a> object
* @example
* <div>
* <code>
* function draw() {
* background(200);
*
* // Create a variable, proportional to the mouseX,
* // varying from 0-360, to represent an angle in degrees.
* let myDegrees = map(mouseX, 0, width, 0, 360);
*
* // Display that variable in an onscreen text.
* // (Note the nfc() function to truncate additional decimal places,
* // and the "\xB0" character for the degree symbol.)
* let readout = 'angle = ' + nfc(myDegrees, 1) + '\xB0';
* noStroke();
* fill(0);
* text(readout, 5, 15);
*
* // Create a p5.Vector using the fromAngle function,
* // and extract its x and y components.
* let v = p5.Vector.fromAngle(radians(myDegrees), 30);
* let vx = v.x;
* let vy = v.y;
*
* push();
* translate(width / 2, height / 2);
* noFill();
* stroke(150);
* line(0, 0, 30, 0);
* stroke(0);
* line(0, 0, vx, vy);
* pop();
* }
* </code>
* </div>
*/
public static fromAngle(angle: number, length: number = 1): Vector {
return new Vector(length * Math.cos(angle), Math.sin(angle), 0);
}
/**
* Make a new 3D vector from a pair of ISO spherical angles
*
* @method fromAngles
* @static
* @param {Number} theta the polar angle, in radians (zero is up)
* @param {Number} phi the azimuthal angle, in radians
* (zero is out of the screen)
* @param {Number} [length] the length of the new vector (defaults to 1)
* @return {Vector} the new <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a> object
* @example
* <div modernizr='webgl'>
* <code>
* function setup() {
* createCanvas(100, 100, WEBGL);
* fill(255);
* noStroke();
* }
* function draw() {
* background(255);
*
* let t = millis() / 1000;
*
* // add three point lights
* pointLight(color('#f00'), p5.Vector.fromAngles(t * 1.0, t * 1.3, 100));
* pointLight(color('#0f0'), p5.Vector.fromAngles(t * 1.1, t * 1.2, 100));
* pointLight(color('#00f'), p5.Vector.fromAngles(t * 1.2, t * 1.1, 100));
*
* sphere(35);
* }
* </code>
* </div>
*/
public static fromAngles(theta: number, phi: number, length: number = 1): Vector {
const cosPhi: number = Math.cos(phi);
const sinPhi: number = Math.sin(phi);
const cosTheta: number = Math.cos(theta);
const sinTheta: number = Math.sin(theta);
return new Vector(
length * sinTheta * sinPhi,
-length * cosTheta,
length * sinTheta * cosPhi
);
}
/**
* Make a new 2D unit vector from a random angle
*
* @method random2D
* @static
* @return {Vector} the new <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a> object
* @example
* <div class="norender">
* <code>
* let v = p5.Vector.random2D();
* // May make v's attributes something like:
* // [0.61554617, -0.51195765, 0.0] or
* // [-0.4695841, -0.14366731, 0.0] or
* // [0.6091097, -0.22805278, 0.0]
* print(v);
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* frameRate(1);
* }
*
* function draw() {
* background(240);
*
* let v0 = createVector(50, 50);
* let v1 = p5.Vector.random2D();
* drawArrow(v0, v1.mult(50), 'black');
* }
*
* // draw an arrow for a vector at a given base position
* function drawArrow(base, vec, myColor) {
* push();
* stroke(myColor);
* strokeWeight(3);
* fill(myColor);
* translate(base.x, base.y);
* line(0, 0, vec.x, vec.y);
* rotate(vec.heading());
* let arrowSize = 7;
* translate(vec.mag() - arrowSize, 0);
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
* pop();
* }
* </code>
* </div>
*/
public static random2D(): Vector {
return this.fromAngle(Math.random() * constants.TWO_PI);
}
/**
* Make a new random 3D unit vector.
*
* @method random3D
* @static
* @return {Vector} the new <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a> object
* @example
* <div class="norender">
* <code>
* let v = p5.Vector.random3D();
* // May make v's attributes something like:
* // [0.61554617, -0.51195765, 0.599168] or
* // [-0.4695841, -0.14366731, -0.8711202] or
* // [0.6091097, -0.22805278, -0.7595902]
* print(v);
* </code>
* </div>
*/
public static random3D(): Vector {
const angle: number = Math.random() * constants.TWO_PI;
const vz: number = Math.random() * 2 - 1;
const vzBase: number = Math.sqrt(1 - vz * vz);
const vx: number = vzBase * Math.cos(angle);
const vy: number = vzBase * Math.sin(angle);
return new Vector(vx, vy, vz);
}
// Adds two vectors together and returns a new one.
/**
* @method add
* @static
* @param {Vector} v1 a <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a> to add
* @param {Vector} v2 a <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a> to add
* @param {Vector} [target] the vector to receive the result
* @return {Vector} the resulting <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>
*/
public static add(v1: Vector, v2: Vector, target?: Vector): Vector {
if (!target) {
target = v1.copy();
if (arguments.length === 3) {
console.warn(
'The target parameter is undefined, it should be of type Vector',
'Vector.add'
);
}
} else {
target.set(v1);
}
target.add(v2);
return target;
}
// Returns a vector remainder when it is divided by another vector
/**
* @method rem
* @static
* @param {Vector} v1 dividend <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>
* @param {Vector} v2 divisor <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>
*/
/**
* @method rem
* @static
* @param {Vector} v1
* @param {Vector} v2
* @return {Vector} the resulting <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>
*/
public static rem(v1: Vector, v2: Vector): Vector | undefined {
if (v1 instanceof Vector && v2 instanceof Vector) {
let target: Vector = v1.copy();
target.rem(v2);
return target;
}
}
/*
* Subtracts one <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a> from another and returns a new one. The second
* vector (v2) is subtracted from the first (v1), resulting in v1-v2.
*/
/**
* @method sub
* @static
* @param {Vector} v1 a <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a> to subtract from
* @param {Vector} v2 a <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a> to subtract
* @param {Vector} [target] the vector to receive the result
* @return {Vector} the resulting <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>
*/
public static sub(v1: Vector, v2: Vector, target?: Vector): Vector {
if (!target) {
target = v1.copy();
if (arguments.length === 3) {
console.warn(
'The target parameter is undefined, it should be of type Vector',
'Vector.sub'
);
}
} else {
target.set(v1);
}
target.sub(v2);
return target;
}
/**
* @method mult
* @static
* @param {Number} x
* @param {Number} y
* @param {Number} [z]
* @return {Vector} The resulting new <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>
*/
/**
* @method mult
* @static
* @param {Vector} v
* @param {Number} n
* @param {Vector} [target] the vector to receive the result
*/
/**
* @method mult
* @static
* @param {Vector} v0
* @param {Vector} v1
* @param {Vector} [target]
*/
/**
* @method mult
* @static
* @param {Vector} v0
* @param {Number[]} arr
* @param {Vector} [target]
*/
public static mult(v: Vector, n: ParamsType, target?: Vector): Vector {
if (!target) {
target = v.copy();
if (arguments.length === 3) {
console.warn(
'The target parameter is undefined, it should be of type Vector',
'Vector.mult'
);
}
} else {
target.set(v);
}
target.mult(n);
return target;
}
/**
* Rotates the vector (only 2D vectors) by the given angle, magnitude remains the same and returns a new vector.
*/
/**
* @method rotate
* @static
* @param {Vector} v
* @param {Number} angle
* @param {Vector} [target] the vector to receive the result
*/
public static rotate(v: Vector, a: number, target: Vector): Vector {
if (arguments.length === 2) {
target = v.copy();
} else {
if (!(target instanceof Vector)) {
console.warn(
'The target parameter should be of type Vector',
'Vector.rotate'
);
}
target.set(v);
}
target.rotate(a);
return target;
}
/**
* Divides a vector by a scalar and returns a new vector.
*/
/**
* @method div
* @static
* @param {Number} x
* @param {Number} y
* @param {Number} [z]
* @return {Vector} The resulting new <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>
*/
/**
* @method div
* @static
* @param {Vector} v
* @param {Number} n
* @param {Vector} [target] the vector to receive the result
*/
/**
* @method div
* @static
* @param {Vector} v0
* @param {Vector} v1
* @param {Vector} [target]
*/
/**
* @method div
* @static
* @param {Vector} v0
* @param {Number[]} arr
* @param {Vector} [target]
*/
public static div(v: Vector, n: ParamsType, target: Vector): Vector {
if (!target) {
target = v.copy();
if (arguments.length === 3) {
console.warn(
'The target parameter is undefined, it should be of type Vector',
'Vector.div'
);
}
} else {
target.set(v);
}
target.div(n);
return target;
}
/**
* Calculates the dot product of two vectors.
*/
/**
* @method dot
* @static
* @param {Vector} v1 the first <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>
* @param {Vector} v2 the second <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>
* @return {Number} the dot product
*/
public static dot(v1: Vector, v2: Vector): number {
return v1.dot(v2);
}
/**
* Calculates the cross product of two vectors.
*/
/**
* @method cross
* @static
* @param {Vector} v1 the first <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>
* @param {Vector} v2 the second <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>
* @return {Number} the cross product
*/
public static cross(v1: Vector, v2: Vector): Vector {
return v1.cross(v2);
}
/**
* Calculates the Euclidean distance between two points (considering a
* point as a vector object).
*/
/**
* @method dist
* @static
* @param {Vector} v1 the first <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>
* @param {Vector} v2 the second <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>
* @return {Number} the distance
*/
public static dist(v1: Vector, v2: Vector): number {
return v1.dist(v2);
}
/**
* Linear interpolate a vector to another vector and return the result as a
* new vector.
*/
/**
* @method lerp
* @static
* @param {Vector} v1
* @param {Vector} v2
* @param {Number} amt
* @param {Vector} [target] the vector to receive the result
* @return {Vector} the lerped value
*/
public static lerp(v1: Vector, v2: Vector, amt: number, target: Vector): Vector {
if (!target) {
target = v1.copy();
if (arguments.length === 4) {
console.warn(
'The target parameter is undefined, it should be of type Vector',
'Vector.lerp'
);
}
} else {
target.set(v1);
}
target.lerp(v2, amt);
return target;
}
/**
* Calculates the magnitude (length) of the vector and returns the result as
* a float (this is simply the equation sqrt(x\*x + y\*y + z\*z).)
*/
/**
* @method mag
* @static
* @param {Vector} vecT the vector to return the magnitude of
* @return {Number} the magnitude of vecT
*/
public static mag(vecT: Vector): number {
const x: number = vecT.x,
y: number = vecT.y,
z: number = vecT.z;
const magSq = x * x + y * y + z * z;
return Math.sqrt(magSq);
}
/**
* Normalize the vector to length 1 (make it a unit vector).
*/
/**
* @method normalize
* @static
* @param {Vector} v the vector to normalize
* @param {Vector} [target] the vector to receive the result
* @return {Vector} v normalized to a length of 1
*/
public static normalize(v: Vector, target: Vector): Vector {
if (arguments.length < 2) {
target = v.copy();
} else {
if (!(target instanceof Vector)) {
console.warn(
'The target parameter should be of type Vector',
'Vector.normalize'
);
}
target.set(v);
}
return target.normalize();
}
//#endregion
//#region Private Methods / Helpers
private calculateRemainder2D(xComponent: number, yComponent: number): Vector {
if (xComponent !== 0) {
this.x = this.x % xComponent;
}
if (yComponent !== 0) {
this.y = this.y % yComponent;
}
return this;
}
private calculateRemainder3D(xComponent: number, yComponent: number, zComponent: number): Vector {
if (xComponent !== 0) {
this.x = this.x % xComponent;
}
if (yComponent !== 0) {
this.y = this.y % yComponent;
}
if (zComponent !== 0) {
this.z = this.z % zComponent;
}
return this;
}
//#endregion
//#region Public Methods
/**
* Returns a string representation of a vector v by calling String(v)
* or v.toString(). This method is useful for logging vectors in the
* console.
* @method toString
* @return {String}
* @example
* <div class = "norender">
* <code>
* function setup() {
* let v = createVector(20, 30);
* print(String(v)); // prints "p5.Vector Object : [20, 30, 0]"
* }
* </code>
* </div>
*
* <div>
* <code>
* function draw() {
* background(240);
*
* let v0 = createVector(0, 0);
* let v1 = createVector(mouseX, mouseY);
* drawArrow(v0, v1, 'black');
*
* noStroke();
* text(v1.toString(), 10, 25, 90, 75);
* }
*
* // draw an arrow for a vector at a given base position
* function drawArrow(base, vec, myColor) {
* push();
* stroke(myColor);
* strokeWeight(3);
* fill(myColor);
* translate(base.x, base.y);
* line(0, 0, vec.x, vec.y);
* rotate(vec.heading());
* let arrowSize = 7;
* translate(vec.mag() - arrowSize, 0);
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
* pop();
* }
* </code>
* </div>
*/
public toString(): string {
return `Vector Object: [${this.x}, ${this.y}, ${this.z}]`;
}
/**
* Sets the x, y, and z component of the vector using two or three separate
* variables, the data from a <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>, or the values from a float array.
* @method set
* @param {Number} [x] the x component of the vector
* @param {Number} [y] the y component of the vector
* @param {Number} [z] the z component of the vector
* @chainable
* @example
* <div class="norender">
* <code>
* function setup() {
* let v = createVector(1, 2, 3);
* v.set(4, 5, 6); // Sets vector to [4, 5, 6]
*
* let v1 = createVector(0, 0, 0);
* let arr = [1, 2, 3];
* v1.set(arr); // Sets vector to [1, 2, 3]
* }
* </code>
* </div>
*
* <div>
* <code>
* let v0, v1;
* function setup() {
* createCanvas(100, 100);
*
* v0 = createVector(0, 0);
* v1 = createVector(50, 50);
* }
*
* function draw() {
* background(240);
*
* drawArrow(v0, v1, 'black');
* v1.set(v1.x + random(-1, 1), v1.y + random(-1, 1));
*
* noStroke();
* text('x: ' + round(v1.x) + ' y: ' + round(v1.y), 20, 90);
* }
*
* // draw an arrow for a vector at a given base position
* function drawArrow(base, vec, myColor) {
* push();
* stroke(myColor);
* strokeWeight(3);
* fill(myColor);
* translate(base.x, base.y);
* line(0, 0, vec.x, vec.y);
* rotate(vec.heading());
* let arrowSize = 7;
* translate(vec.mag() - arrowSize, 0);
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
* pop();
* }
* </code>
* </div>
*/
/**
* @method set
* @param {p5.Vector|Number[]} value the vector to set
* @chainable
*/
public set(x: ParamsType = 0, y: number = 0, z: number = 0): Vector {
if (x instanceof Vector) {
this.x = x.x;
this.y = x.y;
this.z = x.z;
return this;
}
if (x instanceof Array) {
this.x = x[0];
this.y = x[1];
this.z = x[2];
return this;
}
this.x = x;
this.y = y;
this.z = z;
return this;
}
/**
* Gets a copy of the vector, returns a <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a> object.
*
* @method copy
* @return {Vector} the copy of the <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a> object
* @example
* <div class="norender">
* <code>
* let v1 = createVector(1, 2, 3);
* let v2 = v1.copy();
* print(v1.x === v2.x && v1.y === v2.y && v1.z === v2.z);
* // Prints "true"
* </code>
* </div>
*/
public copy(): Vector {
return new Vector(this.x, this.y, this.z);
}
/**
* Adds x, y, and z components to a vector, adds one vector to another, or
* adds two independent vectors together. The version of the method that adds
* two vectors together is a static method and returns a <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>, the others
* acts directly on the vector. Additionally, you may provide arguments to this function as an array.
* See the examples for more context.
*
* @method add
* @param {Number} x the x component of the vector to be added
* @param {Number} [y] the y component of the vector to be added
* @param {Number} [z] the z component of the vector to be added
* @chainable
* @example
* <div class="norender">
* <code>
* let v = createVector(1, 2, 3);
* v.add(4, 5, 6);
* // v's components are set to [5, 7, 9]
* </code>
* </div>
*
* <div class="norender">
* <code>
* let v = createVector(1, 2, 3);
* // Provide arguments as an array
* let arr = [4, 5, 6];
* v.add(arr);
* // v's components are set to [5, 7, 9]
* </code>
* </div>
*
* <div class="norender">
* <code>
* // Static method
* let v1 = createVector(1, 2, 3);
* let v2 = createVector(2, 3, 4);
*
* let v3 = p5.Vector.add(v1, v2);
* // v3 has components [3, 5, 7]
* print(v3);
* </code>
* </div>
*
* <div>
* <code>
* // red vector + blue vector = purple vector
* function draw() {
* background(240);
*
* let v0 = createVector(0, 0);
* let v1 = createVector(mouseX, mouseY);
* drawArrow(v0, v1, 'red');
*
* let v2 = createVector(-30, 20);
* drawArrow(v1, v2, 'blue');
*
* let v3 = p5.Vector.add(v1, v2);
* drawArrow(v0, v3, 'purple');
* }
*
* // draw an arrow for a vector at a given base position
* function drawArrow(base, vec, myColor) {
* push();
* stroke(myColor);
* strokeWeight(3);
* fill(myColor);
* translate(base.x, base.y);
* line(0, 0, vec.x, vec.y);
* rotate(vec.heading());
* let arrowSize = 7;
* translate(vec.mag() - arrowSize, 0);
* triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
* pop();
* }
* </code>
* </div>
*/
/**
* @method add
* @param {p5.Vector|Number[]} value the vector to add
* @chainable
*/
public add(x: ParamsType = 0 , y: number = 0, z: number = 0): Vector {
if (x instanceof Vector) {
this.x += x.x;
this.y += x.y;
this.z += x.z;
return this;
}
if (x instanceof Array) {
this.x += x[0];
this.y += x[1];
this.z += x[2];
return this;
}
this.x += x;
this.y += y;
this.z += z;
return this;
}
/**
* Gives remainder of a vector when it is divided by another vector.
* See examples for more context.
*
* @method rem
* @param {Number} x the x component of divisor vector
* @param {Number} y the y component of divisor vector
* @param {Number} z the z component of divisor vector
* @chainable
* @example
* <div class='norender'>
* <code>
* let v = createVector(3, 4, 5);
* v.rem(2, 3, 4);
* // v's components are set to [1, 1, 1]
* </code>
* </div>
* <div class="norender">
* <code>
* // Static method
* let v1 = createVector(3, 4, 5);
* let v2 = createVector(2, 3, 4);
*
* let v3 = p5.Vector.rem(v1, v2);
* // v3 has components [1, 1, 1]
* print(v3);
* </code>
* </div>
*/
/**
* @method rem
* @param {p5.Vector | Number[]} value divisor vector
* @chainable
*/
public rem(x: ParamsType, y: number = 0, z: number = 0): Vector | undefined {
if (x instanceof Vector) {
if (Number.isFinite(x.x) && Number.isFinite(x.y) && Number.isFinite(x.z)) {
const xComponent: number = parseFloat(`${x.x}`);
const yComponent: number = parseFloat(`${x.y}`);
const zComponent: number = parseFloat(`${x.z}`);
this.calculateRemainder3D(xComponent, yComponent, zComponent);
}
} else if (x instanceof Array) {
if (x.every(element => Number.isFinite(element))) {
if (x.length === 2) {
this.calculateRemainder2D(x[0], x[1]);
}
if (x.length === 3) {
this.calculateRemainder3D(x[0], x[1], x[2]);
}
}
} else if (arguments.length === 1) {
if (Number.isFinite(arguments[0]) && arguments[0] !== 0) {
this.x = this.x % arguments[0];
this.y = this.y % arguments[0];
this.z = this.z % arguments[0];
return this;
}
} else if (arguments.length === 2) {
const vectorComponents: number[] = [...arguments];
if (vectorComponents.every(element => Number.isFinite(element))) {
if (vectorComponents.length === 2) {
this.calculateRemainder2D(
vectorComponents[0],
vectorComponents[1]
);
}
}
} else if (arguments.length === 3) {
const vectorComponents: number[] = [...arguments];
if (vectorComponents.every(element => Number.isFinite(element))) {
if (vectorComponents.length === 3) {
this.calculateRemainder3D(
vectorComponents[0],
vectorComponents[1],
vectorComponents[2]
);
}
}
}
}
/**
* Subtracts x, y, and z components from a vector, subtracts one vector from
* another, or subtracts two independent vectors. The version of the method
* that subtracts two vectors is a static method and returns a <a href="https://p5js.org/reference/#/p5.Vector">p5.Vector</a>, the
* other acts directly on the vector. Additionally, you may provide arguments to this function as an array.
* See the examples for more context.
*
* @method sub
* @param {Number} x the x component of the vector to subtract
* @param {Number} [y] the y component of the vector to subtract
* @param {Number} [z] the z component of the vector to subtract
* @chainable
* @example
* <div class="norender">
* <code>
* let v = createVector(4, 5, 6);
* v.sub(1, 1, 1);
* // v's components are set to [3, 4, 5]
* </code>
* </div>
*
* <div class="norender">
* <code>
* let v = createVector(4, 5, 6);
* // Provide arguments as an array
* let arr = [1, 1, 1];
* v.sub(arr);
* // v's components are set to [3, 4, 5]
* </code>
* </div>
*
* <div class="norender">
* <code>
* // Static method
* let v1 = createVector(2, 3, 4);
* let v2 = createVector(1, 2, 3);
*
* let v3 = p5.Vector.sub(v1, v2);
* // v3 has components [1, 1, 1]
* print(v3);
* </code>
* </div>
*
* <div>
* <code>
* // red vector - blue vector = purple vector
* function draw() {
* background(240);
*
* let v0 = createVector(0, 0);
* let v1 = createVector(70, 50);
* drawArrow(v0, v1, 'red');
*
* let v2 = createVector(mouseX, mouseY);
* drawArrow(v0, v2, 'blue');
*
* let v3 = p5.Vector.sub(v1, v2);