eso no es cosa de programacion sino de MATEMATICAS
Math.atan2()
Method; computes and returns the arc tangent of y/x in radians. The return value represents the angle opposite the opposite angle of a right triangle, where x is the adjacent side length and y is the opposite side length.
Math.atan()
Method; computes and returns the arc tangent for the number specified in the parameter x. The return value is between negative pi divided by 2, and positive pi divided by 2.
Esa es la razon por la que no te funciona con atan