28

I want to reverse a sin/cos operation to get back an angle, but I can't figure out what I should be doing.

I have used sin and cos on an angle in radians to get the x/y vector as such:

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

Given s_x and s_y, is it possible to get back the angle? I thought atan2 was the function to use, but I'm not getting the expected results.

4

5 に答える 5

29

atan2(s_y, s_x)あなたに正しい角度を与えるはずです。との順序を逆にした可能性がs_xありs_yます。acosまた、と関数をそれぞれとでasin直接使用できます。s_xs_y

于 2012-12-29T06:05:50.553 に答える
10

I use the acos function to get back the angle from the given s_x cosinus. But because several angles may result to the same cosinus (for example cos(+60°) = cos(-60°) = 0.5), it's not possible to get back the angle directly from s_x. So I also use the sign of s_y to get back the sign of the angle.

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

for the specific case of (s_y == 0), it does not matter to take +acos or -acos because it means the angle is 0° (+0° or -0° are the same angles) or 180° (+180° or -180° are the same angles).

于 2014-05-23T10:38:00.207 に答える
2

数学では、sinとcosの逆演算です。これはarcsinとarccosです。使用しているプログラミング言語がわかりません。しかし、通常、cosとsin関数がある場合は、逆関数を持つことができます。

于 2012-12-29T06:07:15.430 に答える
2

asin(s_x), acos(s_y), perhaps, if you are using c.

于 2012-12-29T06:09:56.330 に答える
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;
}
于 2021-04-08T16:59:44.153 に答える