C语言WFC实现矩形旋转

前言:结合上一篇的博客进行对矩形的创建和旋转操作

代码实现:

//旋转矩阵
void CCGDrawingView::RotateRectangle(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, double angle, CDC* pDC)
{
	//绘制旋转前的矩形
	Rectangle(x1, y1, x2, y2, x3, y3, x4, y4, RGB(255, 0, 0), pDC);

	//绘制旋转后矩形
	//计算矩形的中心坐标
	double x0 = 0.5 * (x1 + x2);
	double y0 = 0.5 * (y1 + y3);

	//对原始坐标进行平移
	double x11 = x1 - x0;
	double y11 = y1 - y0;

	double x21 = x2 - x0;
	double y21 = y2 - y0;

	double x31 = x3 - x0;
	double y31 = y3 - y0;

	double x41 = x4 - x0;
	double y41 = y4 - y0;

	//对平移后的坐标进行旋转
	double PI = 3.14159265358979323846;
	double rad_angle = angle * PI / 180.0;
	double x12 = cos(rad_angle) * x11 - sin(rad_angle) * y11;
	double y12 = sin(rad_angle) * x11 + cos(rad_angle) * y11;

	double x22 = cos(rad_angle) * x21 - sin(rad_angle) * y21;
	double y22 = sin(rad_angle) * x21 + cos(rad_angle) * y21;

	double x32 = cos(rad_angle) * x31 - sin(rad_angle) * y31;
	double y32 = sin(rad_angle) * x31 + cos(rad_angle) * y31;

	double x42 = cos(rad_angle) * x41 - sin(rad_angle) * y41;
	double y42 = sin(rad_angle) * x41 + cos(rad_angle) * y41;

	double x13 = x12 + x0;
	double y13 = y12 + y0;

	double x23 = x22 + x0;
	double y23 = y22 + y0;

	double x33 = x32 + x0;
	double y33 = y32 + y0;

	double x43 = x42 + x0;
	double y43 = y42 + y0;

	Rectangle(int(x13 + 0.5), int(y13 + 0.5),
		int(x23 + 0.5), int(y23 + 0.5),
		int(x33 + 0.5), int(y33 + 0.5),
		int(x43 + 0.5), int(y43 + 0.5),
		RGB(0, 255, 0),
		pDC);

}


结果展示:旋转45度

C语言WFC实现矩形旋转_第1张图片

你可能感兴趣的:(c语言,算法,开发语言)