在开始我的问题之前,我想指出几件事:1) 我知道 cmath 库中已经有一个 atan2 函数,这纯粹是为了我自己的练习,以及 2)我知道代码不占0。
好的,所以 tan(theta) = y/x,其中 y 和 x 是平面上的坐标...这意味着:
theta = atan(y/x) 在四边形 I 和 IV 中,以及 theta = atan(y/x) + 180 in Quads II 和 III
那么为什么当我使用以下代码时:
float atan(float y, float x)
{
float result = 0.0f;
if (x > 0) //quads I and IV if x is positive
{
result = atanf(y/x);
}
else if (x < 0)
{
result = atan(y/x) + 180; //quads II and III if x is negative
}
return result;
}
它吐我垃圾吗? 例如,对于坐标 (-4,4),它给了我结果:179.215,而它应该是 135:
atan(4/-4) = -45 度 + 180 度 = 135 度
但是正在发生的是计算
atan(4.0f/-4.0f) = -0.785398 + 180 度 = 179.215。
我是不是漏掉了一些步骤?
最佳答案
标准的 atan 和 atan2 函数,以及所有其他处理角度的 C 函数,处理的是弧度,而不是度数。
如果你想让自己的函数输出度数,你必须将atanf的返回值乘以180/pi
;要以弧度表示所有内容,请添加 pi 而不是 180。
关于c++ - c++中的atan问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11484692/