Skip to content Skip to sidebar Skip to footer

How To Convert From Atan To Atan2?

In Python, if one wanted to create the effect of atan2() from the atan() function, how would one go about this? For example, if I had cartesian coordinates (x, y) and wanted to fin

Solution 1:

In a nutshell, math.atan only works for quadrants 1 and 4. For quadrants 2 and 3 (i.e. for x < 0), then you'd have to add or subtract pi (180 degrees) to get to quadrants 1 and 4 and find the respective value for the theta. In code, it means that

if (y < 0and x < 0): 
    print(math.atan(y/x) - math.pi)
elif (y > 0and x < 0): 
    print(math.atan(y/x) + math.pi)

For more reference, take a look at atan2. The other two use cases are those in which x > 0, which works well with atan.

Post a Comment for "How To Convert From Atan To Atan2?"