绕原点旋转点的正确三角学方法

16

以下两种方法使用了正确的数学方法来旋转一个点吗?如果是,哪一种方法是正确的?

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;
}

11
我不是一个翻译专家,但我会尽力帮助你。您的意思是什么?"cx"和"cy"是什么?另外,您声明了函数的类型为"POINT",但它并没有返回"POINT"或任何其他东西。 - Brian Hooper
1
@Brian Hooper:指出有意义的变量名称的好处,点赞!;) - Cogwheel
3个回答

33

来自维基百科

要使用矩阵进行旋转,需要将要旋转的点(x,y)写成向量形式,然后乘以从角度θ计算出的矩阵,如下所示:

https://upload.wikimedia.org/math/0/e/d/0ed0d28652a45d730d096a56e2d0d0a3.png

其中(x′,y′)是旋转后点的坐标,并且可以看到 x′ 和 y′ 的公式为

alt text


3
不要忘记,如果你在一个典型的屏幕坐标空间中工作,你的y轴将会与数学标准相反(向下为+y,向上为-y),你需要考虑到这一点。 - Daniel Bingham

26

这要看你如何定义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;

1

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

//----------------------------------------------------------------------------------
// 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;
}

4
你可以将 cosfsinf 的结果存储到变量中,以减少三角函数调用的数量。 :) - Justin Ardini

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