In this Linux Hint article, we will show you how to use the atan2() function to find the arc tangent of the y and x coordinates.
We will discuss the theoretical part of the function, its syntax, the input and output arguments, and the type of data that each of them accepts.
Then, we will apply what we learned with a practical example that includes code snippets and images that show the step by step process on how to find the arc tangent of a coordinate using the atan2() function in the C language.
Atan2() Function Syntax in C Language
Atan2() Function Description in C Language
While the atan() function returns the arc tangent of y, the atan2() function returns the arc tangent or inverse tangent of the y/x coordinates in intervals of -π/2 and π/2, so that the returned result is expressed in radians.
The atan2() function is part of the C math library and is defined in the “math.h” header. Its use must be previously included in our “.c” code or in the “.h” header” with the following statement:
Once the “math.h” header is included in our code, we can use the atan() function and the accompanying asinl() and asinf() functions provided by the C math library.
This function is located in the “libm” library or in the math library. You need to call it in the compile command with the following “-lm” command in gcc.
How to Obtain the Arc Tangent of the Y and X Coordinates with the Atan2() Function in the C Language
In this example, we will see step by step on how to define the necessary variables and constants. Then, obtain the arc tangent of the y and x coordinates with the atan2() function.
First, we must include all the necessary headers in our code to make use of these functions.
#include <math.h>
void main ()
{
//…
}
Once the libraries are included, we must declare the Double type variables for y, x, and arc_tan where the atan2() function returns its result.
The coordinates that we will use for this example are x = -5 and y =5.
#include <math.h>
void main ()
{
double x, y, arc_tan;
x = -5;
y = 5;
}
To obtain the arc tangent of the x and y coordinates, we now call the atan2() function, passing the previously defined coordinates as input argument and arc_tan as output argument.
We then use the printf() function to output the result to the command console:
The following image shows the result of compiling and running this code. You can see in it the result of the tangent of the arc in radians which is calculated using the x and y coordinates:
How to Obtain the Arc Tangent Expressed in Degrees with the Atan2() Function in the C Language
The mathematical functions solve the calculations in C which is related to angle measurements using radians as the unit of measure. In general, the technical data sheets components in all fields contain the formulas for the calculations in degrees, so it is more convenient to convert the results of these functions from radians to degrees.
Here is a convenient method to get the result of the atan2() function in degrees. The formula for this conversion is as follows:
To get the result of the arc tangent of a coordinate in degrees, we define a macro asin_deg in the head of our code that calls the atan2() function and multiplies its result by 180 / 𝝿, as shown in the following:
In this way, we converted the radians to degrees with a simple calculation and displayed the result on the screen with this unit of measurement.
The following image shows the result of the calculation of the previous example in degrees:
Conclusion
In this Linux Hint article about the C language, we showed you how to get the inverse tangent of arc tangent of the x and y coordinates with the atan2() function. We looked at the theoretical framework of this function and explored the syntax for the output arguments and the data type that it processes. We also showed you how to convert the results which are returned by this function into the unit of degrees. We hope that you found this article useful. For more articles like this one about language, use the search engine on our website.