Verifying Formulas of angle of a point form the origin (For All Quadrants)

29 Views Asked by At

i want to verify that is my formulas are correct or not for finding angle of a point form the origin.

1st => (Atan(y/x) * (180/PI))

2nd => 180 - (Atan(y/-x) * (180/PI))

3rd => 180 + (Atan(-y/-x) * (180/PI))

4th => 360 - (Atan(y/x) * (180/PI)

1

There are 1 best solutions below

1
On BEST ANSWER

Your formulas look ok. (except the 4th Quadrant, should be + sign)

But, instead of forcing positive atan argument, why not just do atan(y/x) ?

atan(y/x) principle angle is between $±\pi/2$

If you don't mind negative angles, 1st and the 4th Quadrant is covered.

Since period of $\tan x$ is $\pi$, for 2nd and 3rd Quadrant, just add or subtract $\pi$

If atan2(y, x) is available, it will sort out these details. If not, you can build your own.
see Charles Petzold's blog, Atan2 and Exceptions (and why there aren't any)

from math import atan, pi, copysign

def my_atan2(y, x):
    if x>0: return atan(y/x) 
    if x<0: return atan(y/x) + copysign(pi, y)
    if y: return copysign(pi/2, y)
    if copysign(1,x) < 0: x = pi
    return copysign(x, y)