Python atan2

The Python atan2 function returns the angle (in radius) from the X-Axis to the specified point (y, x). In this section, we discuss using the atan2 function with an example.

The syntax of the atan2 Function in Python Programming Language is

math.atan2(y, x);
  • X: It can be a number or a valid numerical expression that represents Cartesian X – Coordinate.
  • Y: It can be a number or a valid numerical expression representing Cartesian Y – Coordinate.

HINT: If we pass any non-numeric values to the atan2 Function, then it returns TypeError as output

Python atan2 Function Example

The atan2 Function in this language returns the angle (in radius) from the X-Axis to the specified point (y, x). In this atan2 example, We are going to find the same with different data types and display the output.

TIP: Please refer to the tan article to understand the Python Tangent Function.

# Example

import math

Tup = (1, 2, 3, -4 , 5)
Lis = [-1, 2, -3.5, -4 , 5]

print('Tangent value of Positive Number = %.2f' %math.atan2(2, 4))
print('Tangent value of Negative Number = %.2f' %math.atan2(-1, 6))

print('Tangent value of Tuple Item = %.2f' %math.atan2(Tup[3], Tup[2]))
print('Tangent value of List Item = %.2f' %math.atan2(Lis[2], Lis[4]))

print('Tangent value of Multiple Number = %.2f' %math.atan2(2 + 7 - 4, 9-5))

print('Tangent value of String Number = %.2f', math.atan2('Hello', 'Python'))
Python ATAN2 Function

First, we used the atan2 Function directly on both the Positive integer and negative integer. The following statements find the angle (in radius) for the corresponding values.

print('Tangent value of Positive Number = %.2f' %math.atan2(2, 4))
print('Tangent value of Negative Number = %.2f' %math.atan2(-1, 6))

Next, we used the atan2 Function on Tuple and List items. If you see the above screenshot, it is working perfectly on them.

print('Tangent value of Tuple Item = %.2f' %math.atan2(Tup[3], Tup[2]))
print('Tangent value of List Item = %.2f' %math.atan2(Lis[2], Lis[4]))

Next, we used the atan2 from the list of math functions on multiple values.

print('Tangent value of Multiple Number = %.2f' %math.atan2(2 + 7 - 4, 9-5))

Within the last statement, we tried atan2 Function on the string value, and it returns TypeError as output.

print('Tangent value of String Number = %.2f', math.atan2('Hello', 'Python'))

Python atan2 to find the distance between two points

As we all know, the math atan2() function is useful for calculating the angle between two points. However, to find the distance, we must use the hypot function.

In the following example, we declare the x and y coordinates of two points. Next, we used the atan2() function to find the angle in radians between them. Next, the hypot function returns the distance between two points.

import math

x1, y1 = 1, 2
x2, y2 = 10, 18

dx = x2 - x1
dy = y2 - y1

angle = math.atan2(dy, dx)
distance = math.hypot(dx, dy)

print("Angle in Radians:", angle)
print("Distance between two Points:", distance)
Angle in Radians: 1.0584068664841588
Distance between two Points: 18.35755975068582

Does math.atan2 returns degrees or radians?

By default, the atan2() function returns the angle in radians. If you want to return the result (angle between two points) in degrees, use the built-in degrees() function.

import math

x = math.atan2(2, 4)

print("Angle in Radians:", x)
print("Angle in Degrees:", math.degrees(x))
Angle in Radians: 0.4636476090008061
Angle in Degrees: 26.56505117707799

How to get a 0 to 360-degree angle from Python atan2?

By default, the atan2() function follows the table below to display the results. If both the parameter values are positive, it returns an angle between 0 and 90.

yxQuadrantAngle (range)
++10° to 90°
+290° to 180°
3-180° to -90°
+4-90° to 0°

In the following example, we use the atan2() function with negative x and negative y. It means the angle should fall between -180° and -90°.

import math

x = -3
y = -4

angle = math.atan2(y, x)
print(angle)
print(math.degrees(angle))
-2.214297435588181
-126.86989764584402

If you want the angle to be between 0 and 360, use the if statement code below.

import math

x = -3
y = -4

angle = math.degrees(math.atan2(y, x))
if angle < 0:
angle += 360

print(angle)
233.13010235415598