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
14 changes: 9 additions & 5 deletions src/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2757,8 +2757,10 @@ class Vector {
* <a href="#/p5.Vector">p5.Vector</a> 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 <a href="#/p5.Vector">p5.Vector</a> objects.
Expand Down Expand Up @@ -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;
}
}
Expand Down
31 changes: 31 additions & 0 deletions test/unit/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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;
});
});
});

Expand Down
Loading