Skip to content
Open
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
16 changes: 15 additions & 1 deletion src/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2189,7 +2189,21 @@ class Vector {
*/
setHeading(a) {
if (this.isPInst) a = this._toRadians(a);
let m = this.mag();
if (this.z !== 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think, this condition is not correct, since the z component of the vector can be zero and it could still be a 3-d vector. Maybe you need to replace this.z !== 0 with this.z !== undefined.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, one more thought. Since setHeading() is logically meaningful only for 2d vectors, so do you think we need to check this.y === undefined as well?

p5._friendlyError(
'p5.Vector.setHeading() only supports 2D vectors (z === 0). ' +
'For 3D or higher-dimensional vectors, use rotate() or another ' +
'appropriate method instead.',
'p5.Vector.setHeading'
);
return this;
}
const m = this.mag();
if (m === 0) {
this.x = 0;
this.y = 0;
return this;
}
Comment on lines +2201 to +2206
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this block of code is mostly redundant. When m === 0 we had this.x = m * Math.cos(a) which comes out to be this.x = 0 * Math.cos(a) which is this.x = 0, so we can remove this block and revert that change.

this.x = m * Math.cos(a);
this.y = m * Math.sin(a);
return this;
Expand Down
Loading