atan2() function in C
C's function atan2() function is used to calculate the angle between the two lines. The two lines are the ray from the origin to the given point containing the x and y coordinates using the arctagent function and returning the angle in radians.
The atan2 function first appeared in the setup accent Fortran in 1961. It was initially destined to return a correct and clear profit for the angle. The atan2() function handles the sign conventions and quadrant adjustments automatically, making it more suitable for applications where the angle needs to be calculated based on the coordinates of a point.
Using the atan2() function, you can avoid some of the complexities and ambiguities that arise when using only the atan() function. It simplifies calculating angles in a coordinate system and provides more accurate results by considering both the x and y coordinates. This atan2() function is present in the math library because it is an inbuilt function.
Syntax:
It has the following syntax:
#include <math.h>
// function with two arguments as point coordinates and return a double value of angle
// y coordinate, x coordinate
double atan2(double y, double x);
Select a point or coordinate (x, y) as the angle measure using the arctangent function. Incompletely individual cases for the x principles and individual cases for the negative or individual coordinate of nothing must be handled by mathematical formulas or calculation rules. In experimental estimating, finding angle measurements and converting cartesian to cold-related are average tasks, and it is possible that this rule has to be changed and adjusted.
To resolve this problem, computer science education became familiar with the atan2 function as early as the 1960s' Fortran IV dialect. The angle between the x-pivot and a direction from the origin to a point (x, y) anywhere in the Cartesian plane is measured by the quota atan2(y,x). The multivalued function Arctan(y/x)'s correct arm is chosen by choosing one of the four equally sized portions of the result based on the signs of x and y.
Parameters: y coordinate, x coordinate
Returntype: double
Program for the atan2() function in C:
#include<stdio.h>
#include<math.h>
double convertRadiansToDegrees(double radians) {
//This function is used to convert the radians to degree
double degree = radians * (180.0 / 3.14159265358979323846);
// returning the degree value by taking the radian value
return degree;
}
int main(){
// angles provided are measured counterclockwise from the positive x-axis
// point A
// taking a point in the first quadrant
int x1=2,y1=3;
// angle in radian and degrees
double angle1 = atan2(y1,x1);
printf("angle in radians %f for the point in first quadrant \n",angle1);
printf("angle in degree %f for the point in first quadrant \n",convertRadiansToDegrees(angle1));
// point B
// taking a point in the second quadrant
int x2=-4,y2=1;
// angle in radian and degrees
double angle2 = atan2(y2,x2);
printf("angle in radians %f for the point in second quadrant \n",angle2);
printf("angle in degree %f for the point in second quadrant \n",convertRadiansToDegrees(angle2));
// point C
// taking a point in the third quadrant
int x3=-1,y3=-5;
// angle in radian and degrees
double angle3 = atan2(y3,x3);
printf("angle in radians %f for the point in third quadrant \n",angle3);
printf("angle in degree %f for the point in third quadrant \n",convertRadiansToDegrees(angle3));
// point D
// taking a point in the fourth quadrant
int x4=6,y4=-2;
// angle in radian and degrees
double angle4 = atan2(y4,x4);
printf("angle in radians %f for the point in fourth quadrant \n",angle4);
printf("angle in degree %f for the point in fourth quadrant \n",convertRadiansToDegrees(angle4));
}
Output:
angle in radians 0.982794 for the point in first quadrant
angle in degree 56.309932 for the point in first quadrant
angle in radians 2.896614 for the point in second quadrant
angle in degree 165.963757 for the point in second quadrant
angle in radians -1.768192 for the point in third quadrant
angle in degree -101.309932 for the point in third quadrant
angle in radians -0.321751 for the point in fourth quadrant
angle in degree -18.434949 for the point in fourth quadrant
Explanation of the program:
Point A: (2, 3)
Quadrant: First Quadrant (Q1)
Explanation: This point is located in the positive x-axis and y-axis regions. Both a and b coordinates are positive.
Point B: (-4, 1)
Quadrant: Second Quadrant (Q2)
Explanation: This point is located in the negative x-axis and positive y-axis region. The a coordinate is negative, and the b coordinate is positive.
Point C: (-1, -5)
Quadrant: Third Quadrant (Q3)
Explanation: This point is located in the negative x-axis and y-axis regions. Both the a and b coordinates are negative.
Point D: (6, -2)
Quadrant: Fourth Quadrant (Q4)
Explanation: This point is located in the positive x-axis and negative y-axis region. The a coordinate is positive, and the b coordinate is negative.
In a Cartesian coordinate system, the four quadrants are defined as follows:
First Quadrant (Q1): Positive x-axis and positive y-axis region.
Second Quadrant (Q2): Negative x-axis and positive y-axis region.
Third Quadrant (Q3): Negative x-axis and negative y-axis region.
Fourth Quadrant (Q4): Positive x-axis and negative y-axis region.
The result is in radians and degrees of the angles of the points in all coordinates.