从sin/cos转换中获取角度

30

我希望能够通过反转sin/cos运算来获取角度,但是我不知道应该怎么做。

我已经使用弧度制下的 sincos 函数得到了 x/y 向量,如下所示:

double angle = 90.0 * M_PI / 180.0;  // 90 deg. to rad.
double s_x = cos( angle );
double s_y = sin( angle );

已知 s_xs_y,能否计算出对应的角度呢?我认为应该使用函数 atan2,但是结果不如预期。

5个回答

34

atan2(s_y, s_x) 应该可以给你正确的角度。也许你把s_xs_y的顺序搞反了。此外,你还可以直接在s_xs_y上使用acosasin函数。


1
我的x/y确实被颠倒了,因为我有一些代码将sin分配给了x,而其他一些位则将sin分配给了y。 - Eric Fortier

12

我使用 acos 函数从给定的 s_x 余弦值获取角度。但是,因为几个角度可能会对应相同的余弦值(例如cos(+60°)=cos(-60°)=0.5),所以不可能直接从s_x获取角度。因此,我还使用 s_y 的符号 来获取角度的符号。

// Java code
double angleRadian = (s_y > 0) ? Math.acos(s_x) : -Math.acos(s_x);
double angleDegrees = angleRadian * 180 / Math.PI;

对于(s_y == 0)这种特殊情况,无论是取+acos还是-tacos都无所谓,因为它表示的角度为0°(+0°和-0°是相同的角度)或者180°(+180°和-180°也是相同的角度)。


3

在数学中,sin和cos有相应的反函数,分别为arcsin和arccos。我不知道你使用的是哪种编程语言,但通常只要有cos和sin函数,就一定会有相应的反函数。


2

asin(s_x), acos(s_y),如果你在使用C语言,那么这些函数可能会用到。


1
double angle_from_sin_cos( double sinx, double cosx ) //result in -pi to +pi range
{
    double ang_from_cos = acos(cosx);
    double ang_from_sin = asin(sinx);
    double sin2 = sinx*sinx;
    if(sinx<0)
    {
        ang_from_cos = -ang_from_cos;
        if(cosx<0) //both negative
            ang_from_sin = -PI -ang_from_sin;
    }
    else if(cosx<0)
        ang_from_sin = PI - ang_from_sin;
    //now favor the computation coming from the
    //smaller of sinx and cosx, as the smaller
    //the input value, the smaller the error
    return (1.0-sin2)*ang_from_sin + sin2*ang_from_cos;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接