Skip to content

Commit df9f948

Browse files
authored
Merge pull request #6831 from asukaminato0721/update-vector
Update p5.Vector.js
2 parents cf94f56 + a20ceef commit df9f948

File tree

1 file changed

+78
-70
lines changed

1 file changed

+78
-70
lines changed

src/math/p5.Vector.js

Lines changed: 78 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,8 @@ p5.Vector = class {
585585
* @param {p5.Vector | Number[]} value divisor vector.
586586
* @chainable
587587
*/
588-
rem (x, y, z) {
588+
rem (...args) {
589+
let [x, y, z] = args;
589590
if (x instanceof p5.Vector) {
590591
if ([x.x,x.y,x.z].every(Number.isFinite)) {
591592
const xComponent = parseFloat(x.x);
@@ -598,41 +599,35 @@ p5.Vector = class {
598599
);
599600
}
600601
} else if (Array.isArray(x)) {
601-
if (x.every(element => Number.isFinite(element))) {
602+
if (x.every(Number.isFinite)) {
602603
if (x.length === 2) {
603604
return this.calculateRemainder2D(x[0], x[1]);
604605
}
605606
if (x.length === 3) {
606607
return this.calculateRemainder3D(x[0], x[1], x[2]);
607608
}
608609
}
609-
} else if (arguments.length === 1) {
610-
if (Number.isFinite(arguments[0]) && arguments[0] !== 0) {
611-
this.x = this.x % arguments[0];
612-
this.y = this.y % arguments[0];
613-
this.z = this.z % arguments[0];
610+
} else if (args.length === 1) {
611+
if (Number.isFinite(x) && x !== 0) {
612+
this.x = this.x % x;
613+
this.y = this.y % x;
614+
this.z = this.z % x;
614615
return this;
615616
}
616-
} else if (arguments.length === 2) {
617-
const vectorComponents = [...arguments];
618-
if (vectorComponents.every(element => Number.isFinite(element))) {
619-
if (vectorComponents.length === 2) {
620-
return this.calculateRemainder2D(
621-
vectorComponents[0],
622-
vectorComponents[1]
623-
);
624-
}
617+
} else if (args.length === 2) {
618+
if (args.every(Number.isFinite)) {
619+
return this.calculateRemainder2D(
620+
x,
621+
y
622+
);
625623
}
626-
} else if (arguments.length === 3) {
627-
const vectorComponents = [...arguments];
628-
if (vectorComponents.every(element => Number.isFinite(element))) {
629-
if (vectorComponents.length === 3) {
630-
return this.calculateRemainder3D(
631-
vectorComponents[0],
632-
vectorComponents[1],
633-
vectorComponents[2]
634-
);
635-
}
624+
} else if (args.length === 3) {
625+
if (args.every(Number.isFinite)) {
626+
return this.calculateRemainder3D(
627+
x,
628+
y,
629+
z
630+
);
636631
}
637632
}
638633
}
@@ -987,7 +982,8 @@ p5.Vector = class {
987982
* @chainable
988983
*/
989984

990-
mult(x, y, z) {
985+
mult(...args) {
986+
let [x, y, z] = args;
991987
if (x instanceof p5.Vector) {
992988
// new p5.Vector will check that values are valid upon construction but it's possible
993989
// that someone could change the value of a component after creation, which is why we still
@@ -1037,21 +1033,21 @@ p5.Vector = class {
10371033
return this;
10381034
}
10391035

1040-
const vectorComponents = [...arguments];
1036+
const vectorComponents = args;
10411037
if (
10421038
vectorComponents.every(element => Number.isFinite(element)) &&
10431039
vectorComponents.every(element => typeof element === 'number')
10441040
) {
1045-
if (arguments.length === 1) {
1041+
if (args.length === 1) {
10461042
this.x *= x;
10471043
this.y *= x;
10481044
this.z *= x;
10491045
}
1050-
if (arguments.length === 2) {
1046+
if (args.length === 2) {
10511047
this.x *= x;
10521048
this.y *= y;
10531049
}
1054-
if (arguments.length === 3) {
1050+
if (args.length === 3) {
10551051
this.x *= x;
10561052
this.y *= y;
10571053
this.z *= z;
@@ -1267,7 +1263,8 @@ p5.Vector = class {
12671263
* @param {p5.Vector} v vector to divide the components of the original vector by.
12681264
* @chainable
12691265
*/
1270-
div(x, y, z) {
1266+
div(...args) {
1267+
let [x, y, z] = args;
12711268
if (x instanceof p5.Vector) {
12721269
// new p5.Vector will check that values are valid upon construction but it's possible
12731270
// that someone could change the value of a component after creation, which is why we still
@@ -1300,7 +1297,7 @@ p5.Vector = class {
13001297
}
13011298
if (Array.isArray(x)) {
13021299
if (
1303-
x.every(element => Number.isFinite(element)) &&
1300+
x.every(Number.isFinite) &&
13041301
x.every(element => typeof element === 'number')
13051302
) {
13061303
if (x.some(element => element === 0)) {
@@ -1330,26 +1327,25 @@ p5.Vector = class {
13301327
return this;
13311328
}
13321329

1333-
const vectorComponents = [...arguments];
13341330
if (
1335-
vectorComponents.every(element => Number.isFinite(element)) &&
1336-
vectorComponents.every(element => typeof element === 'number')
1331+
args.every(Number.isFinite) &&
1332+
args.every(element => typeof element === 'number')
13371333
) {
1338-
if (vectorComponents.some(element => element === 0)) {
1334+
if (args.some(element => element === 0)) {
13391335
console.warn('p5.Vector.prototype.div:', 'divide by 0');
13401336
return this;
13411337
}
13421338

1343-
if (arguments.length === 1) {
1339+
if (args.length === 1) {
13441340
this.x /= x;
13451341
this.y /= x;
13461342
this.z /= x;
13471343
}
1348-
if (arguments.length === 2) {
1344+
if (args.length === 2) {
13491345
this.x /= x;
13501346
this.y /= y;
13511347
}
1352-
if (arguments.length === 3) {
1348+
if (args.length === 3) {
13531349
this.x /= x;
13541350
this.y /= y;
13551351
this.z /= z;
@@ -3171,10 +3167,7 @@ p5.Vector = class {
31713167
* </code>
31723168
* </div>
31733169
*/
3174-
static fromAngle(angle, length) {
3175-
if (typeof length === 'undefined') {
3176-
length = 1;
3177-
}
3170+
static fromAngle(angle, length = 1) {
31783171
return new p5.Vector(length * Math.cos(angle), length * Math.sin(angle), 0);
31793172
}
31803173

@@ -3234,10 +3227,7 @@ p5.Vector = class {
32343227
* </code>
32353228
* </div>
32363229
*/
3237-
static fromAngles(theta, phi, length) {
3238-
if (typeof length === 'undefined') {
3239-
length = 1;
3240-
}
3230+
static fromAngles(theta, phi, length = 1) {
32413231
const cosPhi = Math.cos(phi);
32423232
const sinPhi = Math.sin(phi);
32433233
const cosTheta = Math.cos(theta);
@@ -3369,10 +3359,11 @@ p5.Vector = class {
33693359
* @return {p5.Vector} resulting <a href="#/p5.Vector">p5.Vector</a>.
33703360
*/
33713361

3372-
static add(v1, v2, target) {
3362+
static add(...args) {
3363+
let [v1, v2, target] = args;
33733364
if (!target) {
33743365
target = v1.copy();
3375-
if (arguments.length === 3) {
3366+
if (args.length === 3) {
33763367
p5._friendlyError(
33773368
'The target parameter is undefined, it should be of type p5.Vector',
33783369
'p5.Vector.add'
@@ -3420,10 +3411,11 @@ p5.Vector = class {
34203411
* @return {p5.Vector} The resulting <a href="#/p5.Vector">p5.Vector</a>
34213412
*/
34223413

3423-
static sub(v1, v2, target) {
3414+
static sub(...args) {
3415+
let [v1, v2, target] = args;
34243416
if (!target) {
34253417
target = v1.copy();
3426-
if (arguments.length === 3) {
3418+
if (args.length === 3) {
34273419
p5._friendlyError(
34283420
'The target parameter is undefined, it should be of type p5.Vector',
34293421
'p5.Vector.sub'
@@ -3455,6 +3447,7 @@ p5.Vector = class {
34553447
* @param {p5.Vector} v
34563448
* @param {Number} n
34573449
* @param {p5.Vector} [target] vector to receive the result.
3450+
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
34583451
*/
34593452

34603453
/**
@@ -3463,6 +3456,7 @@ p5.Vector = class {
34633456
* @param {p5.Vector} v0
34643457
* @param {p5.Vector} v1
34653458
* @param {p5.Vector} [target]
3459+
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
34663460
*/
34673461

34683462
/**
@@ -3471,11 +3465,13 @@ p5.Vector = class {
34713465
* @param {p5.Vector} v0
34723466
* @param {Number[]} arr
34733467
* @param {p5.Vector} [target]
3468+
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
34743469
*/
3475-
static mult(v, n, target) {
3470+
static mult(...args) {
3471+
let [v, n, target] = args;
34763472
if (!target) {
34773473
target = v.copy();
3478-
if (arguments.length === 3) {
3474+
if (args.length === 3) {
34793475
p5._friendlyError(
34803476
'The target parameter is undefined, it should be of type p5.Vector',
34813477
'p5.Vector.mult'
@@ -3498,9 +3494,11 @@ p5.Vector = class {
34983494
* @param {p5.Vector} v
34993495
* @param {Number} angle
35003496
* @param {p5.Vector} [target] The vector to receive the result
3497+
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
35013498
*/
3502-
static rotate(v, a, target) {
3503-
if (arguments.length === 2) {
3499+
static rotate(...args) {
3500+
let [v, a, target] = args;
3501+
if (args.length === 2) {
35043502
target = v.copy();
35053503
} else {
35063504
if (!(target instanceof p5.Vector)) {
@@ -3534,6 +3532,7 @@ p5.Vector = class {
35343532
* @param {p5.Vector} v
35353533
* @param {Number} n
35363534
* @param {p5.Vector} [target] The vector to receive the result
3535+
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
35373536
*/
35383537

35393538
/**
@@ -3542,6 +3541,7 @@ p5.Vector = class {
35423541
* @param {p5.Vector} v0
35433542
* @param {p5.Vector} v1
35443543
* @param {p5.Vector} [target]
3544+
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
35453545
*/
35463546

35473547
/**
@@ -3550,12 +3550,14 @@ p5.Vector = class {
35503550
* @param {p5.Vector} v0
35513551
* @param {Number[]} arr
35523552
* @param {p5.Vector} [target]
3553+
* @return {p5.Vector} The resulting new <a href="#/p5.Vector">p5.Vector</a>
35533554
*/
3554-
static div(v, n, target) {
3555+
static div(...args) {
3556+
let [v, n, target] = args;
35553557
if (!target) {
35563558
target = v.copy();
35573559

3558-
if (arguments.length === 3) {
3560+
if (args.length === 3) {
35593561
p5._friendlyError(
35603562
'The target parameter is undefined, it should be of type p5.Vector',
35613563
'p5.Vector.div'
@@ -3624,10 +3626,11 @@ p5.Vector = class {
36243626
* @param {p5.Vector} [target] The vector to receive the result
36253627
* @return {p5.Vector} The lerped value
36263628
*/
3627-
static lerp(v1, v2, amt, target) {
3629+
static lerp(...args) {
3630+
let [v1, v2, amt, target] = args;
36283631
if (!target) {
36293632
target = v1.copy();
3630-
if (arguments.length === 4) {
3633+
if (args.length === 4) {
36313634
p5._friendlyError(
36323635
'The target parameter is undefined, it should be of type p5.Vector',
36333636
'p5.Vector.lerp'
@@ -3655,10 +3658,11 @@ p5.Vector = class {
36553658
* @param {p5.Vector} [target] vector to receive the result.
36563659
* @return {p5.Vector} slerped vector between v1 and v2
36573660
*/
3658-
static slerp(v1, v2, amt, target) {
3661+
static slerp(...args) {
3662+
let [v1, v2, amt, target] = args;
36593663
if (!target) {
36603664
target = v1.copy();
3661-
if (arguments.length === 4) {
3665+
if (args.length === 4) {
36623666
p5._friendlyError(
36633667
'The target parameter is undefined, it should be of type p5.Vector',
36643668
'p5.Vector.slerp'
@@ -3711,8 +3715,9 @@ p5.Vector = class {
37113715
* @param {p5.Vector} [target] The vector to receive the result
37123716
* @return {p5.Vector} The vector v, normalized to a length of 1
37133717
*/
3714-
static normalize(v, target) {
3715-
if (arguments.length < 2) {
3718+
static normalize(...args) {
3719+
let [v, target] = args;
3720+
if (args.length < 2) {
37163721
target = v.copy();
37173722
} else {
37183723
if (!(target instanceof p5.Vector)) {
@@ -3738,8 +3743,9 @@ p5.Vector = class {
37383743
* @param {p5.Vector} [target] the vector to receive the result (Optional)
37393744
* @return {p5.Vector} v with a magnitude limited to max
37403745
*/
3741-
static limit(v, max, target) {
3742-
if (arguments.length < 3) {
3746+
static limit(...args) {
3747+
let [v, max, target] = args;
3748+
if (args.length < 3) {
37433749
target = v.copy();
37443750
} else {
37453751
if (!(target instanceof p5.Vector)) {
@@ -3765,8 +3771,9 @@ p5.Vector = class {
37653771
* @param {p5.Vector} [target] the vector to receive the result (Optional)
37663772
* @return {p5.Vector} v with a magnitude set to len
37673773
*/
3768-
static setMag(v, len, target) {
3769-
if (arguments.length < 3) {
3774+
static setMag(...args) {
3775+
let [v, len, target] = args;
3776+
if (args.length < 3) {
37703777
target = v.copy();
37713778
} else {
37723779
if (!(target instanceof p5.Vector)) {
@@ -3824,8 +3831,9 @@ p5.Vector = class {
38243831
* @param {p5.Vector} [target] vector to receive the result.
38253832
* @return {p5.Vector} the reflected vector
38263833
*/
3827-
static reflect(incidentVector, surfaceNormal, target) {
3828-
if (arguments.length < 3) {
3834+
static reflect(...args) {
3835+
let [incidentVector, surfaceNormal, target] = args;
3836+
if (args.length < 3) {
38293837
target = incidentVector.copy();
38303838
} else {
38313839
if (!(target instanceof p5.Vector)) {

0 commit comments

Comments
 (0)