Calculating the angle between the horizontal and a line through two points
<p>A JavaScript question.</p>

<p>Below is a routine that seems to have some problems. What is the problem? The function, given two points, is supposed to return the angle (in radians) formed between the horizontal axis and the line containing the two points (X1,Y1) and (X2,Y2).</p>

<pre><code>function GetAngle(X1, Y1, X2, Y2) {
    if (Y2 == Y1) {
        return (X1 &gt; X2) ? Math.PI : 0; 
    }
    if (X2 == X1) {
        return (Y2 &gt; Y1) ? Math.PI/2 : 1.5*Math.PI;
    }
    var tangent = (X2 - X1) / (Y2 - Y1);
    var ang = Math.atan(tangent);
    if (Y2-Y1 &lt; 0) ang -= Math.PI;
    return ang;
}
</code></pre>
