资源描述
实验二
实验目的:
绘制金刚石图案,并利用鼠标交互实现平移、放大和缩小
实验过程:
设置映射模式为MM_ISOTROPIC,通过在MouseDown、MouseUp及MouseMove消息处理中改变逻辑坐标的原点及范围实现图形的平移及放大和缩小
实验主要算法:
CAaaaaView::CAaaaaView()//构造函数
{
// TOD: add construction code here
Xw=1000;
Yw=1000;
x0=y0=0;
}
void CAaaaaView::OnDraw(CDC* pDC)
{
CAaaaaDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
draw(pCurrentDC);
}
/////////////////////////////////////////////////////////////////////////////
// CAaaaaView printing
void CAaaaaView::draw(CDC *pDC)
{
CRect Rect;////////////////////////////////////////////////////////////////
GetClientRect(& Rect);/////////////////////////////////////////////////////
pDC->SetMapMode(MM_ISOTROPIC);//////////////////////设置映射模式/////////
pDC->SetWindowExt(Xw,Yw);///////////////////////////
pDC->SetWindowOrg(x0,y0);
pDC->SetViewportExt(Rect.right,-Rect.bottom);/////////////////////////////
pDC->SetViewportOrg(Rect.right/2,Rect.bottom/2);/////////////////////////
// CBrush d(RGB(0,255,0));////////设置填充颜色
// pDC->SelectObject(d);/////////////////////
pDC->Ellipse(-200,200,200,-200);
int a[30][2];//////////x坐标为a[i][0];y坐标为a[i][1];
int r=200;
double c=3.141592653;
for(int i=0;i<=29;i++){
a[i][0]=r* cos(2*c/30*i);
a[i][1]=r* sin(2*c/30*i);
}
//ofstream d("checkpoint.txt",ios::trunc);//写入记事本,检验是否正确创建各个点
//for(int l=0;l<30;l++){
// d<<a[l][0]<<" "<<a[l][1]<<endl;
//}
//d.close();
for(int j=0;j<30;j++)
for(int k=0;k<30;k++){
pDC->MoveTo(a[j][0],a[j][1]);
pDC->LineTo(a[k][0],a[k][1]);
}
}
void CAaaaaView::OnLButtonDown(UINT nFlags, CPoint point) //缩放下左击放大,平移下平移
{
// TODO: Add your message handler code here and/or call default
if(a)
{
Xw=Xw/2;
Yw=Yw/2;
CPoint p=point;
pCurrentDC->DPtoLP(&p);
x0=Xw/4+p.x;
y0=Yw/4+p.y;
}
else{
CPoint b=point;
pCurrentDC->DPtoLP(&b);
x0=x0-b.x;
y0=y0-b.y;
}
Invalidate();
CView::OnLButtonDown(nFlags, point);
}
int CAaaaaView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
pCurrentDC= new CClientDC(this);
return 0;
}
void CAaaaaView::OnRButtonDown(UINT nFlags, CPoint point) //缩放下的右击缩小
{
// TODO: Add your message handler code here and/or call default
if(a)
{
Xw=Xw*2;
Yw=Yw*2;
CPoint p=point;
pCurrentDC->DPtoLP(&p);
x0=p.x+Xw/4;
y0=p.y+Yw/4;
Invalidate();}
CView::OnRButtonDown(nFlags, point);
}
void CAaaaaView::On1() //缩放菜单
{
// TODO: Add your command handler code here
a=true;
}
void CAaaaaView::On2() //平移菜单
{
// TODO: Add your command handler code here
a=false;
}
实验体会:
本次实验让我懂得如何去平移一个物体,也让我懂得如何缩放物体。
展开阅读全文