forked from processing/p5.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp5.Vector.js
2382 lines (2309 loc) · 59.7 KB
/
p5.Vector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
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
* @submodule Vector
* @requires constants
*/
import p5 from '../core/main';
import * as constants from '../core/constants';
/**
* 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="#/p5.Vector/mag">mag()</a> and <a href="#/p5.Vector/heading">heading()</a>.
*
* In many of the p5.js examples, you will see <a href="#/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="#/p5.Vector">p5.Vector</a> class.
*
* @class p5.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);
*
* describe(`2 white ellipses. One center-left the other
* bottom right and off canvas`);
* </code>
* </div>
*/
p5.Vector = function Vector() {
let x, y, z;
// This is how it comes in with createVector()
// This check if the first argument is a function
if ({}.toString.call(arguments[0]) === '[object Function]') {
// In this case the vector have an associated p5 instance
this.isPInst = true;
this._fromRadians = arguments[0];
this._toRadians = arguments[1];
x = arguments[2] || 0;
y = arguments[3] || 0;
z = arguments[4] || 0;
// This is what we'll get with new p5.Vector()
} else {
x = arguments[0] || 0;
y = arguments[1] || 0;
z = arguments[2] || 0;
}
/**
* The x component of the vector
* @property x {Number}
*/
this.x = x;
/**
* The y component of the vector
* @property y {Number}
*/
this.y = y;
/**
* The z component of the vector
* @property z {Number}
*/
this.z = z;
};
/**
* 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>
*/
p5.Vector.prototype.toString = function p5VectorToString() {
return `p5.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="#/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
*/
p5.Vector.prototype.set = function set(x, y, z) {
if (x instanceof p5.Vector) {
this.x = x.x || 0;
this.y = x.y || 0;
this.z = x.z || 0;
return this;
}
if (x instanceof Array) {
this.x = x[0] || 0;
this.y = x[1] || 0;
this.z = x[2] || 0;
return this;
}
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
return this;
};
/**
* Gets a copy of the vector, returns a <a href="#/p5.Vector">p5.Vector</a> object.
*
* @method copy
* @return {p5.Vector} the copy of the <a href="#/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>
*/
p5.Vector.prototype.copy = function copy() {
if (this.isPInst) {
return new p5.Vector(
this._fromRadians,
this._toRadians,
this.x,
this.y,
this.z
);
} else {
return new p5.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="#/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
*/
p5.Vector.prototype.add = function add(x, y, z) {
if (x instanceof p5.Vector) {
this.x += x.x || 0;
this.y += x.y || 0;
this.z += x.z || 0;
return this;
}
if (x instanceof Array) {
this.x += x[0] || 0;
this.y += x[1] || 0;
this.z += x[2] || 0;
return this;
}
this.x += x || 0;
this.y += y || 0;
this.z += z || 0;
return this;
};
/// 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;
};
/**
* 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
*/
p5.Vector.prototype.rem = function rem(x, y, z) {
if (x instanceof p5.Vector) {
if (Number.isFinite(x.x) && Number.isFinite(x.y) && Number.isFinite(x.z)) {
const xComponent = parseFloat(x.x);
const yComponent = parseFloat(x.y);
const zComponent = parseFloat(x.z);
return calculateRemainder3D.call(
this,
xComponent,
yComponent,
zComponent
);
}
} else if (x instanceof Array) {
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 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="#/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);
* drawArrow(v2, 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 sub
* @param {p5.Vector|Number[]} value the vector to subtract
* @chainable
*/
p5.Vector.prototype.sub = function sub(x, y, z) {
if (x instanceof p5.Vector) {
this.x -= x.x || 0;
this.y -= x.y || 0;
this.z -= x.z || 0;
return this;
}
if (x instanceof Array) {
this.x -= x[0] || 0;
this.y -= x[1] || 0;
this.z -= x[2] || 0;
return this;
}
this.x -= x || 0;
this.y -= y || 0;
this.z -= z || 0;
return this;
};
/**
* Multiplies the vector by a scalar, multiplies the x, y, and z components from a vector, or multiplies
* the x, y, and z components of two independent vectors. When multiplying a vector by a scalar, the x, y,
* and z components of the vector are all multiplied by the scalar. When multiplying a vector by a vector,
* the x, y, z components of both vectors are multiplied by each other
* (for example, with two vectors a and b: a.x * b.x, a.y * b.y, a.z * b.z). The static version of this method
* creates a new <a href="#/p5.Vector">p5.Vector</a> while the non static version acts on the vector
* directly. Additionally, you may provide arguments to this function as an array.
* See the examples for more context.
*
* @method mult
* @param {Number} n The number to multiply with the vector
* @chainable
* @example
* <div class="norender">
* <code>
* let v = createVector(1, 2, 3);
* v.mult(2);
* // v's components are set to [2, 4, 6]
* </code>
* </div>
*
* <div class="norender">
* <code>
* let v0 = createVector(1, 2, 3);
* let v1 = createVector(2, 3, 4);
* v0.mult(v1); // v0's components are set to [2, 6, 12]
* </code>
* </div>
*
* <div class="norender">
* <code>
* let v0 = createVector(1, 2, 3);
* // Provide arguments as an array
* let arr = [2, 3, 4];
* v0.mult(arr); // v0's components are set to [2, 6, 12]
* </code>
* </div>
*
* <div class="norender">
* <code>
* let v0 = createVector(1, 2, 3);
* let v1 = createVector(2, 3, 4);
* const result = p5.Vector.mult(v0, v1);
* print(result); // result's components are set to [2, 6, 12]
* </code>
* </div>
*
* <div class="norender">
* <code>
* // Static method
* let v1 = createVector(1, 2, 3);
* let v2 = p5.Vector.mult(v1, 2);
* // v2 has components [2, 4, 6]
* print(v2);
* </code>
* </div>
*
* <div>
* <code>
* function draw() {
* background(240);
*
* let v0 = createVector(50, 50);
* let v1 = createVector(25, -25);
* drawArrow(v0, v1, 'red');
*
* let num = map(mouseX, 0, width, -2, 2, true);
* let v2 = p5.Vector.mult(v1, num);
* drawArrow(v0, v2, 'blue');
*
* noStroke();
* text('multiplied by ' + num.toFixed(2), 5, 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 mult
* @param {Number} x The number to multiply with the x component of the vector
* @param {Number} y The number to multiply with the y component of the vector
* @param {Number} [z] The number to multiply with the z component of the vector
* @chainable
*/
/**
* @method mult
* @param {Number[]} arr The array to multiply with the components of the vector
* @chainable
*/
/**
* @method mult
* @param {p5.Vector} v The vector to multiply with the components of the original vector
* @chainable
*/
p5.Vector.prototype.mult = function mult(x, y, z) {
if (x instanceof p5.Vector) {
// new p5.Vector will check that values are valid upon construction but it's possible
// that someone could change the value of a component after creation, which is why we still
// perform this check
if (
Number.isFinite(x.x) &&
Number.isFinite(x.y) &&
Number.isFinite(x.z) &&
typeof x.x === 'number' &&
typeof x.y === 'number' &&
typeof x.z === 'number'
) {
this.x *= x.x;
this.y *= x.y;
this.z *= x.z;
} else {
console.warn(
'p5.Vector.prototype.mult:',
'x contains components that are either undefined or not finite numbers'
);
}
return this;
}
if (x instanceof Array) {
if (
x.every(element => Number.isFinite(element)) &&
x.every(element => typeof element === 'number')
) {
if (x.length === 1) {
this.x *= x[0];
this.y *= x[0];
this.z *= x[0];
} else if (x.length === 2) {
this.x *= x[0];
this.y *= x[1];
} else if (x.length === 3) {
this.x *= x[0];
this.y *= x[1];
this.z *= x[2];
}
} else {
console.warn(
'p5.Vector.prototype.mult:',
'x contains elements that are either undefined or not finite numbers'
);
}
return this;
}
const vectorComponents = [...arguments];
if (
vectorComponents.every(element => Number.isFinite(element)) &&
vectorComponents.every(element => typeof element === 'number')
) {
if (arguments.length === 1) {
this.x *= x;
this.y *= x;
this.z *= x;
}
if (arguments.length === 2) {
this.x *= x;
this.y *= y;
}
if (arguments.length === 3) {
this.x *= x;
this.y *= y;
this.z *= z;
}
} else {
console.warn(
'p5.Vector.prototype.mult:',
'x, y, or z arguments are either undefined or not a finite number'
);
}
return this;
};
/**
* Divides the vector by a scalar, divides a vector by the x, y, and z arguments, or divides the x, y, and
* z components of two vectors against each other. When dividing a vector by a scalar, the x, y,
* and z components of the vector are all divided by the scalar. When dividing a vector by a vector,
* the x, y, z components of the source vector are treated as the dividend, and the x, y, z components
* of the argument is treated as the divisor (for example with two vectors a and b: a.x / b.x, a.y / b.y, a.z / b.z).
* The static version of this method creates a
* new <a href="#/p5.Vector">p5.Vector</a> while the non static version acts on the vector directly.
* Additionally, you may provide arguments to this function as an array.
* See the examples for more context.
*
* @method div
* @param {number} n The number to divide the vector by
* @chainable
* @example
* <div class="norender">
* <code>
* let v = createVector(6, 4, 2);
* v.div(2); //v's components are set to [3, 2, 1]
* </code>
* </div>
*
* <div class="norender">
* <code>
* let v0 = createVector(9, 4, 2);
* let v1 = createVector(3, 2, 4);
* v0.div(v1); // v0's components are set to [3, 2, 0.5]
* </code>
* </div>
*
* <div class="norender">
* <code>
* let v0 = createVector(9, 4, 2);
* // Provide arguments as an array
* let arr = [3, 2, 4];
* v0.div(arr); // v0's components are set to [3, 2, 0.5]
* </code>
* </div>
*
* <div class="norender">
* <code>
* let v0 = createVector(9, 4, 2);
* let v1 = createVector(3, 2, 4);
* let result = p5.Vector.div(v0, v1);
* print(result); // result's components are set to [3, 2, 0.5]
* </code>
* </div>
*
* <div class="norender">
* <code>
* // Static method
* let v1 = createVector(6, 4, 2);
* let v2 = p5.Vector.div(v1, 2);
* // v2 has components [3, 2, 1]
* print(v2);
* </code>
* </div>
*
* <div>
* <code>
* function draw() {
* background(240);
*
* let v0 = createVector(0, 100);
* let v1 = createVector(50, -50);
* drawArrow(v0, v1, 'red');
*
* let num = map(mouseX, 0, width, 10, 0.5, true);
* let v2 = p5.Vector.div(v1, num);
* drawArrow(v0, v2, 'blue');
*
* noStroke();
* text('divided by ' + num.toFixed(2), 10, 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 div
* @param {Number} x The number to divide with the x component of the vector
* @param {Number} y The number to divide with the y component of the vector
* @param {Number} [z] The number to divide with the z component of the vector
* @chainable
*/
/**
* @method div
* @param {Number[]} arr The array to divide the components of the vector by
* @chainable
*/
/**
* @method div
* @param {p5.Vector} v The vector to divide the components of the original vector by
* @chainable
*/
p5.Vector.prototype.div = function div(x, y, z) {
if (x instanceof p5.Vector) {
// new p5.Vector will check that values are valid upon construction but it's possible
// that someone could change the value of a component after creation, which is why we still
// perform this check
if (
Number.isFinite(x.x) &&
Number.isFinite(x.y) &&
Number.isFinite(x.z) &&
typeof x.x === 'number' &&
typeof x.y === 'number' &&
typeof x.z === 'number'
) {
if (x.x === 0 || x.y === 0 || x.z === 0) {
console.warn('p5.Vector.prototype.div:', 'divide by 0');
return this;
}
this.x /= x.x;
this.y /= x.y;
this.z /= x.z;
} else {
console.warn(
'p5.Vector.prototype.div:',
'x contains components that are either undefined or not finite numbers'
);
}
return this;
}
if (x instanceof Array) {
if (
x.every(element => Number.isFinite(element)) &&
x.every(element => typeof element === 'number')
) {
if (x.some(element => element === 0)) {
console.warn('p5.Vector.prototype.div:', 'divide by 0');
return this;
}
if (x.length === 1) {
this.x /= x[0];
this.y /= x[0];
this.z /= x[0];
} else if (x.length === 2) {
this.x /= x[0];
this.y /= x[1];
} else if (x.length === 3) {
this.x /= x[0];
this.y /= x[1];
this.z /= x[2];
}
} else {
console.warn(
'p5.Vector.prototype.div:',
'x contains components that are either undefined or not finite numbers'
);
}
return this;
}
const vectorComponents = [...arguments];
if (
vectorComponents.every(element => Number.isFinite(element)) &&
vectorComponents.every(element => typeof element === 'number')
) {
if (vectorComponents.some(element => element === 0)) {
console.warn('p5.Vector.prototype.div:', 'divide by 0');
return this;
}
if (arguments.length === 1) {
this.x /= x;
this.y /= x;
this.z /= x;
}
if (arguments.length === 2) {
this.x /= x;
this.y /= y;
}
if (arguments.length === 3) {
this.x /= x;
this.y /= y;
this.z /= z;
}
} else {
console.warn(
'p5.Vector.prototype.div:',
'x, y, or z arguments are either undefined or not a finite number'
);
}
return this;
};
/**
* 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
* @return {Number} magnitude of the vector
* @example
* <div>
* <code>
* function draw() {
* background(240);
*
* let v0 = createVector(0, 0);
* let v1 = createVector(mouseX, mouseY);
* drawArrow(v0, v1, 'black');
*
* noStroke();
* text('vector length: ' + v1.mag().toFixed(2), 10, 70, 90, 30);
* }
*
* // 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>
* <div class="norender">
* <code>
* let v = createVector(20.0, 30.0, 40.0);
* let m = v.mag();
* print(m); // Prints "53.85164807134504"
* </code>