C++ 从弧度转换为度数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40776703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting from Radians to Degrees
提问by Lucy Loo
I'm building a small Physics engine and I'm having trouble converting my Radian value to Degrees using atan
, as I need an angle to output in Degrees only.
我正在构建一个小型物理引擎,但无法使用 将弧度值转换atan
为度数,因为我只需要一个角度以度数输出。
Firstly, I have an x
and y
value, and I need to find an angle using atan
, so I divide y by x like so:
首先,我有一个x
andy
值,我需要使用 找到一个角度atan
,所以我像这样将 y 除以 x:
angleDivide = yN / xN;
Then, before putting this value into tan
, I attempt to convert it to Degrees like this:
然后,在将此值放入 之前tan
,我尝试将其转换为度数,如下所示:
angleToDegrees = angleDivide * (3.14 / 180);
Then I place angleToDegrees into atan
:
然后我将angleToDegrees 放入atan
:
angle = atan(angleToDegrees);
But when I'm displaying angle
, I'm, still getting radian values.
但是当我显示时angle
,我仍然得到弧度值。
Please could you tell me what is wrong with my code and how to fix this?
请你告诉我我的代码有什么问题以及如何解决这个问题?
回答by anthonybell
You want to calculate radians=tan(y/x)
first.
你想先计算radians=tan(y/x)
。
Then you can convert it to degrees:
然后您可以将其转换为度数:
radians = atan(y/x)
degrees = radians * (180.0/3.141592653589793238463)
See the reference herefor atan:
On a side note, you also have to take into account what quadrant you are in to get the correct answer (since -y/x
is the same number as y/-x
)
附带说明一下,您还必须考虑您所在的象限才能获得正确答案(因为-y/x
与 相同y/-x
)