I want to calculate the angle a line makes with the positive x-axis in a clockwise direction. It's a lot like a bearing except instead of North, I want the angle it makes with positive x-axis. The image illustrates what I'm after.
Below is the code I wrote to achieve this. It works fine but I am just wondering if there's a way to reduce all these if statements.
ActionScript Code:
/**
* @return Bearing, in radians, of <code>p2</code> from <code>p1</code>
*/
public static function bearing(p1:Point, p2:Point):Number
{
var dy:Number = p2.y - p1.y;
var dx:Number = p2.x - p1.x;
var theta:Number = Math.atan(dy/dx);
if (theta < 0)
if (p2.y > p1.y)
theta += Math.PI;
else
theta += MathHelper._2_PI;
else if (p2.y < p1.y)
theta += Math.PI;
return theta;
}
Edit: MathHelper._2_PI = 2 * Math.PI