Skip to content

Commit 47f642a

Browse files
authored
Merge pull request #1738 from DonKarlssonSan/master
Fixing Vector.rotate() #1728
2 parents 1c0a623 + 0c53e89 commit 47f642a

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/math/p5.Vector.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,12 +613,12 @@ p5.Vector.prototype.heading = function () {
613613
* </div>
614614
*/
615615
p5.Vector.prototype.rotate = function (a) {
616+
var newHeading = this.heading() + a;
616617
if (this.p5) {
617618
if (this.p5._angleMode === constants.DEGREES) {
618-
a = polarGeometry.degreesToRadians(a);
619+
newHeading = polarGeometry.degreesToRadians(newHeading);
619620
}
620621
}
621-
var newHeading = this.heading() + a;
622622
var mag = this.mag();
623623
this.x = Math.cos(newHeading) * mag;
624624
this.y = Math.sin(newHeading) * mag;

test/unit/math/p5.Vector.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

22
suite('p5.Vector', function() {
3+
var RADIANS = 'radians';
4+
var DEGREES = 'degrees';
35

46
var myp5 = new p5(function( p ) {
57
p.setup = function() {};
@@ -84,8 +86,35 @@ suite('p5.Vector', function() {
8486
assert.equal(v.z, 0);
8587
});
8688
});
87-
});
8889

90+
suite('p5.prototype.rotate() RADIANS', function() {
91+
setup(function() {
92+
myp5.angleMode(RADIANS);
93+
v = myp5.createVector(0, 1);
94+
});
95+
96+
test('should have x, y, z rotated to 0, -1, 0 (RADIANS)', function() {
97+
v.rotate(Math.PI);
98+
assert.closeTo(v.x, 0, 0.001);
99+
assert.closeTo(v.y, -1, 0.001);
100+
assert.closeTo(v.z, 0, 0.001);
101+
});
102+
});
103+
104+
suite('p5.prototype.rotate() DEGREES', function() {
105+
setup(function() {
106+
myp5.angleMode(DEGREES);
107+
v = myp5.createVector(0, 1);
108+
});
109+
110+
test('should have x, y, z rotated to 0, -1, 0 (DEGREES)', function() {
111+
v.rotate(180);
112+
assert.closeTo(v.x, 0, 0.001);
113+
assert.closeTo(v.y, -1, 0.001);
114+
assert.closeTo(v.z, 0, 0.001);
115+
});
116+
});
117+
});
89118

90119
// describe('set()', function() {
91120
// describe('with p5.Vector', function() {

0 commit comments

Comments
 (0)