计算机图形学之二维平移旋转缩放代码

以下均为MFC工程代码

代码一:简单实现

void CGeotranView::Onyuantu() 
{
	// 原图,画三角形
	CDC *pDC=GetDC();
	pDC->MoveTo(100,100);
	pDC->LineTo(200,50);
	pDC->LineTo(200,150);
	pDC->LineTo(100,100);
	ReleaseDC(pDC);	
}

void CGeotranView::Ontranslation() 
{
	// 平移 tx=50,ty=60
	CDC *pDC=GetDC();
	int tx=50,ty=60;
	pDC->MoveTo(100+tx,100+ty);
	pDC->LineTo(200+tx,50+ty);
	pDC->LineTo(200+tx,150+ty);
	pDC->LineTo(100+tx,100+ty);
	ReleaseDC(pDC);		
}

void CGeotranView::Onrotation() 
{
	// 旋转, 角度sita=30度
	CDC *pDC=GetDC();
	double sita=30;
	double hudu=sita*3.14159/180;
	pDC->MoveTo(int(100*cos(hudu)-100*sin(hudu)),int(100*sin(hudu)+100*cos(hudu)));
	pDC->LineTo(int(200*cos(hudu)-50*sin(hudu)),int(200*sin(hudu)+50*cos(hudu)));
	pDC->LineTo(int(200*cos(hudu)-150*sin(hudu)),int(200*sin(hudu)+150*cos(hudu)));
	pDC->LineTo(int(100*cos(hudu)-100*sin(hudu)),int(100*sin(hudu)+100*cos(hudu)));
	ReleaseDC(pDC);	
	
}

void CGeotranView::Onscaling() 
{
	// 缩放 sx=2,sy=3
	int sx=2,sy=3;
	CDC *pDC=GetDC();
	pDC->MoveTo(100*sx,100*sy);
	pDC->LineTo(200*sx,50*sy);
	pDC->LineTo(200*sx,150*sy);
	pDC->LineTo(100*sx,100*sy);
	ReleaseDC(pDC);		
}

代码二:新的点的坐标计算通过函数实现

POINT translationPoint(POINT point,int tx,int ty)
{
	POINT newpoint;
	newpoint.x=	point.x+tx;
	newpoint.y=	point.y+ty;	
	return newpoint;
}

POINT rotationPoint(POINT point,double sita)
{
	POINT newpoint;
	double hudu=sita*3.14159/180;
	newpoint.x=	(int)(point.x*cos(hudu)-point.y*sin(hudu));
	newpoint.y=	(int)(point.x*sin(hudu)+point.y*cos(hudu));
	return newpoint;
}


POINT scalingPoint(POINT point,int sx,int sy)
{
	POINT newpoint;
	newpoint.x=	point.x*sx;
	newpoint.y=	point.y*sy;
	return newpoint;
}

void CGeotranView::Onyuantu2() 
{
	POINT point1,point2,point3;
	point1.x=200;point1.y=200;
	point2.x=300;point3.y=150;
	point3.x=300;point2.y=250;

	// 原图,画三角形
	CDC *pDC=GetDC();
	pDC->MoveTo(point1);
	pDC->LineTo(point2);
	pDC->LineTo(point3);
	pDC->LineTo(point1);
	ReleaseDC(pDC);


	
}

void CGeotranView::Ontranslation2() 
{
	POINT point1,point2,point3;
	point1.x=200;point1.y=200;
	point2.x=300;point3.y=150;
	point3.x=300;point2.y=250;

	

	// 平移 tx=50,ty=60
	CDC *pDC=GetDC();
	int tx=50,ty=60;
	pDC->MoveTo(translationPoint(point1,tx,ty));
	pDC->LineTo(translationPoint(point2,tx,ty));
	pDC->LineTo(translationPoint(point3,tx,ty));
	pDC->LineTo(translationPoint(point1,tx,ty));
	ReleaseDC(pDC);	
}

void CGeotranView::Onrotation2() 
{
	POINT point1,point2,point3;
	point1.x=200;point1.y=200;
	point2.x=300;point3.y=150;
	point3.x=300;point2.y=250;
	

	// 旋转, 角度sita=30度
	CDC *pDC=GetDC();
	double sita=30;
	pDC->MoveTo(rotationPoint(point1,sita));
	pDC->LineTo(rotationPoint(point2,sita));
	pDC->LineTo(rotationPoint(point3,sita));
	pDC->LineTo(rotationPoint(point1,sita));
	ReleaseDC(pDC);	
	
}

void CGeotranView::Onscaling2() 
{
	POINT point1,point2,point3;
	point1.x=200;point1.y=200;
	point2.x=300;point3.y=150;
	point3.x=300;point2.y=250;

	// 缩放 sx=2,sy=2
	int sx=2,sy=2;
	CDC *pDC=GetDC();
	pDC->MoveTo(scalingPoint(point1,sx,sy));
	pDC->LineTo(scalingPoint(point2,sx,sy));
	pDC->LineTo(scalingPoint(point3,sx,sy));
	pDC->LineTo(scalingPoint(point1,sx,sy));
	ReleaseDC(pDC);	
	
}


你可能感兴趣的:(计算机,图形,几何变换)