diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js
index e7a90d4218..e5275ad7cc 100644
--- a/src/math/p5.Vector.js
+++ b/src/math/p5.Vector.js
@@ -13,7 +13,7 @@ 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.'
+ 'When working with two vectors of different sizes, the smaller dimension is used. In this operation, both vectors will be treated as ' + resultDimension + 'D vectors, and any additional values of the longer vector will be ignored.'
);
}
return resultDimension;
@@ -2757,8 +2757,10 @@ class Vector {
* p5.Vector object.
*
* The version of `equals()` with multiple parameters interprets them as the
- * components of another vector. Any missing parameters are assigned the value
- * 0.
+ * components of another vector.
+ *
+ * If the two vectors have different lengths, a warning is logged and only
+ * the components up to the shorter length are compared.
*
* The static version of `equals()`, as in `p5.Vector.equals(v0, v1)`,
* interprets both parameters as p5.Vector objects.
@@ -2826,8 +2828,10 @@ class Vector {
values = args;
}
- for (let i = 0; i < this.values.length; i++) {
- if (this.values[i] !== (values[i] || 0)) {
+ const minDimension = prioritizeSmallerDimension(this.values.length, values);
+
+ for (let i = 0; i < minDimension; i++) {
+ if (this.values[i] !== values[i]) {
return false;
}
}
diff --git a/test/unit/math/p5.Vector.js b/test/unit/math/p5.Vector.js
index df694d71a6..bbe7c38a5b 100644
--- a/test/unit/math/p5.Vector.js
+++ b/test/unit/math/p5.Vector.js
@@ -2034,6 +2034,25 @@ suite('p5.Vector', function () {
const v1 = new Vector(0, -1, 1);
expect(v1.equals(0, -1, 1)).to.be.true;
});
+
+ test('should be symmetric when dimensions differ', function () {
+ const v3d = new Vector(1, 2, 3);
+ const v4d = new Vector(1, 2, 3, 4);
+ expect(v3d.equals(v4d)).to.equal(v4d.equals(v3d));
+ });
+
+ test('should compare up to the shorter length when dimensions differ', function () {
+ const v3d = new Vector(1, 2, 3);
+ const v4d = new Vector(1, 2, 3, 4);
+ expect(v3d.equals(v4d)).to.be.true;
+ });
+
+ test('should return false when values differ within the shorter length', function () {
+ const v3d = new Vector(1, 2, 3);
+ const v4d = new Vector(1, 2, 9, 4);
+ expect(v3d.equals(v4d)).to.be.false;
+ expect(v4d.equals(v3d)).to.be.false;
+ });
});
suite('p5.Vector.equals() [CLASS]', function () {
@@ -2064,6 +2083,18 @@ suite('p5.Vector', function () {
const a2 = [0, -1, 1];
expect(Vector.equals(a1, a2)).to.be.true;
});
+
+ test('should be symmetric when dimensions differ', function () {
+ const v3d = new Vector(1, 2, 3);
+ const v4d = new Vector(1, 2, 3, 4);
+ expect(Vector.equals(v3d, v4d)).to.equal(Vector.equals(v4d, v3d));
+ });
+
+ test('should compare up to the shorter length when dimensions differ', function () {
+ const v3d = new Vector(1, 2, 3);
+ const v4d = new Vector(1, 2, 3, 4);
+ expect(Vector.equals(v3d, v4d)).to.be.true;
+ });
});
});