Selected Reading

C++ cmath atan2() Function



The C++ cmath atan2() function calculates the angle in radians between the positive x-axis and a line from the origin (0, 0) to the point (x, y). It takes two arguments: the y-coordinate first and the x-coordinate second.

This function returns a floating-point number representing the angle in radians, ranging from - to . This function is especially useful for finding angles based on two-dimensional Cartesian coordinates.

Syntax

Following is the syntax for C++ cmath atan2() function.

double atan2(double y, double x);
or
float atan2(float y, float x);
or
long double atan2(long double y, long double x);

Parameters

  • Y - Value representing the proportion of the y-coordinate.

  • X - Value representing the proportion of the x-coordinate.

Return Value

This function returns the principal value of the arc tangent of y/x, expressed in radians.

Time Complexity

The time complexity of this function is constant, i.e.,O(1).

Example 1

In this example, we will find the angle between the positive x-axis and a point in a Cartesian coordinate system. This angle shows the direction of the point relative to the horizontal axis.

#include <iostream>
#include <cmath>
int main() {
    double x = 3.0, y = 5.0;
    std::cout << "Angle of point (3,5): " << std::atan2(y, x) << " radians" << std::endl;
    return 0;
}

Output

Output of the above code is as follows

Angle of point (3,5): 1.03038 radians

Example 2

Let's calculate the angle for a point in each quadrant, showing how the C++ cmath atan2() function accurately determines the angle based on the signs of x and y.

#include <iostream>
#include <cmath>
int main() {
    double x1 = 1.0, y1 = -1.0; 
    double x2 = -1.0, y2 = 1.0;
    std::cout << "Angle of point (1, -1): " << std::atan2(y1, x1) << " radians" << std::endl;
    std::cout << "Angle of point (-1, 1): " << std::atan2(y2, x2) << " radians" << std::endl;
    return 0;
}

Output

Following is the output of the above code

Angle of point (1, -1): -0.785398 radians
Angle of point (-1, 1): 2.35619 radians

Example 3

In the following example, we are going to use std::atan2() in a loop, and calculating the angles for various points around a circle. This shows how std::atan2() determines the angle accurately for each point based on the coordinates.

#include <iostream>
#include <cmath>
int main() {
    double radius = 1.0;
    for (int i = 0; i <= 6; ++i) {
        double x = radius * std::cos(i * M_PI / 3);
        double y = radius * std::sin(i * M_PI / 3);
        std::cout << "Angle of point (" << x << ", " << y << "): " << std::atan2(y, x) << " radians" << std::endl;
    }
    return 0;
}

Output

If we run the above code it will generate the following output

Angle of point (1, 0): 0 radians
Angle of point (0.5, 0.866025): 1.0472 radians
Angle of point (-0.5, 0.866025): 2.0944 radians
Angle of point (-1, 1.22465e-16): 3.14159 radians
Angle of point (-0.5, -0.866025): -2.0944 radians
Angle of point (0.5, -0.866025): -1.0472 radians
Angle of point (1, -2.44929e-16): -2.44929e-16 radians
cpp_cmath.htm
Advertisements