i'd love to draw it here but i can't... the divide by zero was a potential issue
if there ever was a divide by zero but i think that would just crash.
the actual problem was inherent in atan:
imagine dividing 360 degress into four quadrants. in the first and third quadrant
tan a (where 'a' is an angle) is positive, and in the second and fourth quadrants
tan a is negative. the trouble is, there exists
tan b where
b is in the third quadrant that is equal to
tan a where
a is in the first quadrant (and same deal for second and fourth quadrants). so when you do
atan x, there are always two possible angles it can be. the
atan function resolves this by dealing only in two quadrants, so +-90 degrees.
atan2 still deals with
one number (x/y) but based on the signs of x and y it can figure out which quadrant the angle is in out of all four quadrants, giving you +-180 degree range.
so basically your code would work fine for perhaps half the angles, and then do the exact opposite for the others until you changed to atan2.
i hope that makes sense
julz