Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update p5.Vector.js #6831

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Changes from 2 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
125 changes: 70 additions & 55 deletions src/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ p5.Vector = class {
* @param {p5.Vector | Number[]} value divisor vector.
* @chainable
*/
rem (x, y, z) {
rem (...args) {
let [x, y, z] = args;
if (x instanceof p5.Vector) {
if ([x.x,x.y,x.z].every(Number.isFinite)) {
const xComponent = parseFloat(x.x);
Expand All @@ -456,31 +457,31 @@ p5.Vector = class {
return this.calculateRemainder3D(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];
} else if (args.length === 1) {
if (Number.isFinite(x) && x !== 0) {
this.x = this.x % x;
this.y = this.y % x;
this.z = this.z % x;
return this;
}
} else if (arguments.length === 2) {
const vectorComponents = [...arguments];
} else if (args.length === 2) {
const vectorComponents = args;
if (vectorComponents.every(element => Number.isFinite(element))) {
if (vectorComponents.length === 2) {
asukaminato0721 marked this conversation as resolved.
Show resolved Hide resolved
return this.calculateRemainder2D(
vectorComponents[0],
vectorComponents[1]
x,
y
);
}
}
} else if (arguments.length === 3) {
const vectorComponents = [...arguments];
} else if (args.length === 3) {
const vectorComponents = args;
if (vectorComponents.every(element => Number.isFinite(element))) {
if (vectorComponents.length === 3) {
return this.calculateRemainder3D(
vectorComponents[0],
vectorComponents[1],
vectorComponents[2]
x,
y,
z
);
}
}
Expand Down Expand Up @@ -745,7 +746,8 @@ p5.Vector = class {
* @chainable
*/

mult(x, y, z) {
mult(...args) {
let [x, y, z] = args;
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
Expand Down Expand Up @@ -795,21 +797,21 @@ p5.Vector = class {
return this;
}

const vectorComponents = [...arguments];
const vectorComponents = args;
if (
vectorComponents.every(element => Number.isFinite(element)) &&
vectorComponents.every(element => typeof element === 'number')
) {
if (arguments.length === 1) {
if (args.length === 1) {
this.x *= x;
this.y *= x;
this.z *= x;
}
if (arguments.length === 2) {
if (args.length === 2) {
this.x *= x;
this.y *= y;
}
if (arguments.length === 3) {
if (args.length === 3) {
this.x *= x;
this.y *= y;
this.z *= z;
Expand Down Expand Up @@ -962,7 +964,8 @@ p5.Vector = class {
* @param {p5.Vector} v vector to divide the components of the original vector by.
* @chainable
*/
div(x, y, z) {
div(...args) {
let [x, y, z] = args;
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
Expand Down Expand Up @@ -1025,7 +1028,7 @@ p5.Vector = class {
return this;
}

const vectorComponents = [...arguments];
const vectorComponents = args;
if (
vectorComponents.every(element => Number.isFinite(element)) &&
vectorComponents.every(element => typeof element === 'number')
Expand All @@ -1035,16 +1038,16 @@ p5.Vector = class {
return this;
}

if (arguments.length === 1) {
if (args.length === 1) {
this.x /= x;
this.y /= x;
this.z /= x;
}
if (arguments.length === 2) {
if (args.length === 2) {
this.x /= x;
this.y /= y;
}
if (arguments.length === 3) {
if (args.length === 3) {
this.x /= x;
this.y /= y;
this.z /= z;
Expand Down Expand Up @@ -2400,10 +2403,7 @@ p5.Vector = class {
* </code>
* </div>
*/
static fromAngle(angle, length) {
if (typeof length === 'undefined') {
length = 1;
}
static fromAngle(angle, length = 1) {
return new p5.Vector(length * Math.cos(angle), length * Math.sin(angle), 0);
}

Expand Down Expand Up @@ -2451,10 +2451,7 @@ p5.Vector = class {
* </code>
* </div>
*/
static fromAngles(theta, phi, length) {
if (typeof length === 'undefined') {
length = 1;
}
static fromAngles(theta, phi, length = 1) {
const cosPhi = Math.cos(phi);
const sinPhi = Math.sin(phi);
const cosTheta = Math.cos(theta);
Expand Down Expand Up @@ -2565,10 +2562,11 @@ p5.Vector = class {
* @return {p5.Vector} resulting <a href="#/p5.Vector">p5.Vector</a>.
*/

static add(v1, v2, target) {
static add(...args) {
let [v1, v2, target] = args;
if (!target) {
target = v1.copy();
if (arguments.length === 3) {
if (args.length === 3) {
p5._friendlyError(
'The target parameter is undefined, it should be of type p5.Vector',
'p5.Vector.add'
Expand Down Expand Up @@ -2616,10 +2614,11 @@ p5.Vector = class {
* @return {p5.Vector} The resulting <a href="#/p5.Vector">p5.Vector</a>
*/

static sub(v1, v2, target) {
static sub(...args) {
let [v1, v2, target] = args;
if (!target) {
target = v1.copy();
if (arguments.length === 3) {
if (args.length === 3) {
p5._friendlyError(
'The target parameter is undefined, it should be of type p5.Vector',
'p5.Vector.sub'
Expand Down Expand Up @@ -2651,6 +2650,7 @@ p5.Vector = class {
* @param {p5.Vector} v
* @param {Number} n
* @param {p5.Vector} [target] vector to receive the result.
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
*/

/**
Expand All @@ -2659,6 +2659,7 @@ p5.Vector = class {
* @param {p5.Vector} v0
* @param {p5.Vector} v1
* @param {p5.Vector} [target]
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
*/

/**
Expand All @@ -2667,11 +2668,13 @@ p5.Vector = class {
* @param {p5.Vector} v0
* @param {Number[]} arr
* @param {p5.Vector} [target]
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
*/
static mult(v, n, target) {
static mult(...args) {
let [v, n, target] = args;
if (!target) {
target = v.copy();
if (arguments.length === 3) {
if (args.length === 3) {
p5._friendlyError(
'The target parameter is undefined, it should be of type p5.Vector',
'p5.Vector.mult'
Expand All @@ -2694,9 +2697,11 @@ p5.Vector = class {
* @param {p5.Vector} v
* @param {Number} angle
* @param {p5.Vector} [target] The vector to receive the result
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
*/
static rotate(v, a, target) {
if (arguments.length === 2) {
static rotate(...args) {
let [v, a, target] = args;
if (args.length === 2) {
target = v.copy();
} else {
if (!(target instanceof p5.Vector)) {
Expand Down Expand Up @@ -2730,6 +2735,7 @@ p5.Vector = class {
* @param {p5.Vector} v
* @param {Number} n
* @param {p5.Vector} [target] The vector to receive the result
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
*/

/**
Expand All @@ -2738,6 +2744,7 @@ p5.Vector = class {
* @param {p5.Vector} v0
* @param {p5.Vector} v1
* @param {p5.Vector} [target]
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
*/

/**
Expand All @@ -2746,12 +2753,14 @@ p5.Vector = class {
* @param {p5.Vector} v0
* @param {Number[]} arr
* @param {p5.Vector} [target]
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
*/
static div(v, n, target) {
static div(...args) {
let [v, n, target] = args;
if (!target) {
target = v.copy();

if (arguments.length === 3) {
if (args.length === 3) {
p5._friendlyError(
'The target parameter is undefined, it should be of type p5.Vector',
'p5.Vector.div'
Expand Down Expand Up @@ -2820,10 +2829,11 @@ p5.Vector = class {
* @param {p5.Vector} [target] The vector to receive the result
* @return {p5.Vector} The lerped value
*/
static lerp(v1, v2, amt, target) {
static lerp(...args) {
let [v1, v2, amt, target] = args;
if (!target) {
target = v1.copy();
if (arguments.length === 4) {
if (args.length === 4) {
p5._friendlyError(
'The target parameter is undefined, it should be of type p5.Vector',
'p5.Vector.lerp'
Expand Down Expand Up @@ -2851,10 +2861,11 @@ p5.Vector = class {
* @param {p5.Vector} [target] vector to receive the result.
* @return {p5.Vector} slerped vector between v1 and v2
*/
static slerp(v1, v2, amt, target) {
static slerp(...args) {
let [v1, v2, amt, target] = args;
if (!target) {
target = v1.copy();
if (arguments.length === 4) {
if (args.length === 4) {
p5._friendlyError(
'The target parameter is undefined, it should be of type p5.Vector',
'p5.Vector.slerp'
Expand Down Expand Up @@ -2907,8 +2918,9 @@ p5.Vector = class {
* @param {p5.Vector} [target] The vector to receive the result
* @return {p5.Vector} The vector v, normalized to a length of 1
*/
static normalize(v, target) {
if (arguments.length < 2) {
static normalize(...args) {
let [v, target] = args;
if (args.length < 2) {
target = v.copy();
} else {
if (!(target instanceof p5.Vector)) {
Expand All @@ -2934,8 +2946,9 @@ p5.Vector = class {
* @param {p5.Vector} [target] the vector to receive the result (Optional)
* @return {p5.Vector} v with a magnitude limited to max
*/
static limit(v, max, target) {
if (arguments.length < 3) {
static limit(...args) {
let [v, max, target] = args;
if (args.length < 3) {
target = v.copy();
} else {
if (!(target instanceof p5.Vector)) {
Expand All @@ -2961,8 +2974,9 @@ p5.Vector = class {
* @param {p5.Vector} [target] the vector to receive the result (Optional)
* @return {p5.Vector} v with a magnitude set to len
*/
static setMag(v, len, target) {
if (arguments.length < 3) {
static setMag(...args) {
let [v, len, target] = args;
if (args.length < 3) {
target = v.copy();
} else {
if (!(target instanceof p5.Vector)) {
Expand Down Expand Up @@ -3020,8 +3034,9 @@ p5.Vector = class {
* @param {p5.Vector} [target] vector to receive the result.
* @return {p5.Vector} the reflected vector
*/
static reflect(incidentVector, surfaceNormal, target) {
if (arguments.length < 3) {
static reflect(...args) {
let [incidentVector, surfaceNormal, target] = args;
if (args.length < 3) {
target = incidentVector.copy();
} else {
if (!(target instanceof p5.Vector)) {
Expand Down
Loading