实验序号:03 实验项目名称:多边形填充算法的实现
学 号 | 姓 名 | 专业、班级 | 软件工程 | |||
实验地点 | 计-201 | 指导教师 | 时 间 | 2016.4.05 | ||
一、实验目的及要求 多边形的填充算法有很多种,比如说边缘填充算法,栅栏填充算法,还有种子填充算法。种子填充算法和其他几种的算法采用的是完全不同的方法。 二、实验设备(环境)及要求 1、了解MATLAB软件基本操作,熟悉基本计算和绘图功能。 2、环境要求:配有Windows XP及以上操作系统,安装MATLAB软件的完整版。 3、实验过程中,务必仔细观察上机现象,记录必要信息,分析实验结果,按要求写出实验报告。 三、实验内容与步骤 题目:多边形填充算法的实现
四、实验结果与数据处理 class CDrawView:public CView { protected://create from serialization only CDrawView(); DECLARE_DYNCREATE(CDrawView) public: CDrawDoc*GetDocument(); public: CPoint spt[7],s_point; ...... } void CDrawView::OnDraw(CDC*pDC) {CDrawDoc*pDoc=GetDocument(); ASSERT_VALID(pDoc); CPen newpen(PS_SOLID,1,RGB(0,0,255)); CPen*old=pDC->SelectObject(&newpen); pDC->TextOut(20,20,"双击鼠标左键,生成多边形"); pDC->TextOut(20,50,"先在多边形内部单击鼠标右键,选择种子点,然后单击功能菜单实现填充"); pDC->SelectObject(old); } void CDrawView::OnSeedfill() {CclientDC dc(this); int fill=RGB(0,255,0); int boundary=RGB(0,0,255); int x,y,p0,pmin,pmax; for(int m=1;m<7;m++) { for(int n=0;n<7-m;n++) { if(spt[n].y p0=spt[n].y; spt[n]=spt[n+1]; spt[n+1]=p0; } } } pmax=spt[0].y; pmin=spt[6].y; x=s_point.x;y=s_point.y; for(;y<=pmax;y++) { int current=dc.GetPixel(x,y); while((current!=boundary)&&(current!=fill)) { dc.SetPixel(x,y,fill); x++; current=dc,GetPixel(x,y); } x=s-point.x; x--; current=dc.GetPixel(x,y); while((current!=boundary)&&(current!=fill)) { dc.SetPixel(x,y,fill); x--; current=dc,GetPixel(x,y); } x=s-point.x; } x=s_point.x;y=s_point.y-1; for(;y>=pmin;y--) { int current=dc.GetPixel(x,y); while((current!=boundary)&&(current!=fill)) { dc.SetPixel(x,y,fill); x++; current=dc,GetPixel(x,y); } x=s-point.x; x--; current=dc.GetPixel(x,y); while((current!=boundary)&&(current!=fill)) { dc.SetPixel(x,y,fill); x--; current=dc,GetPixel(x,y); } x=s-point.x; } } void CDrawView::OnLButtonDblClk(UINT nFlags,CPoint point) { RedrawWindow(); CDC*pDC=GetDC(); CPen newpen(PS_SOLID,1,RGB(0,0,255)); CPen*old=pDC->SelectObject(&newpen); spt[0]=CPoint(100,100); spt[1]=CPoint(250,100); spt[2]=CPoint(250,250); spt[3]=CPoint(100,250); spt[4]=CPoint(150,200); spt[5]=CPoint(90,150); spt[6]=CPoint(160,150); spt[7]=CPoint(100,100); pDC->SelectObject(old); CView::OnLButtonDblClk(nFlags,point); } void CDrawView::OnLButtonDown(UINT nFlags,CPoint point) { s_point=pont; CView::OnRButtonDown(nFlags,point); } CView::OnLButtonDblClk(nFlags, point); } void CMy2dcadView::makeet()() { int p1=0,p2=1; int smally,bigy,smallx,bigx; Edge*ep1; for(; p1 continue; else if (lhpoint[p1][1] bigy=lhpoint[p2][1]; smallx=lhpoint[p1][0]; bigx=lhpoint[p2][0]; } else { smally=lhpoint[p2][1]; bigy=lhpoint[p1][1]; smallx=lhpoint[p2][0]; bigx=lhpoint[p1][0]; } ep1=new Edge; ep1->ymax=bigy; ep1->x=(float)smallx; ep1->deltax=(float)(smallx-bigx)/(smally-bigy); ep1->nextEdge=et[smally]; et[smally]=ep1; } } void CMy2dcadView::OnRButtonDown(UINT nFlags,CPint point) { //TODO:Add your message handler code here and/or call default CView::OnRButtonDown(nFlag,point); CClientDC dc(this); COLORREF Color; int scany=-1; int i=0,j=0; int k; int r,g,b; Edge*etp; makeet(); while (et[++scany]==NULL); While(scany<600){ if(et[scany]!=NULL){ etp=et[scany]; while(etp){ i=scany; while(i j=(int)etp->x; j++; while(i<600){ Color=dc.GetPixel(j,i); r=255--GetRValue(Color); G=255--GetGValue(Color); B=255--GetBValue(Color); dc.GetPixel(j,i),RGB(r,g,b)); j++;} etp->x+=etp->deltax; i++; } etp->x+=etp->nextEdge; } et[scany]=NULL; } scany++; } npoint=20; } 运行结果: 五、分析与讨论 | ||||||
六、教师评语 签名: 日期: | 成绩 |