- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <unordered_multiset>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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