1、计算机图形学画线画圆算法宁夏师范学院数学与计算机科学学院计算机图形学实验报告实验序号: 3 实验项目名称: 画线、画圆算法学号姓名专业、班实验地点指导教师时 间一、实验目的:1、环境要求:Microsoft Visual C+ 6.0二、实验内容与步骤 1、首先建立一个单文档的MFC项目Graphic。 2、接下来使用对话框控件设计好如图(1)对话框界面,并且创建新类Inputdialg,其基类为Dialog,再对菜单栏进行设计,让其与对话框映射起来。 3、在Inputdialg类中添加整型变量 m_startX,m_startY,m_endX,m_endY,m_circleR,m_MidPo
2、intX ,m_MidPointY 。可以同时对其赋初值,如图(2):4、接下来是将以上变量与对话框中的编辑框中值映射,需要通过添加DoDataExchange函数进行值的交换,然后进入类向导(ctrl+W),选择成员变量选项,进行如下映射:5、再为对话框按钮添加映射函数:void Inputdialg:OnDda() / TODO: Add your control notification handler code here /释放设备指针 UpdateData(TRUE); startX=m_startX; startY=m_startY; endX=m_endX; endY=m_end
3、Y; UpdateData(FALSE); flag=true; pview-Invalidate();void Inputdialg:OnMidpoint() / TODO: Add your control notification handler code here UpdateData(TRUE); startX=m_startX; startY=m_startY; endX=m_endX; endY=m_endY; UpdateData(FALSE); flag2=true; pview-Invalidate();void Inputdialg:OnBresenham() / TOD
4、O: Add your control notification handler code here UpdateData(TRUE); startX=m_startX; startY=m_startY; endX=m_endX; endY=m_endY; UpdateData(FALSE); flag3=true; pview-Invalidate();void Inputdialg:OnBresenhamcircle() / TODO: Add your control notification handler code here UpdateData(TRUE); MidPointX=m
5、_MidPointX; MidPointY=m_MidPointY; circleR=m_circleR; UpdateData(FALSE); flag4=true; pview-Invalidate();6、要实现图像的显示还是需要通过视图类来实现,在ondraw()函数中添加如下代码: CClientDC dc(this); if(flag=true) DDALine(startX,startY,endX,endY,RGB(0,255,0),dc); flag=false; if(flag2=true) MidBresenhamLine(startX,startY,endX,endY,R
6、GB(255,0,0),dc); flag2=false; if (flag3=true) BresenhamLine(startX,startY,endX,endY,RGB(255,0,255),dc); flag3=false; if (flag4=true) MidBresenhamCircle(MidPointX,MidPointY,circleR,RGB(250,0,100),dc); flag4=false; 7、上一步中的flag,flag2,flag3,flag4都是未定义变量,需要再对其进行定义,这次是对部变量的定义,(1)在StdAx.h 添加: extern int st
7、artX,startY,endX,endY,circleR,MidPointX,MidPointY; extern bool flag,flag2,flag3,flag4;(2)在StdAfx.cpp中添加: int startX,startY,endX,endY,circleR,MidPointX,MidPointY; bool flag,flag2,flag3,flag4;(3)在Inputdialg.cpp中添加: extern CGraphicView *pview; extern int startX,startY,endX,endY,circleR,MidPointX,MidPoi
8、ntY; extern bool flag,flag2,flag3,flag4;8、在GraphicView.cpp中定义CGraphicView *pview=NULL;,要指针生效同样需要初始化, 如下:CGraphicView:CGraphicView() / TODO: add construction code here pview=this;9、至此,准备工作做的是差不多了,但最重要的是还缺少实现函数,在视图类中添加如下函数:void CGraphicView:DDALine(int x0, int y0, int x1, int y1, int color,CClientDC &
9、dc) /sleep判断是否直接画 int dx,dy,eps1,k; float x,y,xIncre,yIncre; dx=x1-x0;dy=y1-y0; x=x0;y=y0; if(abs(dx)abs(dy) eps1=abs(dx); else eps1=abs(dy); xIncre=(float)dx/(float)eps1; yIncre=(float)dy/(float)eps1; CPen newpen(PS_SOLID,1,color); dc.SelectObject(&newpen); for(k=0;k=eps1;k+) int a=(int)(x+0.5); in
10、t b=(int)(y+0.5); /dc.Ellipse(a-1,b-1,a+1,b+1); dc.SetPixel(a,b,color); x+=xIncre; y+=yIncre; void CGraphicView:MidBresenhamLine(int x0,int y0,int x1,int y1,int color,CClientDC &dc) CPen newpen(PS_SOLID,1,color); dc.SelectObject(&newpen); if(x0=x1)/斜率不存在 for(int i=y0;ix1) x=x1;x1=x0;x0=x; y=y1;y1=y0
11、;y0=y; dx=x1-x0;dy=y1-y0; float k=(float)dy/(float)dx; /斜率 if(k=0&k=1) x=x0;y=y0; d=dx-2*dy; UpIncre=2*dx-2*dy;DownIncre=-2*dy; while(x=x1) dc.Ellipse(x-1,y-1,x+1,y+1); x+; if(d1) x=x0;y=y0; d=2*dx-dy; UpIncre=-2*dx+2*dy;/右 DownIncre=-2*dx; /左 while(y=y1) dc.Ellipse(x-1,y-1,x+1,y+1); y+; if(d=-1&k=0
12、) x=x0;y=y0; d=-dx-2*dy; UpIncre=-2*dx-2*dy;/下 DownIncre=-2*dy; /上 while(x0) /大于0,取下点 y-; d+=UpIncre; else d+=DownIncre; else if(k=y1) dc.Ellipse(x-1,y-1,x+1,y+1); y-; if(d0) /大于0,取右点 x+; d+=UpIncre; else d+=DownIncre; void CGraphicView:BresenhamLine(int x0, int y0, int x1, int y1, int color,CClient
13、DC &dc) CPen newpen(PS_SOLID,1,color); dc.SelectObject(&newpen); if(x0=x1)/斜率不存在 for(int i=y0;ix1) x=x1;x1=x0;x0=x; y=y1;y1=y0;y0=y; dx=x1-x0;dy=y1-y0; x=x0;y=y0; float k=(float)dy/(float)dx; /斜率 if(k=0&k=1) e=-dx; while(x0) y+; e-=2*dx; else if(k1) e=-dy; while(y0) x+; e-=2*dy; else if(k=-1&k=0) e=
14、-dx; while(x=x1) dc.Ellipse(x-1,y-1,x+1,y+1); x+; e+=2*dy; if(e=0) y-; e+=2*dx; else if(k=y1) dc.Ellipse(x-1,y-1,x+1,y+1); y-; e+=2*dy; /e+k if(e0) x+; e-=2*dy; void CGraphicView:MidBresenhamCircle(int MidPointX,int MidPointY,int r,int color,CClientDC &dc) CPen newpen(PS_SOLID,1,color); dc.SelectObj
15、ect(&newpen); int x,y,d; /x为中点x坐标 x=0; y=r; d=1-r; while(x=y) dc.SetPixel( x+MidPointX, y+MidPointY,color); dc.SetPixel(-x+MidPointX, y+MidPointY,color); dc.SetPixel(-x+MidPointX,-y+MidPointY,color); dc.SetPixel( x+MidPointX,-y+MidPointY,color); dc.SetPixel( y+MidPointX, x+MidPointY,color); dc.SetPixel(-y+MidPointX, x+MidPointY,color); dc.SetPixel(-y+MidPointX,-x+MidPointY,color); dc.SetPixel( y+MidPointX,-x+MidPointY,color); if(d0) d+=2*x+3; else d+=2*(x-y)+5; y-; x+; 10、最后在对应的实现文件中加入适当的头文件申明,保证编译链接的通过。三、运行结果 DDA: MIDPOINT: BRESENHAM: BRESENHAMCIRCLE: MidpointEllipse:五、教师评语签名: 日期: 年 月 日 成绩
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1