Use the Math.atan2 function. It is like arctan but knows about x and y coordinates, so it can handles lines that are horizontal, vertical, or pointing in other directions -- arctan's range of -pi/2 through pi/2 will not give the correct answer for some lines.