Skip to main content
edited tags
Link
200_success
  • 144.1k
  • 22
  • 188
  • 473
Source Link
Jack Wilsdon
  • 1.6k
  • 3
  • 21
  • 36

Best way to work out angle between points?

I have the following JavaScript code to work out the angle between two points (clockwise). The code I have seems a bit "hacky" to me, as I have a while loop ensuring the number is not negative (to make the range 0-360), as well as I have to subtract 270 degrees to get it to work correctly. Is there a better way to do this?

this.angle = function(point)
{
    var delta = point.subtract(this);
    var aR = Math.atan2(delta.y, delta.x);
    var aD = aR * (180 / Math.PI);
    aD -= 270;
    while (aD < 0)
    {
        aD += 360;
    }
    return aD;
};