-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathp5.Vector.js
More file actions
3698 lines (3611 loc) · 102 KB
/
p5.Vector.js
File metadata and controls
3698 lines (3611 loc) · 102 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
/**
* @module Math
*/
import * as constants from '../core/constants';
/// HELPERS FOR REMAINDER METHOD
const calculateRemainder2D = function (xComponent, yComponent) {
if (xComponent !== 0) {
this.x = this.x % xComponent;
}
if (yComponent !== 0) {
this.y = this.y % yComponent;
}
return this;
};
const calculateRemainder3D = function (xComponent, yComponent, zComponent) {
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;
};
class Vector {
// This is how it comes in with createVector()
// This check if the first argument is a function
constructor(...args) {
let values = args; // .map(arg => arg || 0);
if (typeof args[0] === 'function') {
this.isPInst = true;
this._fromRadians = args[0];
this._toRadians = args[1];
values = args.slice(2); // .map(arg => arg || 0);
}
let dimensions = values.length; // TODO: make default 3 if no arguments
if (dimensions === 0) {
this.dimensions = 2;
this._values = [0, 0, 0];
} else {
this.dimensions = dimensions;
this._values = values;
}
// This property is here where duck typing (checking if obj.isVector) needs
// to be used over more standard type checking (obj instanceof Vector). This
// needs to happen where we are building multiple files, such as in p5.webgpu.js,
// where if we `import { Vector }` directly, it will be a separate copy of the
// Vector class from the one imported in the main p5.js bundle.
this.isVector = true;
}
/**
* Gets the values of the N-dimensional vector.
*
* This method returns an array of numbers that represent the vector.
* Each number in the array corresponds to a different component of the vector,
* like its position in different directions (e.g., x, y, z).
*
* @returns {Array<number>} The array of values representing the vector.
*/
get values() {
return this._values;
}
/**
* Sets the values of the vector.
*
* This method allows you to update the entire vector with a new set of values.
* You need to provide an array of numbers, where each number represents a component
* of the vector (e.g., x, y, z). The length of the array should match the number of
* dimensions of the vector. If the array is shorter, the missing components will be
* set to 0. If the array is longer, the extra values will be ignored.
*
* @param {Array<number>} newValues - An array of numbers representing the new values for the vector.
*
*/
set values(newValues) {
let dimensions = newValues.length;
if (dimensions === 0) {
this.dimensions = 2;
this._values = [0, 0, 0];
} else {
this.dimensions = dimensions;
this._values = newValues.slice();
}
}
/**
* Gets the x component of the vector.
*
* This method returns the value of the x component of the vector.
* Think of the x component as the horizontal position or the first number in the vector.
* If the x component is not defined, it will return 0.
*
* @returns {Number} The x component of the vector. Returns 0 if the value is not defined.
*/
get x() {
return this._values[0] || 0;
}
/**
* Retrieves the value at the specified index from the vector.
*
* This method allows you to get the value of a specific component of the vector
* by providing its index. Think of the vector as a list of numbers, where each
* number represents a different direction (like x, y, or z). The index is just
* the position of the number in that list.
*
* For example, if you have a vector with values 10, 20, 30 the index 0 would
* give you the first value 10, index 1 would give you the second value 20,
* and so on.
*
* @param {Number} index - The position of the value you want to get from the vector.
* @returns {Number} The value at the specified position in the vector.
* @throws Will throw an error if the index is out of bounds, meaning if you try to
* get a value from a position that doesn't exist in the vector.
*/
getValue(index) {
if (index < this._values.length) {
return this._values[index];
} else {
p5._friendlyError(
'The index parameter is trying to set a value outside the bounds of the vector',
'p5.Vector.setValue'
);
}
}
/**
* Sets the value at the specified index of the vector.
*
* This method allows you to change a specific component of the vector by providing its index and the new value you want to set.
* Think of the vector as a list of numbers, where each number represents a different direction (like x, y, or z).
* The index is just the position of the number in that list.
*
* For example, if you have a vector with values [0, 20, 30], and you want to change the second value (20) to 50,
* you would use this method with index 1 (since indexes start at 0) and value 50.
*
* @param {Number} index - The position in the vector where you want to set the new value.
* @param {Number} value - The new value you want to set at the specified position.
* @throws Will throw an error if the index is outside the bounds of the vector, meaning if you try to set a value at a position that doesn't exist in the vector.
*/
setValue(index, value) {
if (index < this._values.length) {
this._values[index] = value;
} else {
p5._friendlyError(
'The index parameter is trying to set a value outside the bounds of the vector',
'p5.Vector.setValue'
);
}
}
/**
* Gets the y component of the vector.
*
* This method returns the value of the y component of the vector.
* Think of the y component as the vertical position or the second number in the vector.
* If the y component is not defined, it will return 0.
*
* @returns {Number} The y component of the vector. Returns 0 if the value is not defined.
*/
get y() {
return this._values[1] || 0;
}
/**
* Gets the z component of the vector.
*
* This method returns the value of the z component of the vector.
* Think of the z component as the depth or the third number in the vector.
* If the z component is not defined, it will return 0.
*
* @returns {Number} The z component of the vector. Returns 0 if the value is not defined.
*/
get z() {
return this._values[2] || 0;
}
/**
* Gets the w component of the vector.
*
* This method returns the value of the w component of the vector.
* Think of the w component as the fourth number in the vector.
* If the w component is not defined, it will return 0.
*
* @returns {Number} The w component of the vector. Returns 0 if the value is not defined.
*/
get w() {
return this._values[3] || 0;
}
/**
* Sets the x component of the vector.
*
* This method allows you to change the x value of the vector.
* The x value is the first number in the vector, representing the horizontal position.
* By calling this method, you can update the x value to a new number.
*
* @param {Number} xVal - The new value for the x component.
*/
set x(xVal) {
if (this._values.length > 1) {
this._values[0] = xVal;
}
}
/**
* Sets the y component of the vector.
*
* This method allows you to change the y value of the vector.
* The y value is the second number in the vector, representing the vertical position.
* By calling this method, you can update the y value to a new number.
*
* @param {Number} yVal - The new value for the y component.
*/
set y(yVal) {
if (this._values.length > 1) {
this._values[1] = yVal;
}
}
/**
* Sets the z component of the vector.
*
* This method allows you to change the z value of the vector.
* The z value is the third number in the vector, representing the depth or the third dimension.
* By calling this method, you can update the z value to a new number.
*
* @param {Number} zVal - The new value for the z component.
*/
set z(zVal) {
if (this._values.length > 2) {
this._values[2] = zVal;
}
}
/**
* Sets the w component of the vector.
*
* This method allows you to change the w value of the vector.
* The w value is the fourth number in the vector, representing the fourth dimension.
* By calling this method, you can update the w value to a new number.
*
* @param {Number} wVal - The new value for the w component.
*/
set w(wVal) {
if (this._values.length > 3) {
this._values[3] = wVal;
}
}
/**
* Returns a string representation of a vector.
*
* Calling `toString()` is useful for printing vectors to the console while
* debugging.
*
* @return {String} string representation of the vector.
*
* @example
* // META:norender
* function setup() {
* let v = createVector(20, 30);
*
* // Prints 'vector[20, 30, 0]'.
* print(v.toString());
* }
*/
toString() {
return `vector[${this._values.join(', ')}]`;
}
/**
* Sets the vector's `x`, `y`, and `z` components.
*
* `set()` can use separate numbers, as in `v.set(1, 2, 3)`, a
* <a href="#/p5.Vector">p5.Vector</a> object, as in `v.set(v2)`, or an
* array of numbers, as in `v.set([1, 2, 3])`.
*
* If a value isn't provided for a component, it will be set to 0. For
* example, `v.set(4, 5)` sets `v.x` to 4, `v.y` to 5, and `v.z` to 0.
* Calling `set()` with no arguments, as in `v.set()`, sets all the vector's
* components to 0.
*
* @param {Number} [x] x component of the vector.
* @param {Number} [y] y component of the vector.
* @param {Number} [z] z component of the vector.
* @chainable
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Style the points.
* strokeWeight(5);
*
* // Top left.
* let pos = createVector(25, 25);
* point(pos);
*
* // Top right.
* // set() with numbers.
* pos.set(75, 25);
* point(pos);
*
* // Bottom right.
* // set() with a p5.Vector.
* let p2 = createVector(75, 75);
* pos.set(p2);
* point(pos);
*
* // Bottom left.
* // set() with an array.
* let arr = [25, 75];
* pos.set(arr);
* point(pos);
*
* describe('Four black dots arranged in a square on a gray background.');
* }
*/
/**
* @param {p5.Vector|Number[]} value vector to set.
* @chainable
*/
set(...args) {
if (args[0] instanceof Vector) {
this._values = args[0].values.slice();
} else if (Array.isArray(args[0])) {
this._values = args[0].map(arg => arg || 0);
} else {
this._values = args.map(arg => arg || 0);
}
this.dimensions = this._values.length;
return this;
}
/**
* Returns a copy of the <a href="#/p5.Vector">p5.Vector</a> object.
*
* @return {p5.Vector} copy of the <a href="#/p5.Vector">p5.Vector</a> object.
*
* @example
* function setup() {
* createCanvas(100 ,100);
*
* background(200);
*
* // Create a p5.Vector object.
* let pos = createVector(50, 50);
*
* // Make a copy.
* let pc = pos.copy();
*
* // Draw the point.
* strokeWeight(5);
* point(pc);
*
* describe('A black point drawn in the middle of a gray square.');
* }
*/
copy() {
if (this.isPInst) {
return new Vector(this._fromRadians, this._toRadians, ...this._values);
} else {
return new Vector(...this._values);
}
}
/**
* Adds to a vector's components.
*
* `add()` can use separate numbers, as in `v.add(1, 2, 3)`,
* another <a href="#/p5.Vector">p5.Vector</a> object, as in `v.add(v2)`, or
* an array of numbers, as in `v.add([1, 2, 3])`.
*
* If a value isn't provided for a component, it won't change. For
* example, `v.add(4, 5)` adds 4 to `v.x`, 5 to `v.y`, and 0 to `v.z`.
* Calling `add()` with no arguments, as in `v.add()`, has no effect.
*
* This method supports N-dimensional vectors.
*
* The static version of `add()`, as in `p5.Vector.add(v2, v1)`, returns a new
* <a href="#/p5.Vector">p5.Vector</a> object and doesn't change the
* originals.
*
* @param {Number|Array} x x component of the vector to be added or an array of components.
* @param {Number} [y] y component of the vector to be added.
* @param {Number} [z] z component of the vector to be added.
* @chainable
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Style the points.
* strokeWeight(5);
*
* // Top left.
* let pos = createVector(25, 25);
* point(pos);
*
* // Top right.
* // Add numbers.
* pos.add(50, 0);
* point(pos);
*
* // Bottom right.
* // Add a p5.Vector.
* let p2 = createVector(0, 50);
* pos.add(p2);
* point(pos);
*
* // Bottom left.
* // Add an array.
* let arr = [-50, 0];
* pos.add(arr);
* point(pos);
*
* describe('Four black dots arranged in a square on a gray background.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Top left.
* let p1 = createVector(25, 25);
*
* // Center.
* let p2 = createVector(50, 50);
*
* // Bottom right.
* // Add p1 and p2.
* let p3 = p5.Vector.add(p1, p2);
*
* // Draw the points.
* strokeWeight(5);
* point(p1);
* point(p2);
* point(p3);
*
* describe('Three black dots in a diagonal line from top left to bottom right.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* describe('Three arrows drawn on a gray square. A red arrow extends from the top left corner to the center. A blue arrow extends from the tip of the red arrow. A purple arrow extends from the origin to the tip of the blue arrow.');
* }
*
* function draw() {
* background(200);
*
* let origin = createVector(0, 0);
*
* // Draw the red arrow.
* let v1 = createVector(50, 50);
* drawArrow(origin, v1, 'red');
*
* // Draw the blue arrow.
* let v2 = createVector(-30, 20);
* drawArrow(v1, v2, 'blue');
*
* // Purple arrow.
* let v3 = p5.Vector.add(v1, v2);
* drawArrow(origin, v3, 'purple');
* }
*
* // Draws an arrow between two vectors.
* 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();
* }
*/
/**
* @param {p5.Vector|Number[]} value The vector to add
* @chainable
*/
add(...args) {
if (args[0] instanceof Vector) {
args = args[0].values;
} else if (Array.isArray(args[0])) {
args = args[0];
}
args.forEach((value, index) => {
this._values[index] = (this._values[index] || 0) + (value || 0);
});
return this;
}
/**
* Performs modulo (remainder) division with a vector's `x`, `y`, and `z`
* components.
*
* `rem()` can use separate numbers, as in `v.rem(1, 2, 3)`,
* another <a href="#/p5.Vector">p5.Vector</a> object, as in `v.rem(v2)`, or
* an array of numbers, as in `v.rem([1, 2, 3])`.
*
* If only one value is provided, as in `v.rem(2)`, then all the components
* will be set to their values modulo 2. If two values are provided, as in
* `v.rem(2, 3)`, then `v.z` won't change. Calling `rem()` with no
* arguments, as in `v.rem()`, has no effect.
*
* The static version of `rem()`, as in `p5.Vector.rem(v2, v1)`, returns a
* new <a href="#/p5.Vector">p5.Vector</a> object and doesn't change the
* originals.
*
* @param {Number} x x component of divisor vector.
* @param {Number} y y component of divisor vector.
* @param {Number} z z component of divisor vector.
* @chainable
*
* @example
* // META:norender
* function setup() {
* // Create a p5.Vector object.
* let v = createVector(3, 4, 5);
*
* // Divide numbers.
* v.rem(2);
*
* // Prints 'p5.Vector Object : [1, 0, 1]'.
* print(v.toString());
* }
*
* @example
* // META:norender
* function setup() {
* // Create a p5.Vector object.
* let v = createVector(3, 4, 5);
*
* // Divide numbers.
* v.rem(2, 3);
*
* // Prints 'p5.Vector Object : [1, 1, 5]'.
* print(v.toString());
* }
*
* @example
* // META:norender
* function setup() {
* // Create a p5.Vector object.
* let v = createVector(3, 4, 5);
*
* // Divide numbers.
* v.rem(2, 3, 4);
*
* // Prints 'p5.Vector Object : [1, 1, 1]'.
* print(v.toString());
* }
*
* @example
* // META:norender
* function setup() {
* // Create p5.Vector objects.
* let v1 = createVector(3, 4, 5);
* let v2 = createVector(2, 3, 4);
*
* // Divide a p5.Vector.
* v1.rem(v2);
*
* // Prints 'p5.Vector Object : [1, 1, 1]'.
* print(v1.toString());
* }
*
* @example
* // META:norender
* function setup() {
* // Create a p5.Vector object.
* let v = createVector(3, 4, 5);
*
* // Divide an array.
* let arr = [2, 3, 4];
* v.rem(arr);
*
* // Prints 'p5.Vector Object : [1, 1, 1]'.
* print(v.toString());
* }
*
* @example
* // META:norender
* function setup() {
* // Create p5.Vector objects.
* let v1 = createVector(3, 4, 5);
* let v2 = createVector(2, 3, 4);
*
* // Divide without modifying the original vectors.
* let v3 = p5.Vector.rem(v1, v2);
*
* // Prints 'p5.Vector Object : [1, 1, 1]'.
* print(v3.toString());
* }
*/
/**
* @param {p5.Vector | Number[]} value divisor vector.
* @chainable
*/
rem(x, y, z) {
if (x instanceof Vector) {
if ([x.x, x.y, x.z].every(Number.isFinite)) {
const xComponent = parseFloat(x.x);
const yComponent = parseFloat(x.y);
const zComponent = parseFloat(x.z);
return calculateRemainder3D.call(
this,
xComponent,
yComponent,
zComponent
);
}
} else if (Array.isArray(x)) {
if (x.every(element => Number.isFinite(element))) {
if (x.length === 2) {
return calculateRemainder2D.call(this, x[0], x[1]);
}
if (x.length === 3) {
return calculateRemainder3D.call(this, 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 = [...arguments];
if (vectorComponents.every(element => Number.isFinite(element))) {
if (vectorComponents.length === 2) {
return calculateRemainder2D.call(
this,
vectorComponents[0],
vectorComponents[1]
);
}
}
} else if (arguments.length === 3) {
const vectorComponents = [...arguments];
if (vectorComponents.every(element => Number.isFinite(element))) {
if (vectorComponents.length === 3) {
return calculateRemainder3D.call(
this,
vectorComponents[0],
vectorComponents[1],
vectorComponents[2]
);
}
}
}
}
/**
* Subtracts from a vector's `x`, `y`, and `z` components.
*
* `sub()` can use separate numbers, as in `v.sub(1, 2, 3)`, another
* <a href="#/p5.Vector">p5.Vector</a> object, as in `v.sub(v2)`, or an array
* of numbers, as in `v.sub([1, 2, 3])`.
*
* If a value isn't provided for a component, it won't change. For
* example, `v.sub(4, 5)` subtracts 4 from `v.x`, 5 from `v.y`, and 0 from `v.z`.
* Calling `sub()` with no arguments, as in `v.sub()`, has no effect.
*
* The static version of `sub()`, as in `p5.Vector.sub(v2, v1)`, returns a new
* <a href="#/p5.Vector">p5.Vector</a> object and doesn't change the
* originals.
*
* @param {Number} x x component of the vector to subtract.
* @param {Number} [y] y component of the vector to subtract.
* @param {Number} [z] z component of the vector to subtract.
* @chainable
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Style the points.
* strokeWeight(5);
*
* // Bottom right.
* let pos = createVector(75, 75);
* point(pos);
*
* // Top right.
* // Subtract numbers.
* pos.sub(0, 50);
* point(pos);
*
* // Top left.
* // Subtract a p5.Vector.
* let p2 = createVector(50, 0);
* pos.sub(p2);
* point(pos);
*
* // Bottom left.
* // Subtract an array.
* let arr = [0, -50];
* pos.sub(arr);
* point(pos);
*
* describe('Four black dots arranged in a square on a gray background.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Create p5.Vector objects.
* let p1 = createVector(75, 75);
* let p2 = createVector(50, 50);
*
* // Subtract with modifying the original vectors.
* let p3 = p5.Vector.sub(p1, p2);
*
* // Draw the points.
* strokeWeight(5);
* point(p1);
* point(p2);
* point(p3);
*
* describe('Three black dots in a diagonal line from top left to bottom right.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* describe('Three arrows drawn on a gray square. A red and a blue arrow extend from the top left. A purple arrow extends from the tip of the red arrow to the tip of the blue arrow.');
* }
*
* function draw() {
* background(200);
*
* let origin = createVector(0, 0);
*
* // Draw the red arrow.
* let v1 = createVector(50, 50);
* drawArrow(origin, v1, 'red');
*
* // Draw the blue arrow.
* let v2 = createVector(20, 70);
* drawArrow(origin, v2, 'blue');
*
* // Purple arrow.
* let v3 = p5.Vector.sub(v2, v1);
* drawArrow(v1, v3, 'purple');
* }
*
* // Draws an arrow between two vectors.
* 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();
* }
*/
/**
* @param {p5.Vector|Number[]} value the vector to subtract
* @chainable
*/
sub(...args) {
if (args[0] instanceof Vector) {
args[0].values.forEach((value, index) => {
this._values[index] -= value || 0;
});
} else if (Array.isArray(args[0])) {
args[0].forEach((value, index) => {
this._values[index] -= value || 0;
});
} else {
args.forEach((value, index) => {
this._values[index] -= value || 0;
});
}
return this;
}
/**
* Multiplies a vector's `x`, `y`, and `z` components.
*
* `mult()` can use separate numbers, as in `v.mult(1, 2, 3)`, another
* <a href="#/p5.Vector">p5.Vector</a> object, as in `v.mult(v2)`, or an array
* of numbers, as in `v.mult([1, 2, 3])`.
*
* If only one value is provided, as in `v.mult(2)`, then all the components
* will be multiplied by 2. If a value isn't provided for a component, it
* won't change. For example, `v.mult(4, 5)` multiplies `v.x` by, `v.y` by 5,
* and `v.z` by 1. Calling `mult()` with no arguments, as in `v.mult()`, has
* no effect.
*
* The static version of `mult()`, as in `p5.Vector.mult(v, 2)`, returns a new
* <a href="#/p5.Vector">p5.Vector</a> object and doesn't change the
* originals.
*
* @param {Number} n The number to multiply with the vector
* @chainable
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Style the points.
* strokeWeight(5);
*
* // Top-left.
* let p = createVector(25, 25);
* point(p);
*
* // Center.
* // Multiply all components by 2.
* p.mult(2);
* point(p);
*
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the center.');
* }
*
* @example
* function setup() {
* strokeWeight(5);
*
* // Top-left.
* let p = createVector(25, 25);
* point(p);
*
* // Bottom-right.
* // Multiply p.x * 2 and p.y * 3
* p.mult(2, 3);
* point(p);
*
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Style the points.
* strokeWeight(5);
*
* // Top-left.
* let p = createVector(25, 25);
* point(p);
*
* // Bottom-right.
* // Multiply p.x * 2 and p.y * 3
* let arr = [2, 3];
* p.mult(arr);
* point(p);
*
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Style the points.
* strokeWeight(5);
*
* // Top-left.
* let p = createVector(25, 25);
* point(p);
*
* // Bottom-right.
* // Multiply p.x * p2.x and p.y * p2.y
* let p2 = createVector(2, 3);
* p.mult(p2);
* point(p);
*
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Style the points.
* strokeWeight(5);
*
* // Top-left.
* let p = createVector(25, 25);
* point(p);
*
* // Bottom-right.
* // Create a new p5.Vector with
* // p3.x = p.x * p2.x
* // p3.y = p.y * p2.y
* let p2 = createVector(2, 3);
* let p3 = p5.Vector.mult(p, p2);
* point(p3);
*
* describe('Two black dots drawn on a gray square. One dot is in the top left corner and the other is in the bottom center.');
* }
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* describe('Two arrows extending from the top left corner. The blue arrow is twice the length of the red arrow.');
* }
*
* function draw() {
* background(200);
*
* let origin = createVector(0, 0);
*
* // Draw the red arrow.
* let v1 = createVector(25, 25);
* drawArrow(origin, v1, 'red');
*
* // Draw the blue arrow.
* let v2 = p5.Vector.mult(v1, 2);
* drawArrow(origin, v2, 'blue');
* }
*
* // Draws an arrow between two vectors.
* 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();
* }
*/
/**
* @param {Number} x number to multiply with the x component of the vector.
* @param {Number} y number to multiply with the y component of the vector.
* @param {Number} [z] number to multiply with the z component of the vector.
* @chainable
*/
/**
* @param {Number[]} arr array to multiply with the components of the vector.
* @chainable
*/
/**
* @param {p5.Vector} v vector to multiply with the components of the original vector.
* @chainable
*/
mult(...args) {
if (args.length === 1 && args[0] instanceof Vector) {
const v = args[0];
const maxLen = Math.min(this._values.length, v.values.length);
for (let i = 0; i < maxLen; i++) {
if (Number.isFinite(v.values[i]) && typeof v.values[i] === 'number') {
this._values[i] *= v.values[i];
} else {
console.warn(
'p5.Vector.prototype.mult:',
'v contains components that are either undefined or not finite numbers'
);
return this;
}
}