二维空间中,给定一个点与一个角度,求其绕另一个点旋转后的坐标。
公式如下,(x1,y1)为要转的点,(x2,y2)为中心点,
x=(x1-x2)cosθ-(y1-y2)sinθ+x2
y=(y1-y2)cosθ+(x1-x2)sinθ+y2
c++实现
void rotate(Point src, Point & dst, float angle) { float theta = angle / 180 * 3.1415926; int x1,x2,y1,y2; x1 = src.x; x2 = dst.x; y1 = src.y; y2 = dst.y; dst.x = (x1 - x2) * cos(theta) - (y1 -y2) * sin(theta) + x2; dst.y = (y1 - y2) * cos(theta) + (x1 - x2) * sin(theta) + y2; }
void Location_Rotation(Point2f* Src_Location, Point2f* Dst_Location, float angle, Mat& input)
{
if (angle != 0)
{
angle *= CV_PI / 180;
Point2f center((float)input.cols*0.5, (float)input.rows*0.5);
float alpha = cos(angle);
float beta = sin(angle);
Mat M(2, 3, CV_32F);
float* m = (float*)M.data;
m[0] = alpha;
m[1] = beta;
m[2] = (1 - alpha)*center.x - beta*center.y;
m[3] = -beta;
m[4] = alpha;
m[5] = beta*center.x + (1 - alpha)*center.y;
Point2f source = Src_Location[0];
Dst_Location[0].x = source.x*m[0] + source.y*m[1] + m[2];
Dst_Location[0].y = source.x*m[3] + source.y*m[4] + m[5];
}
else
{
Dst_Location[0] = Src_Location[0];
}
}