1. 程式人生 > >MFC 二維圖形幾何變換

MFC 二維圖形幾何變換

 實驗原理:

   (1)使用齊次座標進行二維圖形變換。

(2)利用陣列表示並完成矩陣運算。

實驗內容:

將三個頂點為分別為(100, 100),(50, 180)和( 130, 160)的三角形分別進行下列圖形變換:

    (1)沿x軸正方向平移150。

    (2)將三角形放大到原來的2.5倍、


	CClientDC *pDC;
	int i;
	pDC = new CClientDC(this);
	CPoint pt[3] = { CPoint(100,100),CPoint(50,180),CPoint(130,160) };
	pDC->Polygon(pt, 3);
	i = 150;
	CPoint pt2[3] = { CPoint(100+i,100),CPoint(50+i,180),CPoint(130+i,160) };
	pDC->Polygon(pt2, 3);
	CPoint pt3[3] = { CPoint(2.5*100,2.5*100),CPoint(2.5*50,2.5*180),CPoint(2.5*130,2.5*160) };
	pDC->Polygon(pt3, 3);
	delete pDC;


void transform(double M[][3], double oldX, double oldY, double *newX, double *newY)
{
	*newX = 0;
	*newY = 0;
	*newX += oldX*M[0][0] + oldY*M[1][0] + M[2][0];
	*newY += oldX*M[0][1] + oldY*M[1][1] + M[2][1];
}
	void transform(double M[][3], double oldX, double oldY, double *newX, double *newY);
	CClientDC *pDC;
	double newx1, newy1, newx2, newy2, newx3, newy3;
	double x1 = 100, y1 = 100, x2 = 50, y2 = 180, x3 = 130, y3 = 160;
	double M[3][3] = { { 1, 0, 150 }, { 0, 1, 0 }, { 0, 0, 1 } };
	double M1[3][3] = { { 2.5, 0, 0 }, { 0, 2.5, 0 }, { 0, 0, 1 } };
	double newX, newY;
	pDC = new CClientDC(this);
	CPoint pt[3] = { CPoint(100, 100), CPoint(50, 180), CPoint(130, 160) };
	pDC->Polygon(pt, 3);
	transform(M, x1, y1, &newX, &newY);
	newx1 = (int)newX;
	newy1 = (int)newY;
	transform(M, x2, y2, &newX, &newY);
	newx2 = (int)newX;
	newy2 = (int)newY;
	transform(M, x3, y3, &newX, &newY);
	newx3 = (int)newX;
	newy3 = (int)newY;
	CPoint pt2[3] = { CPoint(newx1, newy2), CPoint(newx2, newy2), CPoint(newx3, newy3) };
	pDC->Polygon(pt2, 3);
	transform(M1, x1, y1, &newX, &newY);
	newx1 = (int)newX;
	newy1 = (int)newY;
	transform(M1, x2, y2, &newX, &newY);
	newx2 = (int)newX;
	newy2 = (int)newY;
	transform(M1, x3, y3, &newX, &newY);
	newx3 = (int)newX;
	newy3 = (int)newY;
	CPoint pt3[3] = { CPoint(newx1, newy2), CPoint(newx2, newy2), CPoint(newx3, newy3) };
	pDC->Polygon(pt3, 3);
	delete pDC;
void transform(double M[][3], double oldst[], double newst[],double &x,double &y)
{
	newst[0] = 0;
	newst[1] = 0;
	newst[0] += (oldst[0]*M[0][0] + oldst[1]*M[1][0] + M[2][0]);
	newst[1] += (oldst[0] * M[0][1] + oldst[1] * M[1][1] + M[2][1]);
	x = newst[0];
	y = newst[1];
}
	void transform(double M[][3], double oldst[], double newst[], double &x, double &y);
	CClientDC *pDC;
	double  newst[3] = { 0 };
	double x=0, y=0;
	double xc = 0, yc = 0;
	double M[3][3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 150, 0, 1 } };
	pDC = new CClientDC(this);
	CPoint pt[3] = { CPoint(100, 100), CPoint(50, 180), CPoint(130, 160) };
	pDC->Polygon(pt, 3);
	double str1[3] = { 100, 100, 1 };
	transform(M,str1,newst,x,y);
	xc = x;
	yc = y;
	pDC->MoveTo(x, y);
	double str2[3] = { 50, 180, 1 };
	transform(M, str2, newst, x, y);
	pDC->LineTo(x, y);
	double str3[3] = { 130, 160, 1 };
	transform(M, str3, newst, x, y);
	pDC->LineTo(x, y);
	pDC->LineTo(xc, yc);
	M[0][0] = 2.5;
	M[1][1] = 2.5;
	M[2][0] = 0;
	transform(M, str1, newst, x, y);
	xc = x;
	yc = y;
	pDC->MoveTo(x, y);
	transform(M, str2, newst, x, y);
	pDC->LineTo(x, y);
	transform(M, str3, newst, x, y);
	pDC->LineTo(x, y);
	pDC->LineTo(xc, yc);
	delete pDC;