File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -1440,6 +1440,31 @@ p5.Vector.prototype.heading = function heading() {
14401440 return h ;
14411441} ;
14421442
1443+ /**
1444+ * Rotate the vector to a specific angle (only 2D vectors), magnitude remains the
1445+ * same
1446+ *
1447+ * @method setHeading
1448+ * @param {number } angle the angle of rotation
1449+ * @chainable
1450+ * @example
1451+ * <div class="norender">
1452+ * <code>
1453+ * let v = createVector(10.0, 20.0);
1454+ * // result of v.heading() is 1.1071487177940904
1455+ * v.setHeading(Math.PI);
1456+ * // result of v.heading() is now 3.141592653589793
1457+ * </code>
1458+ * </div>
1459+ */
1460+
1461+ p5 . Vector . prototype . setHeading = function setHeading ( a ) {
1462+ let m = this . mag ( ) ;
1463+ this . x = m * Math . cos ( a ) ;
1464+ this . y = m * Math . sin ( a ) ;
1465+ return this ;
1466+ } ;
1467+
14431468/**
14441469 * Rotate the vector by an angle (only 2D vectors), magnitude remains the
14451470 * same
Original file line number Diff line number Diff line change @@ -18,6 +18,15 @@ suite('p5.Vector', function() {
1818 } ) ;
1919 var v ;
2020
21+ suite ( 'setHeading' , function ( ) {
22+ setup ( function ( ) {
23+ v = myp5 . createVector ( 1 , 1 ) ;
24+ v . setHeading ( 1 ) ;
25+ } ) ;
26+ test ( 'should have heading() value of 1' , function ( ) {
27+ assert . closeTo ( v . heading ( ) , 1 , 0.001 ) ;
28+ } ) ;
29+ } ) ;
2130 suite ( 'p5.prototype.createVector()' , function ( ) {
2231 setup ( function ( ) {
2332 v = myp5 . createVector ( ) ;
You can’t perform that action at this time.
0 commit comments