Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
80 changes: 58 additions & 22 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 @@ -529,7 +535,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 Down Expand Up @@ -653,13 +659,20 @@ 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;
}
}

Expand Down Expand Up @@ -788,7 +801,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 Down Expand Up @@ -976,12 +989,18 @@ 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;
Expand Down Expand Up @@ -1165,24 +1184,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