当前位置:文档之家› 计算机图形学图形二维变换

计算机图形学图形二维变换


void CMyView::ClearMatrix(double A[][3]) { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) A[i][j]=0; } }
void CMyView::Draw(double D[][3], int n) { RedrawWindow(); CClientDC dc(this); CPen pen,*pOldpen; pen.CreatePen(PS_SOLID,3,RGB(0,0,255)); pOldpen=dc.SelectObject(&pen); for(int i=0;i<n;i++) { if(i==0) dc.MoveTo(ROUND(MaxX/2+D[i][0]),ROUND( MaxY/2-D[i][1]));
void CMyView::Calculate(double P0[][3], double T[][3]) { double Ptemp[4][3]; KeepOriginalMatrix(P,Ptemp); for(int i=0;i<4;i++) for(int j=0;j<3;j++) P[i][j]=Ptemp[i][0]*T[0][j]+Ptemp[i][1]*T[1][j] +Ptemp[i][2]*T[2][j]; }
Байду номын сангаас
• void CMyView::KeepOriginalMatrix(double Orig[][3], double Dest[][3]) • { • int i,j; • for(i=0;i<4;i++) • for(j=0;j<3;j++) • Dest[i][j]=Orig[i][j]; • }
• 5.在ondraw函数中添加以下代码
• void CMyView::OnDraw(CDC* pDC) • { • CMyDoc* pDoc = GetDocument(); • ASSERT_VALID(pDoc); • // TODO: add draw code for native data here • GetMaxX(); • GetMaxY(); • CPen pen,*pOldpen; • pen.CreatePen(PS_SOLID,3,RGB(0,255,255)); • pDC->SelectObject(&pen); • pDC->MoveTo(MaxX/2,0);//绘制坐标轴 • pDC->LineTo(MaxX/2,MaxY); • pDC->MoveTo(0,MaxY/2); • pDC->LineTo(MaxX,MaxY/2); • }
• • • • • • • • • • • • • •
void CMyView::Tmove(double Tx, double Ty) { ClearMatrix(TM); RedrawWindow(); TM[0][0]=1; TM[1][1]=1; TM[2][0]=Tx; TM[2][1]=Ty; TM[2][2]=1; Calculate(P,TM); AfxGetMainWnd()->SetWindowText ("二维基本几何变换-平移变换"); Draw(P,ntype); }
二维图形变换
参考步骤
• • • • 1.建立工程文件 2.在头文件中加入 #define ROUND(a) int(a+0.5)//四舍五入 #define PI 3.1415926//圆周率
• 3.添加数据成员
int MaxX,MaxY;//屏幕x 和y 的最大坐标 double P[4][3];//变换点 double TM[3][3];//平移变换矩阵 double TS[3][3];//比例变换矩阵 double TR[3][3];//旋转变换矩阵 double TF[3][3];//反射变换矩阵 double TC[3][3];//错切变换矩阵 double OTriangle[4][3]; //正三角形坐标 int ntype;//顶点个数
• • • • • • • • • •
void CMyView::OnMENUClockwise() { // TODO: Add your command handler code here Trotate(30); } void CMyView::OnMENUAnticlockwise() { // TODO: Add your command handler code here Trotate(-30); }
else dc.LineTo(ROUND(MaxX/2+D[i][0]), ROUND(MaxY/2-D[i][1])); } dc.LineTo(ROUND(MaxX/2+D[0][0]),ROUND (MaxY/2-D[0][1])); dc.SelectObject(pOldpen); pen.DeleteObject(); }
• • • • • • • • • •
void CMyView::OnMENUDecrease() { // TODO: Add your command handler code here Tscale(0.5,0.5); } void CMyView::OnMENUIncrease() { // TODO: Add your command handler code here Tscale(2,2); }
• • • • • • • • • • • •
void CMyView::Tscale(double Sx, double Sy) { ClearMatrix(TS); RedrawWindow(); TS[0][0]=Sx; TS[1][1]=Sy; TS[2][2]=1; Calculate(P,TS); AfxGetMainWnd()->SetWindowText ("二维基本几何变换-比例变换"); Draw(P,ntype); }
void CMyView::GetMaxX() { CRect Rect; GetClientRect(&Rect); MaxX=Rect.right; }
void CMyView::GetMaxY() { CRect Rect; GetClientRect(&Rect); MaxY=Rect.bottom; }
• • • • • • • • • • • • • •
void CMyView::Treform(double b, double c) { ClearMatrix(TC); RedrawWindow(); TC[0][0]=1; TC[0][1]=b; TC[1][0]=c; TC[1][1]=1; TC[2][2]=1; Calculate(P,TC); AfxGetMainWnd()->SetWindowText ("二维基本几何变换-错切变换"); Draw(P,ntype); }
• • • • • • • • • • • • • •
void CMyView::Trotate(double thta) { ClearMatrix(TR); RedrawWindow(); TR[0][0]=cos(thta*PI/180); TR[0][1]=sin(thta*PI/180); TR[1][0]=-sin(thta*PI/180); TR[1][1]=cos(thta*PI/180); TR[2][2]=1; Calculate(P,TR); AfxGetMainWnd()->SetWindowText ("二维基本几何变换-旋转变换"); Draw(P,ntype); }
• void CMyView::OnMENUright() • { • // TODO: Add your command handler code here • Tmove(10,0); • }
• void CMyView::OnMenuup() • { • // TODO: Add your command handler code here • Tmove(0,10); • } • void CMyView::OnMenudown() • { • // TODO: Add your command handler code here • Tmove(0,-10); • }
• • • • • • • • • • • • • • •
void CMyView::OnMENUXaxis() { // TODO: Add your command handler code here Treflect(1,-1); } void CMyView::OnMENUYaxis() { // TODO: Add your command handler code here Treflect(-1,1); } void CMyView::OnMENUorg() { // TODO: Add your command handler code here Treflect(-1,-1); }
• 4.初始化构造函数
• CMyView::CMyView() • { • // TODO: add construction code here • P[0][0]=-100/2;P[0][1]=0;P[0][2]=1; • P[1][0]=100/2;P[1][1]=0;P[1][2]=1; • P[2][0]=0;P[2][1]=100/2*tan(60*PI/180);P[2][2]=1; • P[3][0]=0;P[3][1]=0;P[3][2]=1; • ntype=3; • int i,j; • for(i=0;i<4;i++) • for(j=0;j<3;j++) • OTriangle[i][j]=P[i][j]; • }
相关主题