Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,7 @@ p5.Vector.prototype.heading = function heading() {
*/

p5.Vector.prototype.setHeading = function setHeading(a) {
if (this.isPInst) a = this._toRadians(a);
let m = this.mag();
this.x = m * Math.cos(a);
this.y = m * Math.sin(a);
Expand Down
17 changes: 15 additions & 2 deletions test/unit/math/p5.Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,28 @@ suite('p5.Vector', function() {
});
var v;

suite('setHeading', function() {
suite('p5.prototype.setHeading() RADIANS', function() {
setup(function() {
myp5.angleMode(RADIANS);
v = myp5.createVector(1, 1);
v.setHeading(1);
});
test('should have heading() value of 1', function() {
test('should have heading() value of 1 (RADIANS)', function() {
assert.closeTo(v.heading(), 1, 0.001);
});
});

suite('p5.prototype.setHeading() DEGREES', function() {
setup(function() {
myp5.angleMode(DEGREES);
v = myp5.createVector(1, 1);
v.setHeading(1);
});
test('should have heading() value of 1 (DEGREES)', function() {
assert.closeTo(v.heading(), 1, 0.001);
});
});

suite('p5.prototype.createVector()', function() {
setup(function() {
v = myp5.createVector();
Expand Down