About atan2... you got one detail wrong. In a mathematical coordinate system, the angle for (x,y) with regard to the positive x axis is atan(y/x), not atan(x/y).
By the way, you can imagine atan2 to be the same as this, except that atan2 can also handle the cases where x and/or y is 0:
alpha=atan(y/x)+below(x,0)*acos(1);
