title: Getting the angle from a direction vector?
body: <p>I have this simple function to <strong>set</strong> an angle for a vector. It effectively gets the vector's current magnitude (length), calulates the angle and converts the angle from radians to degrees. Then I apply the angle to X and Y, lastly multiplying the vector by it's original magnitude.</p>
  <pre><code>this.setAngle = function(degree){
      var l = this.length();  //magnitude of vector
      var angle = degree*Math.PI/180; //degress converted to radians
      this.x=Math.cos(angle);
      this.y=Math.sin(angle);
      this.multiply(l);  //original magnitude
      return;
  }
  </code></pre>
  <p>However I am unsure how to obtain (get) an angle from a Vector. Below is my attempt:</p>
  <pre><code>this.getAngle = function(){
      var angle = Math.atan(this.y/this.x);   //radians
      var degrees = angle/(180*Math.PI);  //degrees
      return Math.floor(degrees); //round number, avoid decimal fragments
  }
  </code></pre>
  <p>This attempt doesn't return any value except 0 or -1.</p>
  <p><em>Any suggestions?</em></p>
  <p><strong>Edit:</strong></p>
  <p>Correct method:</p>
  <pre><code>this.getAngle = function(){
      var angle = Math.atan2(this.y, this.x);
      var degrees = 180 * angle / Math.PI;
      return (360 + Math.round(degrees)) % 360;
  }
  </code></pre>