【问题标题】:Getting angle back from a sin/cos conversion从 sin/cos 转换中恢复角度
【发布时间】:2022-01-17 07:44:11
【问题描述】:

我想反转 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 是要使用的函数,但我没有得到预期的结果。

【问题讨论】:

    标签: math angle trigonometry


    【解决方案1】:

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

    【讨论】:

    • 我的 x/y 确实被颠倒了,因为我有一些代码将 sin 分配给 x,而其他一些位将 sin 分配给 y。
    【解决方案2】:

    我使用 acos 函数从给定的 s_x cosinus 中获取角度。但是因为多个角度可能导致相同的余弦(例如 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或-acos无关紧要,因为这意味着角度是0°(+0°或-0°是相同的角度)或180° (+180° 或 -180° 是相同的角度)。

    【讨论】:

      【解决方案3】:

      在数学中是 sin 和 cos 的逆运算。这是 arcsin 和 arccos。 不知道你用的是什么编程语言。但通常如果它具有 cos 和 sin 函数,那么它可以具有反向函数。

      【讨论】:

        【解决方案4】:

        asin(s_x)、acos(s_y),也许,如果你使用的是 c。

        【讨论】:

          【解决方案5】:
          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;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-11-11
            • 1970-01-01
            • 2015-04-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多