Python atan2() Function
Overview
Python atan2() method is an equivalent of the mathematical value of . It is called an arctangent which belongs to inverse trigonometric functions. The result of arctangent is the angle between the axis in a 2D plane and a line drawn from the origin to a coordinate (x, y) in that plane.
Python atan2() Function
Python atan2() method is used to calculate the arctangent of y/x in radians, where x and y are the coordinates of the point (x, y) in a plane. In simple terms, atan2() calculates the inverse of the tangent value. The atan2() method is provided by the math package.
For , the arctangent would be
Syntax of Python atan2() Function
The syntax of Python atan2() function is:
Parameters of Python atan2() Function
The Python atan2() method accepts two parameters x and y, which are the coordinates of the point (x, y)
Return Value of Python atan2() Function
The Python atan2() method returns a float value which is the arctangent of y/x in radians. The output of the atan2() value ranges from -π to π
Exceptions of Python atan2() Function
Python atan2() method throws the TypeError exception if the value passed for x or y is not a real number.
The above method throws the exception
Working of atan2() Function in Python
Let's understand the working of the atan2 method with an example. Consider a point (3, 3) in a 2D plane and connect the point to the origin and the x-axis using a line. Now, we will see a right-angled triangle drawn on the plane as below.
The triangle will have the base and height of size 3 units. Since it is a right-angled triangle, the angle (θ) between the triangle's base and the hypotenuse is . Let's find the tan value using the below well-known formula:
Now we know that . If we want to find the arctangent (i.e.) , the formula will be transformed into
The value of can be computed using Python atan2(y,x) method. We will get the value 0.78539816 when we compute arctangent of (3, 3) using Python atan2(3, 3) method. 0.78539816 is the radians equivalent of .
Let's confirm whether we get the above value using Python atan2() method.
The output of the above code is:
Practical Applications of atan2() Function
One simple real-world application of the atan2() function is to find the angle of elevation and angle of depression. The angle of elevation is the angle between the horizontal plane and diagonal line from the observer’s eye to an object above the eye. In contrast, the angle of depression is the angle between the horizontal plane and diagonal line from the observer's eye to an object below the eye.
We can find the angle () of elevation and depression if we know the coordinates (x, y) of the objects with respect to the eye level (x-axis).
Python atan2() Function examples
Python atan2() for Positive Numbers
In this example, we pass two positive numbers for the value of x and y
Output
Python atan2() for Negative Numbers
In this example, we pass two negative numbers for the value of x and y
Output
Python atan2() for Irrational Numbers
In this example, we pass two irrational numbers ( and ) for the value of x and y
Output
Conclusion
- Python atan2() method is used to calculate the arctangent of y/x in radians.
- Python atan2() calculates the inverse of the tangent value.
- Python atan2() method is provided by the math package.
- atan2() can be calculated for positive, negative, rational, and irrational numbers.
- atan2() is used in the real world to calculate the angle of elevation and depression.