Proper Trigonometry For Rotating A Point Around The Origin

心不动则不痛 提交于 2019-11-28 10:53:34

It depends on how you define angle. If it is measured counterclockwise (which is the mathematical convention) then the correct rotation is your first one:

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

But if it is measured clockwise, then the second is correct:

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

From Wikipedia

To carry out a rotation using matrices the point (x, y) to be rotated is written as a vector, then multiplied by a matrix calculated from the angle, θ, like so:

where (x′, y′) are the co-ordinates of the point after rotation, and the formulae for x′ and y′ can be seen to be

This is extracted from my own vector library..

//---------------------------------------------------------------------------------- // 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; } 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!