math.atan2()

Type Function
Library math.*
Return value Number
Revision Release 2024.3703
Keywords atan2, arc tangent
See also math.atan()
math.tan()

Overview

Returns the arc tangent of y/x (in radians), but uses the signs of both parameters to find the quadrant of the result. It also correctly handles the case of x being 0.

The result will be in the interval [-pi,+pi] radians.

This function is useful when converting rectangular coordinates to polar coordinates. math.atan2() uses the sign of both arguments to place the result into the correct quadrant, and also produces correct values when one of its arguments is 0 or very close to 0.

Syntax

math.atan2( y, x )
y (required)

Number. A number representing the y coordinate.

x (required)

Number. A number representing the x coordinate.

Examples

print(math.atan2(1, 0))     -->  pi/2
print(math.atan2(-1, 0))    -->  -pi/2
print(math.atan2(0, 1))     -->  0
print(math.atan2(0, -1))    -->  pi