以下哪种方法都使用正确的数学来旋转一个点?如果是这样,哪一个是正确的?
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;
}
答案 0 :(得分:27)
答案 1 :(得分:22)
这取决于您如何定义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;
答案 2 :(得分: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;
}