@@ -9,8 +9,14 @@ import * as constants from '../core/constants';
99 * This function is used by binary vector operations to prioritize shorter vectors,
1010 * and to emit a warning when lengths do not match.
1111 */
12- const prioritizeSmallerDimension = function ( currentVectorDimension , args ) {
13- return Math . min ( currentVectorDimension , args . length ) ;
12+ const prioritizeSmallerDimension = function ( currentVectorDimension , args ) {
13+ const resultDimension = Math . min ( currentVectorDimension , args . length ) ;
14+ if ( Array . isArray ( args ) && currentVectorDimension !== args . length ) {
15+ console . warn (
16+ 'When working with two vectors of different sizes, the smaller dimension is used. In this operation, both vector will be treated as ' + resultDimension + 'D vectors, and any additional values of the linger vector will be ignored.'
17+ ) ;
18+ }
19+ return resultDimension ;
1420} ;
1521
1622/**
@@ -399,20 +405,17 @@ class Vector {
399405 }
400406 }
401407
402-
403-
404408 /**
405409 * Adds to a vector's components.
406410 *
407411 * `add()` can use separate numbers, as in `v.add(1, 2, 3)`,
408412 * another <a href="#/p5.Vector">p5.Vector</a> object, as in `v.add(v2)`, or
409413 * an array of numbers, as in `v.add([1, 2, 3])`.
410414 *
411- * Add vectors only when they are the same size: both 2-dimensional, or
412- * both 3-dimensional. When two vectors of different sizes are added, the
413- * smaller dimension will be used, any additional values of the longer
414- * vector will be ignored.
415- * For example, adding `[1, 2, 3]` and `[4, 5]` will result in `[5, 7]`.
415+ * You should add vectors only when they are the same size. When two vectors
416+ * of different sizes are added, the smaller dimension will be used, any
417+ * additional values of the longer vector will be ignored. For example,
418+ * adding `[1, 2, 3]` and `[4, 5]` will result in `[5, 7]`.
416419 *
417420 * Calling `add()` with no arguments, as in `v.add()`, has no effect.
418421 *
@@ -529,7 +532,7 @@ class Vector {
529532 * @param {p5.Vector|Number[] } value The vector to add
530533 * @chainable
531534 */
532- add ( ... args ) {
535+ add ( args ) {
533536 const minDimension = prioritizeSmallerDimension ( this . dimensions , args ) ;
534537 shrinkToDimension ( this . values , minDimension ) ;
535538
@@ -540,11 +543,8 @@ class Vector {
540543 return this ;
541544 }
542545
543-
544-
545546 /**
546- * Performs modulo (remainder) division with a vector's `x`, `y`, and `z`
547- * components.
547+ * Performs modulo (remainder) division with a vector's components.
548548 *
549549 * `rem()` can use separate numbers, as in `v.rem(1, 2, 3)`,
550550 * another <a href="#/p5.Vector">p5.Vector</a> object, as in `v.rem(v2)`, or
@@ -554,8 +554,8 @@ class Vector {
554554 * will be set to their values modulo 2. Calling `rem()` with no
555555 * arguments, as in `v.rem()`, has no effect.
556556 *
557- * Modulo vectors only when they are the same size: both 2D, or both 3D.
558- * When two vectors of different sizes are used, the smaller dimension will be
557+ * You should modulo vectors only when they are the same size. When two
558+ * vectors of different sizes are used, the smaller dimension will be
559559 * used, any additional values of the longer vector will be ignored.
560560 * For example, taking `[3, 6, 9]` modulo `[2, 4]` will result in `[1, 2]`.
561561 *
@@ -653,30 +653,37 @@ class Vector {
653653 * @param {p5.Vector | Number[] } value divisor vector.
654654 * @chainable
655655 */
656- rem ( ... args ) {
656+ rem ( args ) {
657657 const minDimension = prioritizeSmallerDimension ( this . dimensions , args ) ;
658658
659659 shrinkToDimension ( this . values , minDimension ) ;
660- for ( let i = 0 ; i < this . values . length ; i ++ ) {
661- if ( args [ i ] > 0 ) {
662- this . values [ i ] = this . values [ i ] % args [ i ] ;
660+
661+ if ( Array . isArray ( args ) ) {
662+ for ( let i = 0 ; i < this . values . length ; i ++ ) {
663+ if ( args [ i ] > 0 ) {
664+ this . values [ i ] = this . values [ i ] % args [ i ] ;
665+ }
666+ }
667+ } else if ( args > 0 ) {
668+ for ( let i = 0 ; i < this . values . length ; i ++ ) {
669+ this . values [ i ] = this . values [ i ] % args ;
663670 }
664671 }
665672
666673 return this ;
667674 }
668675
669676 /**
670- * Subtracts from a vector's `x`, `y`, and `z` components.
677+ * Subtracts from a vector's components.
671678 *
672679 * `sub()` can use separate numbers, as in `v.sub(1, 2, 3)`, another
673680 * <a href="#/p5.Vector">p5.Vector</a> object, as in `v.sub(v2)`, or an array
674681 * of numbers, as in `v.sub([1, 2, 3])`.
675682 *
676683 * Calling `sub()` with no arguments, as in `v.sub()`, has no effect.
677684 *
678- * Subtract vectors only when they are the same size: both 2D, or both 3D.
679- * When two vectors of different sizes are used, the smaller dimension will be
685+ * You should subtract vectors only when they are the same size. When two
686+ * vectors of different sizes are used, the smaller dimension will be
680687 * used, any additional values of the longer vector will be ignored.
681688 * For example, subtracting `[1, 2]` from `[3, 5, 7]` will result in `[2, 3]`.
682689 *
@@ -788,7 +795,7 @@ class Vector {
788795 * @param {p5.Vector|Number[] } value the vector to subtract
789796 * @chainable
790797 */
791- sub ( ... args ) {
798+ sub ( args ) {
792799 const minDimension = prioritizeSmallerDimension ( this . dimensions , args ) ;
793800 shrinkToDimension ( this . values , minDimension ) ;
794801
@@ -800,7 +807,7 @@ class Vector {
800807 }
801808
802809 /**
803- * Multiplies a vector's `x`, `y`, and `z` components.
810+ * Multiplies a vector's components.
804811 *
805812 * `mult()` can use separate numbers, as in `v.mult(1, 2, 3)`, another
806813 * <a href="#/p5.Vector">p5.Vector</a> object, as in `v.mult(v2)`, or an array
@@ -810,8 +817,8 @@ class Vector {
810817 * will be multiplied by 2. Calling `mult()` with no arguments, as in `v.mult()`, has
811818 * no effect.
812819 *
813- * Multiply vectors only when they are the same size: both 2D, or both 3D.
814- * When two vectors of different sizes are multiplied, the smaller dimension will be
820+ * You should multiply vectors only when they are the same size. When two
821+ * vectors of different sizes are multiplied, the smaller dimension will be
815822 * used, any additional values of the longer vector will be ignored.
816823 * For example, multiplying `[1, 2, 3]` by `[4, 5]` will result in `[4, 10]`.
817824 *
@@ -976,19 +983,25 @@ class Vector {
976983 * @param {p5.Vector } v vector to multiply with the components of the original vector.
977984 * @chainable
978985 */
979- mult ( ... args ) {
986+ mult ( args ) {
980987 const minDimension = prioritizeSmallerDimension ( this . dimensions , args ) ;
981988 shrinkToDimension ( this . values , minDimension ) ;
982989
983- for ( let i = 0 ; i < this . values . length ; i ++ ) {
984- this . values [ i ] *= args [ i ] ;
990+ if ( Array . isArray ( args ) ) {
991+ for ( let i = 0 ; i < this . values . length ; i ++ ) {
992+ this . values [ i ] *= args [ i ] ;
993+ }
994+ } else {
995+ for ( let i = 0 ; i < this . values . length ; i ++ ) {
996+ this . values [ i ] *= args ;
997+ }
985998 }
986999
9871000 return this ;
9881001 }
9891002
9901003 /**
991- * Divides a vector's `x`, `y`, and `z` components.
1004+ * Divides a vector's components.
9921005 *
9931006 * `div()` can use separate numbers, as in `v.div(1, 2, 3)`, another
9941007 * <a href="#/p5.Vector">p5.Vector</a> object, as in `v.div(v2)`, or an array
@@ -998,8 +1011,8 @@ class Vector {
9981011 * will be divided by 2. Calling `div()` with no arguments, as in `v.div()`, has
9991012 * no effect.
10001013 *
1001- * Divide vectors only when they are the same size: both 2D, or both 3D.
1002- * When two vectors of different sizes are divided, the smaller dimension will be
1014+ * You should divide vectors only when they are the same size. When two
1015+ * vectors of different sizes are divided, the smaller dimension will be
10031016 * used, any additional values of the longer vector will be ignored.
10041017 * For example, dividing `[8, 12, 21]` by `[2, 3]` will result in `[4, 4]`.
10051018 *
@@ -1165,24 +1178,41 @@ class Vector {
11651178 * @param {p5.Vector } v vector to divide the components of the original vector by.
11661179 * @chainable
11671180 */
1168- div ( ... args ) {
1181+ div ( args ) {
11691182 const minDimension = prioritizeSmallerDimension ( this . dimensions , args ) ;
11701183
1171- for ( let i = 0 ; i < minDimension ; i ++ ) {
1172- if ( typeof args [ i ] !== 'number' || args [ i ] === 0 ) {
1173- if ( ! this . friendlyErrorsDisabled ( ) ) {
1174- console . warn (
1175- 'p5.Vector.prototype.div' ,
1176- 'Arguments contain components that are 0'
1177- ) ;
1184+ if ( Array . isArray ( args ) ) {
1185+ for ( let i = 0 ; i < minDimension ; i ++ ) {
1186+ if ( ( typeof args [ i ] !== 'number' || args [ i ] === 0 ) ) {
1187+ if ( ! this . friendlyErrorsDisabled ( ) ) {
1188+ console . warn (
1189+ 'p5.Vector.prototype.div' ,
1190+ 'Arguments contain components that are 0'
1191+ ) ;
1192+ }
1193+ return this ;
11781194 }
1179- return this ;
11801195 }
1196+ } else if ( typeof args !== 'number' || args === 0 ) {
1197+ if ( ! this . friendlyErrorsDisabled ( ) ) {
1198+ console . warn (
1199+ 'p5.Vector.prototype.div' ,
1200+ 'Arguments contain components that are 0'
1201+ ) ;
1202+ }
1203+ return this ;
11811204 }
11821205
11831206 shrinkToDimension ( this . values , minDimension ) ;
1184- for ( let i = 0 ; i < this . values . length ; i ++ ) {
1185- this . values [ i ] /= args [ i ] ;
1207+
1208+ if ( Array . isArray ( args ) ) {
1209+ for ( let i = 0 ; i < this . values . length ; i ++ ) {
1210+ this . values [ i ] /= args [ i ] ;
1211+ }
1212+ } else {
1213+ for ( let i = 0 ; i < this . values . length ; i ++ ) {
1214+ this . values [ i ] /= args ;
1215+ }
11861216 }
11871217
11881218 return this ;
0 commit comments