【问题标题】:Proper Trigonometry For Rotating A Point Around The Origin围绕原点旋转点的正确三角法
【发布时间】:2010-07-02 00:57:37
【问题描述】:

以下任何一种方法是否使用正确的数学方法来旋转一个点?如果有,哪一个是正确的?

POINT rotate_point(float cx,float cy,float angle,POINT p)
{
  float s = sin(angle);
  float c = cos(angle);

  // translate point back to origin:
  p.x -= cx;
  p.y -= cy;

  // Which One Is Correct:
  // This?
  float xnew = p.x * c - p.y * s;
  float ynew = p.x * s + p.y * c;
  // Or This?
  float xnew = p.x * c + p.y * s;
  float ynew = -p.x * s + p.y * c;

  // translate point back:
  p.x = xnew + cx;
  p.y = ynew + cy;
}

【问题讨论】:

  • 我不太明白。 cx 和 cy 是什么?此外,您已经声明了 POINT 类型的函数,但它没有返回 POINT,或者实际上没有返回任何东西。
  • @Brian Hooper:+1 指出有意义的变量名的好处;)

标签: c# c++ geometry trigonometry


【解决方案1】:

From Wikipedia

要使用矩阵进行旋转,将要旋转的点 (x, y) 写为向量,然后乘以根据角度 θ 计算得出的矩阵,如下所示:

其中(x′,y′)为旋转后点的坐标,x′和y′的公式可见

【讨论】:

  • 不要忘记,如果您在典型的屏幕坐标空间中工作,您的 y 轴将从数学标准反转(向下是 +y,向上是 -y)并且您需要考虑到这一点。
【解决方案2】:

这取决于您如何定义angle。如果它是逆时针测量的(这是数学惯例),那么正确的旋转是你的第一个:

// This?
float xnew = p.x * c - p.y * s;
float ynew = p.x * s + p.y * c;

但如果是顺时针测量,那么第二个是正确的:

// Or This?
float xnew = p.x * c + p.y * s;
float ynew = -p.x * s + p.y * c;

【讨论】:

    【解决方案3】:

    这是从我自己的向量库中提取的..

    //----------------------------------------------------------------------------------
    // Returns clockwise-rotated vector, using given angle and centered at vector
    //----------------------------------------------------------------------------------
    CVector2D   CVector2D::RotateVector(float fThetaRadian, const CVector2D& vector) const
    {
        // Basically still similar operation with rotation on origin
        // except we treat given rotation center (vector) as our origin now
        float fNewX = this->X - vector.X;
        float fNewY = this->Y - vector.Y;
    
        CVector2D vectorRes(    cosf(fThetaRadian)* fNewX - sinf(fThetaRadian)* fNewY,
                                sinf(fThetaRadian)* fNewX + cosf(fThetaRadian)* fNewY);
        vectorRes += vector;
        return vectorRes;
    }
    

    【讨论】:

    • 您可以将cosfsinf 结果保存到变量中,以使用一半的触发函数调用。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 2015-12-23
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    相关资源
    最近更新 更多