用于旋转原点周围点的正确三角法

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

POINT rotate_point(float cx,float cy,float angle,POINT p) { float s = sin(angle); float c = cos(angle); // translate point back to origin: px -= cx; py -= cy; // Which One Is Correct: // This? float xnew = px * c - py * s; float ynew = px * s + py * c; // Or This? float xnew = px * c + py * s; float ynew = -px * s + py * c; // translate point back: px = xnew + cx; py = ynew + cy; } 

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

 // This? float xnew = px * c - py * s; float ynew = px * s + py * c; 

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

 // Or This? float xnew = px * c + py * s; float ynew = -px * s + py * c; 

来自维基百科

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

http://sofzh.miximages.com/c%23/0ed0d28652a45d730d096a56e2d0d0a3.png

其中(x’,y’)是旋转后点的坐标,x’和y’的公式可以看作是

替代文字

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

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