diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index 802cee370b..4296ee0160 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -31,6 +31,14 @@ const shrinkToDimension = function (arr, dim) { } }; +/** + * @private + * Returns the Friendly Error System. When this file is registered as a p5 + * addon, this is replaced with a function that returns p5.FES. Until then + * (for example, when Vector is imported on its own), messages are no-ops. + */ +let getFES = () => ({ log: () => () => {} }); + class Vector { /** * The values of an N-dimensional vector. @@ -1927,6 +1935,11 @@ class Vector { * } */ heading() { + if (this.dimensions !== 2) { + const FES = getFES(); + const dimensions = this.dimensions; + FES.log`This operation is only defined for 2D vectors. This vector has ${dimensions} dimension(s), so only its x and y values are used.`(); + } const h = Math.atan2(this.y, this.x); if (this.isPInst) return this._fromRadians(h); return h; @@ -2050,6 +2063,10 @@ class Vector { * returns a new p5.Vector object and doesn't change * the original. * + * `rotate()` is only defined for 2D vectors. If it's called on a vector + * with any other number of dimensions, a friendly warning is shown and only + * the vector's x and y values are rotated. + * * @param {Number} angle angle of rotation. * @chainable * @example @@ -3749,6 +3766,8 @@ function vector(p5, fn) { */ p5.Vector = Vector; + getFES = () => p5.FES; + Vector.prototype._friendlyError = p5._friendlyError; Vector.prototype.friendlyErrorsDisabled = function () { return p5.disableFriendlyErrors; diff --git a/test/unit/math/p5.Vector.js b/test/unit/math/p5.Vector.js index 3130b55b25..5130bbc237 100644 --- a/test/unit/math/p5.Vector.js +++ b/test/unit/math/p5.Vector.js @@ -9,10 +9,16 @@ suite('p5.Vector', function () { var v; let FESCalled = false; + let FESLogCalled = false; const mockP5 = { _friendlyError: function (msg, func) { FESCalled = true; console.warn(msg); + }, + FES: { + log: () => () => { + FESLogCalled = true; + } } }; const options = { p5: mockP5 }; @@ -1452,7 +1458,7 @@ suite('p5.Vector', function () { suite('heading', function () { beforeEach(function () { - v = new Vector(0, 0, 0); + v = new Vector(0, 0); }); suite('p5.Vector.prototype.heading() [INSTANCE]', function () { @@ -1463,21 +1469,18 @@ suite('p5.Vector', function () { test('heading for vector pointing right is 0', function () { v.x = 1; v.y = 0; - v.z = 0; expect(v.heading()).to.be.closeTo(0, 0.01); }); test('heading for vector pointing down is PI/2', function () { v.x = 0; v.y = 1; - v.z = 0; expect(v.heading()).to.be.closeTo(Math.PI / 2, 0.01); }); test('heading for vector pointing left is PI', function () { v.x = -1; v.y = 0; - v.z = 0; expect(v.heading()).to.be.closeTo(Math.PI, 0.01); }); @@ -1489,21 +1492,18 @@ suite('p5.Vector', function () { test('heading for vector pointing right is 0', function () { v.x = 1; v.y = 0; - v.z = 0; expect(v.heading()).to.equal(0); }); test('heading for vector pointing down is 90', function () { v.x = 0; v.y = 1; - v.z = 0; expect(v.heading()).to.equal(90); }); test('heading for vector pointing left is 180', function () { v.x = -1; v.y = 0; - v.z = 0; expect(v.heading()).to.equal(180); }); }); @@ -1517,24 +1517,70 @@ suite('p5.Vector', function () { test('heading for vector pointing right is 0', function () { v.x = 1; v.y = 0; - v.z = 0; expect(Vector.heading(v)).to.be.closeTo(0, 0.01); }); test('heading for vector pointing down is PI/2', function () { v.x = 0; v.y = 1; - v.z = 0; expect(Vector.heading(v)).to.be.closeTo(Math.PI / 2, 0.01); }); test('heading for vector pointing left is PI', function () { v.x = -1; v.y = 0; - v.z = 0; expect(Vector.heading(v)).to.be.closeTo(Math.PI, 0.01); }); }); + + suite('dimension warning', function () { + beforeEach(function () { + FESLogCalled = false; + }); + + test('does not warn for a 2D vector', function () { + new Vector(1, 2).heading(); + expect(FESLogCalled).to.eql(false); + }); + + test('warns for a 1D vector and uses y = 0', function () { + expect(new Vector(5).heading()).to.be.closeTo(0, 0.01); + expect(FESLogCalled).to.eql(true); + }); + + test('warns for a 3D vector and ignores z', function () { + expect(new Vector(1, 2, 3).heading()).to.be.closeTo( + Math.atan2(2, 1), + 0.01 + ); + expect(FESLogCalled).to.eql(true); + }); + + test('warns for a 4D vector', function () { + new Vector(1, 2, 3, 4).heading(); + expect(FESLogCalled).to.eql(true); + }); + + test('static p5.Vector.heading() warns for a 3D vector', function () { + Vector.heading(new Vector(1, 2, 3)); + expect(FESLogCalled).to.eql(true); + }); + + test('static p5.Vector.heading() does not warn for a 2D vector', function () { + Vector.heading(new Vector(1, 2)); + expect(FESLogCalled).to.eql(false); + }); + + test('rotate() warns for a 3D vector', function () { + new Vector(1, 0, 0).rotate(0.1); + expect(FESLogCalled).to.eql(true); + }); + + test('rotate() does not warn for a 2D vector', function () { + new Vector(1, 0).rotate(0.1); + expect(FESLogCalled).to.eql(false); + }); + }); }); suite('lerp', function () {