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
19 changes: 19 additions & 0 deletions src/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2050,6 +2063,10 @@ class Vector {
* returns a new <a href="#/p5.Vector">p5.Vector</a> 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
Expand Down Expand Up @@ -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;
Expand Down
66 changes: 56 additions & 10 deletions test/unit/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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 () {
Expand All @@ -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);
});

Expand All @@ -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);
});
});
Expand All @@ -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 () {
Expand Down