Search code examples
c++trigonometryangle

How to get angle based on trigonometric function in C++


I know I can get given angle's trigonometric functions by math.h. I was wondering, if it can be done in reversed direction.

Let's say there's a vector with two coordinates. I have its x and y values, so tangent can be easily calculated

float tg;
if(x != 0)
{
    tg = y/x
}
else
{
    //vector is vertical and it doesn't have a tangent
    //handle this somehow
}

However, I see no way of doing it reversely. I need a way to get angle between 0 and 2pi based on it's tangent.


Solution

  • Use the function angle = atan2(y, x) Here's a link to more info: http://www.cplusplus.com/reference/cmath/atan2/

    OR

    http://en.cppreference.com/w/cpp/numeric/math/atan2