Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 74 additions & 44 deletions src/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ import * as constants from '../core/constants';
* This function is used by binary vector operations to prioritize shorter vectors,
* and to emit a warning when lengths do not match.
*/
const prioritizeSmallerDimension = function(currentVectorDimension, args) {
return Math.min(currentVectorDimension, args.length);
const prioritizeSmallerDimension = function (currentVectorDimension, args) {
const resultDimension = Math.min(currentVectorDimension, args.length);
if (Array.isArray(args) && currentVectorDimension !== args.length) {
console.warn(
'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.'
);
}
return resultDimension;
};

/**
Expand Down Expand Up @@ -399,20 +405,17 @@ class Vector {
}
}



/**
* 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])`.
*
* Add vectors only when they are the same size: both 2-dimensional, or
* both 3-dimensional. When two vectors of different sizes are added, the
* smaller dimension will be used, any additional values of the longer
* vector will be ignored.
* For example, adding `[1, 2, 3]` and `[4, 5]` will result in `[5, 7]`.
* You should add vectors only when they are the same size. When two vectors
* of different sizes are added, the smaller dimension will be used, any
* additional values of the longer vector will be ignored. For example,
* adding `[1, 2, 3]` and `[4, 5]` will result in `[5, 7]`.
*
* Calling `add()` with no arguments, as in `v.add()`, has no effect.
*
Expand Down Expand Up @@ -529,7 +532,7 @@ class Vector {
* @param {p5.Vector|Number[]} value The vector to add
* @chainable
*/
add(...args) {
add(args) {
const minDimension = prioritizeSmallerDimension(this.dimensions, args);
shrinkToDimension(this.values, minDimension);

Expand All @@ -540,11 +543,8 @@ class Vector {
return this;
}



/**
* Performs modulo (remainder) division with a vector's `x`, `y`, and `z`
* components.
* Performs modulo (remainder) division with a vector's 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
Expand All @@ -554,8 +554,8 @@ class Vector {
* will be set to their values modulo 2. Calling `rem()` with no
* arguments, as in `v.rem()`, has no effect.
*
* Modulo vectors only when they are the same size: both 2D, or both 3D.
* When two vectors of different sizes are used, the smaller dimension will be
* You should modulo vectors only when they are the same size. When two
* vectors of different sizes are used, the smaller dimension will be
* used, any additional values of the longer vector will be ignored.
* For example, taking `[3, 6, 9]` modulo `[2, 4]` will result in `[1, 2]`.
*
Expand Down Expand Up @@ -653,30 +653,37 @@ class Vector {
* @param {p5.Vector | Number[]} value divisor vector.
* @chainable
*/
rem(...args) {
rem(args) {
const minDimension = prioritizeSmallerDimension(this.dimensions, args);

shrinkToDimension(this.values, minDimension);
for (let i = 0; i < this.values.length; i++) {
if (args[i] > 0) {
this.values[i] = this.values[i] % args[i];

if(Array.isArray(args)){
for (let i = 0; i < this.values.length; i++) {
if (args[i] > 0) {
this.values[i] = this.values[i] % args[i];
}
}
} else if(args > 0) {
for (let i = 0; i < this.values.length; i++) {
this.values[i] = this.values[i] % args;
}
}

return this;
}

/**
* Subtracts from a vector's `x`, `y`, and `z` components.
* Subtracts from a vector's 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])`.
*
* Calling `sub()` with no arguments, as in `v.sub()`, has no effect.
*
* Subtract vectors only when they are the same size: both 2D, or both 3D.
* When two vectors of different sizes are used, the smaller dimension will be
* You should subtract vectors only when they are the same size. When two
* vectors of different sizes are used, the smaller dimension will be
* used, any additional values of the longer vector will be ignored.
* For example, subtracting `[1, 2]` from `[3, 5, 7]` will result in `[2, 3]`.
*
Expand Down Expand Up @@ -788,7 +795,7 @@ class Vector {
* @param {p5.Vector|Number[]} value the vector to subtract
* @chainable
*/
sub(...args) {
sub(args) {
const minDimension = prioritizeSmallerDimension(this.dimensions, args);
shrinkToDimension(this.values, minDimension);

Expand All @@ -800,7 +807,7 @@ class Vector {
}

/**
* Multiplies a vector's `x`, `y`, and `z` components.
* Multiplies a vector's 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
Expand All @@ -810,8 +817,8 @@ class Vector {
* will be multiplied by 2. Calling `mult()` with no arguments, as in `v.mult()`, has
* no effect.
*
* Multiply vectors only when they are the same size: both 2D, or both 3D.
* When two vectors of different sizes are multiplied, the smaller dimension will be
* You should multiply vectors only when they are the same size. When two
* vectors of different sizes are multiplied, the smaller dimension will be
* used, any additional values of the longer vector will be ignored.
* For example, multiplying `[1, 2, 3]` by `[4, 5]` will result in `[4, 10]`.
*
Expand Down Expand Up @@ -976,19 +983,25 @@ class Vector {
* @param {p5.Vector} v vector to multiply with the components of the original vector.
* @chainable
*/
mult(...args) {
mult(args) {
const minDimension = prioritizeSmallerDimension(this.dimensions, args);
shrinkToDimension(this.values, minDimension);

for (let i = 0; i < this.values.length; i++) {
this.values[i] *= args[i];
if(Array.isArray(args)){
for (let i = 0; i < this.values.length; i++) {
this.values[i] *= args[i];
}
} else {
for (let i = 0; i < this.values.length; i++) {
this.values[i] *= args;
}
}

return this;
}

/**
* Divides a vector's `x`, `y`, and `z` components.
* Divides a vector's components.
*
* `div()` can use separate numbers, as in `v.div(1, 2, 3)`, another
* <a href="#/p5.Vector">p5.Vector</a> object, as in `v.div(v2)`, or an array
Expand All @@ -998,8 +1011,8 @@ class Vector {
* will be divided by 2. Calling `div()` with no arguments, as in `v.div()`, has
* no effect.
*
* Divide vectors only when they are the same size: both 2D, or both 3D.
* When two vectors of different sizes are divided, the smaller dimension will be
* You should divide vectors only when they are the same size. When two
* vectors of different sizes are divided, the smaller dimension will be
* used, any additional values of the longer vector will be ignored.
* For example, dividing `[8, 12, 21]` by `[2, 3]` will result in `[4, 4]`.
*
Expand Down Expand Up @@ -1165,24 +1178,41 @@ class Vector {
* @param {p5.Vector} v vector to divide the components of the original vector by.
* @chainable
*/
div(...args) {
div(args) {
const minDimension = prioritizeSmallerDimension(this.dimensions, args);

for (let i = 0; i < minDimension; i++) {
if (typeof args[i] !== 'number' || args[i] === 0) {
if (!this.friendlyErrorsDisabled()) {
console.warn(
'p5.Vector.prototype.div',
'Arguments contain components that are 0'
);
if (Array.isArray(args)) {
for (let i = 0; i < minDimension; i++) {
if ((typeof args[i] !== 'number' || args[i] === 0)) {
if (!this.friendlyErrorsDisabled()) {
console.warn(
'p5.Vector.prototype.div',
'Arguments contain components that are 0'
);
}
return this;
}
return this;
}
} else if(typeof args !== 'number' || args === 0) {
if (!this.friendlyErrorsDisabled()) {
console.warn(
'p5.Vector.prototype.div',
'Arguments contain components that are 0'
);
}
return this;
}

shrinkToDimension(this.values, minDimension);
for (let i = 0; i < this.values.length; i++) {
this.values[i] /= args[i];

if(Array.isArray(args)){
for (let i = 0; i < this.values.length; i++) {
this.values[i] /= args[i];
}
} else {
for (let i = 0; i < this.values.length; i++) {
this.values[i] /= args;
}
}

return this;
Expand Down
22 changes: 17 additions & 5 deletions src/math/patch-vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function _defaultEmptyVector(target){
*/
export function _validatedVectorOperation(expectsSoloNumberArgument){
return function(target){
return function(...args){
return function (...args) {
if (args.length === 0) {
// No arguments? No action
return this;
Expand All @@ -38,12 +38,14 @@ export function _validatedVectorOperation(expectsSoloNumberArgument){
} else if (Array.isArray(args[0])) {
// First argument is an array? Great, keep it!
args = args[0];
} else if (expectsSoloNumberArgument && args.length === 1){
} else if (args.length === 1){
// Special case for a solo numeric arguments only applies sometimes
args = new Array(3).fill(args[0]);
if (expectsSoloNumberArgument) {
args = args[0];
}
}

if (Array.isArray(args)) {
if(Array.isArray(args)){
for (let i = 0; i < args.length; i++) {
const v = args[i];
if (typeof v !== 'number' || !Number.isFinite(v)) {
Expand All @@ -56,9 +58,19 @@ export function _validatedVectorOperation(expectsSoloNumberArgument){
return this;
}
}
} else {
if (typeof args !== 'number' || !Number.isFinite(args)) {
if (!Vector.friendlyErrorsDisabled()) {
this._friendlyError(
'Arguments contain non-finite numbers',
'p5.Vector'
);
}
return this;
}
}

return target.call(this, ...args);
return target.call(this, args);
};
};
}
Expand Down
Loading