What is atan2 function?
The Atan2 function computes the arc tangent, also known as the inverse tangent, of the y/x coordinates within the range of -π/2 to π/2. This function determines the angle in radians for the specified quadrant, and the output is provided in radians. In contrast, the atan function computes the arc tangent solely based on the y-coordinate.
The atan2 function can be found in the "math.h" header file within the C math library. Prior to utilizing it in our code, it is essential to declare its use in either the ".c" file or the ".h" header file with the following statement:
#include <math.h>
We can make use of the atan function along with the corresponding asinl and asinf methods provided by the C math library once we include the "math.h" header in our code. Both the math library and the "libm" library include this particular function. When compiling, it is necessary to invoke the "-lm" flag in the gcc compile command.
~$ gcc Documents/name.c -lm -o out
Syntax of Atan2 function:
It has the following syntax:
double atan2 ( double y, double x );
Here, we discussed the parameters of the syntax:
Double x: It represents the X coordinate.
Double y: It represents the Y coordinate.
How to use the Atan2 function to find the Arc Tangent of the Y and X Coordinates
In this instance, we will demonstrate the process of declaring the correct constants and variables incrementally. Subsequently, we will employ the atan2 function to calculate the arctangent of the vertical and horizontal coordinates.
To utilize these functions, we need to incorporate all necessary headers in our code.
#include <stdio.h>
#include <math.h>
void main ()
{
//...
}
We need to define variables of type Double for y, x, and arc_tan, with the atan2 function returning the calculated value after importing the necessary libraries. In this instance, the values for the coordinates are x = -6 and y = 6.
#include <stdio.h>
#include <math.h>
void main ()
{
double x, y, arc_tan;
x = -6;
y = 6;
}
Next, we invoke the atan2 function, providing the coordinates defined earlier as the input parameter, and arc_tan as the output variable to calculate the arc tangent of the x and y coordinates. Subsequently, the printf function is employed to display the result on the console:
#include <stdio.h>
#include <math.h>
void main ()
{
double x, y, arc_tan;
x = -6;
y = 6;
arc_tan = atan2 ( y, x );
printf("The arc tangent of y,x = %f\n", arc_tan);
}
Output:
The arc tangent of y,x = 2.356194
How to use the Atan2 function to get the Arc Tangent Expressed in Degrees
In the C programming language, calculations involving angles measured in radians are commonly performed. Converting the results of these calculations from radians to degrees is often preferred, as technical documentation across various fields typically presents formulas in degrees for ease of understanding.
Here is a fast method to convert the output of the atan2 function to degrees. The conversion equation is outlined below:
Degrees = radians * (180 / ?)
In the code snippet below, we create a macro named asin_deg at the beginning of the program. This macro invokes the atan2 function and then calculates the product of the returned value and 180 divided by π to derive the arc tangent result in degrees:
#include <stdio.h>
#include <math.h>
#define pi 3.14159265
void main ()
{
double x, y, deg, arc_tan;
x = -6;
y = 6;
arc_tan = atan2 ( y, x );
deg = arc_tan * 180 / pi;
printf("The arc tangent in degrees of y, x is = %f\n", deg);
}
Output:
The arc tangent in degrees of y, x is = 135.000000
In this way, we executed a rapid calculation to change radians into degrees and subsequently displayed the result in this format on the monitor.
Conclusion:
In this tutorial for the C programming language, we showcased the usage of the atan2 function to calculate the arctangent of the x and y coordinates. We explored the structure of the return values, the data type handled by this function, and the fundamental concepts behind its operation. Furthermore, we illustrated the conversion of the function's results into degrees.