From 74e4b250367485834147366cd20c2dc900f5bca3 Mon Sep 17 00:00:00 2001 From: Aditya Choudhury Date: Thu, 3 Dec 2026 21:12:58 +0530 Subject: [PATCH 1/2] warn when heading() is called on a non-2D vector --- src/math/p5.Vector.js | 17 +++++++++ test/unit/math/p5.Vector.js | 69 +++++++++++++++++++++++++++++++------ 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index 802cee370b..9e2b916638 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,13 @@ class Vector { * } */ heading() { + if (this.dimensions !== 2 && !Vector.friendlyErrorsDisabled()) { + const FES = getFES(); + const dimensions = this.dimensions; + FES.log`heading() is only defined for 2D vectors. This vector has ${dimensions} dimension(s), so only its x and y values are used.`({ + reference: 'p5.Vector.heading' + }); + } const h = Math.atan2(this.y, this.x); if (this.isPInst) return this._fromRadians(h); return h; @@ -3749,6 +3764,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..5abae52a0d 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,73 @@ 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('does not warn or throw when friendly errors are disabled', function () { + mockP5.disableFriendlyErrors = true; + try { + expect(new Vector(1, 2, 3).heading()).to.be.closeTo( + Math.atan2(2, 1), + 0.01 + ); + expect(FESLogCalled).to.eql(false); + } finally { + mockP5.disableFriendlyErrors = false; + } + }); + }); }); suite('lerp', function () { From 4c265b6668f3c123cb4bb4fd5cc1e2505e3c29ce Mon Sep 17 00:00:00 2001 From: Aditya Choudhury Date: Thu, 3 Dec 2026 21:12:58 +0530 Subject: [PATCH 2/2] Make 2D vector warning generic and cover rotate() --- src/math/p5.Vector.js | 10 ++++++---- test/unit/math/p5.Vector.js | 19 ++++++++----------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index 9e2b916638..4296ee0160 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -1935,12 +1935,10 @@ class Vector { * } */ heading() { - if (this.dimensions !== 2 && !Vector.friendlyErrorsDisabled()) { + if (this.dimensions !== 2) { const FES = getFES(); const dimensions = this.dimensions; - FES.log`heading() is only defined for 2D vectors. This vector has ${dimensions} dimension(s), so only its x and y values are used.`({ - reference: 'p5.Vector.heading' - }); + 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); @@ -2065,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 diff --git a/test/unit/math/p5.Vector.js b/test/unit/math/p5.Vector.js index 5abae52a0d..5130bbc237 100644 --- a/test/unit/math/p5.Vector.js +++ b/test/unit/math/p5.Vector.js @@ -1571,17 +1571,14 @@ suite('p5.Vector', function () { expect(FESLogCalled).to.eql(false); }); - test('does not warn or throw when friendly errors are disabled', function () { - mockP5.disableFriendlyErrors = true; - try { - expect(new Vector(1, 2, 3).heading()).to.be.closeTo( - Math.atan2(2, 1), - 0.01 - ); - expect(FESLogCalled).to.eql(false); - } finally { - mockP5.disableFriendlyErrors = 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); }); }); });