如何在C++中基于三角函数得到角度

Mac*_*ban -1 c++ trigonometry angle

我知道我可以通过math.h获得角度的三角函数.我想知道,如果它可以在相反的方向完成.

假设有一个带有两个坐标的向量.我有它的x和y值,因此可以很容易地计算切线

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

但是,我认为没有办法反过来这样做.我需要一种基于它的切线来获得0到2pi之间的角度的方法.

Ken*_*del 6

使用此功能angle = atan2(y, x) 以下是更多信息的链接:http://www.cplusplus.com/reference/cmath/atan2/

要么

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

  • 我建议链接[Alan Stokes评论]中提出的参考文献(http://stackoverflow.com/questions/27749475/how-to-get-angle-based-on-trigonometric-function-in-c#comment43910258_27749475) (2认同)