Why don't you use Math.atan2 , which is more convenient. It automatically does this correctly when both numbers are negative (what information is lost during division) and returns the correct values ββfor edge cases.
var angle = Math.atan2(Y2 - Y1, X2 - X1); // these return differently, even though 0 / -1 === 0 / 1 Math.atan2( 0, -1); // Math.PI Math.atan2( 0, 1); // 0 // same thing: 1 / 1 === -1 / -1 Math.atan2( 1, 1); // Math.PI / 4 Math.atan2(-1, -1); // -Math.PI * 3 / 4 // other values Math.atan2( 1, 1); // Math.PI / 4 Math.atan2( 1, 0); // Math.PI / 2 Math.atan2(-1, 0); // -Math.PI / 2
source share