如果果一个点(x1,y1)旋转到(x2,y2),对应的角度旋转从θ1到θ1+θ2
sinθ1=y1/sqrt(x1*x1+ y1*y1)
cosθ1=x1/sqrt(x1*x1+ y1*y1)
sin(θ1+θ2)=sin(θ1)*cos(θ2)+cos(θ1)*sin(θ2)=y2/sqrt(x2*x2+y2*y2)
cos(θ1+θ2)=cos(θ1)*cos(θ2)-sin(θ1)*sin(θ2)=x2/sqrt(x2*x2+y2*y2)
由于sqrt(x1*x1+ y1*y1) = sqrt(x2*x2+y2*y2)
所以由上述推到出
y2=x1*sin(θ2)+y1*cos(θ2);
x2=x1*cos(θ2)-y1*sin(θ2);
/* ptBase:旋转的基点 ; pts[] 旋转的数组; nCount点数目
*/
void RotatePoints(const POINT& ptBase,POINT pts[],long nCount,double angle,BOOL isUseDegree)
{
if ( fabs(angle) < 1e-6) return;
if ( isUseDegree)//把度数换算成弧度
{
angle = angle /180 * PI;//PI = 3.1415
}
double cose = cos(angle);
double sine = sin(angle);
double dx(0.0),dy(0.0);
for(int i=0;i<nCount; ++i)
{
dx = pts[i].x - ptBase.x;
dy = pts[i].y - ptBase.y;
double x = dx * cose - dy * sine + ptBase.x;
double y = dx * sine + dy * cose + ptBase.y;
pts[i].x = x;
pts[i].y = y;
}
}