您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 实验三计算机图形学多边形填充算法
洛阳理工学院实验报告系别计算机与信息工程系班级B120531学号B12053113姓名课程名称计算机图形学实验日期2013-11-7实验名称多边形填充算法编程成绩实验目的:熟悉多边形填充算法,掌握MFC图形编程的基本方法和调试技巧。实验条件:计算机;VS2008;OpenGL实验内容:1.使用MFC技术实现多边形有效边表填充算法,参考界面效果如下://ChildView.cpp:CChildView类的实现#includestdafx.h#includedemo.h#includeChildView.h#includemath.h#defineRound(d)int(floor(d+0.5))//四舍五入宏定义#ifdef_DEBUG#definenewDEBUG_NEW#endif//CChildViewCChildView::CChildView(){}CChildView::~CChildView(){}BEGIN_MESSAGE_MAP(CChildView,CWnd)ON_WM_PAINT()ON_WM_CREATE()ON_COMMAND(ID_DRAW_PIC,&CChildView::OnDrawPic)END_MESSAGE_MAP()//CChildView消息处理程序BOOLCChildView::PreCreateWindow(CREATESTRUCT&cs){if(!CWnd::PreCreateWindow(cs))returnFALSE;cs.dwExStyle|=WS_EX_CLIENTEDGE;cs.style&=~WS_BORDER;cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,::LoadCursor(NULL,IDC_ARROW),reinterpret_castHBRUSH(COLOR_WINDOW+1),NULL);returnTRUE;}voidCChildView::OnPaint(){CPaintDCdc(this);//用于绘制的设备上下文//TODO:在此处添加消息处理程序代码DrawGraph();//不要为绘制消息而调用CWnd::OnPaint()}voidCChildView::ReadPoint()//点表{P[0].x=50;P[0].y=100;P[1].x=-150;P[1].y=300;P[2].x=-250;P[2].y=50;P[3].x=-150;P[3].y=-250;P[4].x=0;P[4].y=-50;P[5].x=100;P[5].y=-250;P[6].x=300;P[6].y=150;}voidCChildView::DrawPolygon(CDC*pDC)//绘制多边形边界{CLine*line=newCLine;CP2t;for(inti=0;i7;i++)//绘制多边形{if(i==0){line-MoveTo(pDC,P[i]);t=P[i];}else{line-LineTo(pDC,P[i]);}}line-LineTo(pDC,t);//闭合多边形deleteline;}voidCChildView::DrawGraph()//绘制图形{CRectrect;//定义客户区GetClientRect(&rect);//获得客户区的大小CDC*pDC=GetDC();//定义设备上下文指针pDC-SetMapMode(MM_ANISOTROPIC);//自定义坐标系pDC-SetWindowExt(rect.Width(),rect.Height());//设置窗口比例pDC-SetViewportExt(rect.Width(),-rect.Height());//设置视区比例,且x轴水平向右,y轴垂直向上pDC-SetViewportOrg(rect.Width()/2,rect.Height()/2);//设置客户区中心为坐标系原点rect.OffsetRect(-rect.Width()/2,-rect.Height()/2);//矩形与客户区重合if(!bFill)DrawPolygon(pDC);//绘制多边形elseFillPolygon(pDC);//填充多边形ReleaseDC(pDC);//释放DC}voidCChildView::FillPolygon(CDC*pDC)//填充多边形{for(inti=0;i7;i++)//转储顶点坐标,y坐标取为整数{P1[i].x=P[i].x;P1[i].y=Round(P[i].y);P1[i].c=CRGB(bRed/255.0,bGreen/255.0,bBlue/255.0);}CFill*fill=newCFill;//动态分配内存fill-SetPoint(P1,7);//初始化Fill对象fill-CreateBucket();//建立桶表fill-CreateEdge();//建立边表fill-Gouraud(pDC);//填充多边形deletefill;//撤销内存}intCChildView::OnCreate(LPCREATESTRUCTlpCreateStruct){if(CWnd::OnCreate(lpCreateStruct)==-1)return-1;//TODO:在此添加您专用的创建代码bFill=FALSE;ReadPoint();return0;}voidCChildView::OnDrawPic(){//TODO:在此添加命令处理程序代码COLORREFGetClr=RGB(0,0,0);//调色板颜色CColorDialogccd(GetClr,CC_SOLIDCOLOR);if(IDOK==ccd.DoModal())//调用颜色对话框选取填充色GetClr=ccd.GetColor();elsereturn;bRed=GetRValue(GetClr);//获取红色分量bGreen=GetGValue(GetClr);//获取绿色分量bBlue=GetBValue(GetClr);//获取蓝色分量bFill=TRUE;Invalidate();}2.使用MFC技术实现多边形边缘填充算法,参考界面效果如下://demoView.cpp:CdemoView类的实现#includestdafx.h#includedemo.h#includedemoDoc.h#includedemoView.h#includemath.h#defineRound(d)int(floor(d+0.5))//四舍五入宏定义#ifdef_DEBUG#definenewDEBUG_NEW#endif//CdemoViewIMPLEMENT_DYNCREATE(CdemoView,CView)BEGIN_MESSAGE_MAP(CdemoView,CView)//标准打印命令ON_COMMAND(ID_FILE_PRINT,&CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT,&CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW,&CdemoView::OnFilePrintPreview)ON_COMMAND(ID_DRAW_PIC,&CdemoView::OnDrawPic)END_MESSAGE_MAP()//CdemoView构造/析构CdemoView::CdemoView(){//TODO:在此处添加构造代码}CdemoView::~CdemoView(){}BOOLCdemoView::PreCreateWindow(CREATESTRUCT&cs){//TODO:在此处通过修改//CREATESTRUCTcs来修改窗口类或样式returnCView::PreCreateWindow(cs);}//CdemoView绘制voidCdemoView::OnDraw(CDC*/*pDC*/){CdemoDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);if(!pDoc)return;//TODO:在此处为本机数据添加绘制代码DrawGraph();}//CdemoView打印voidCdemoView::OnFilePrintPreview(){AFXPrintPreview(this);}BOOLCdemoView::OnPreparePrinting(CPrintInfo*pInfo){//默认准备returnDoPreparePrinting(pInfo);}voidCdemoView::OnBeginPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/){//TODO:添加额外的打印前进行的初始化过程}voidCdemoView::OnEndPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/){//TODO:添加打印后进行的清理过程}voidCdemoView::OnRButtonUp(UINTnFlags,CPointpoint){ClientToScreen(&point);OnContextMenu(this,point);}voidCdemoView::OnContextMenu(CWnd*pWnd,CPointpoint){theApp.GetContextMenuManager()-ShowPopupMenu(IDR_POPUP_EDIT,point.x,point.y,this,TRUE);}voidCdemoView::ReadPoint()//点表{P[0].x=50;P[0].y=100;P[1].x=-150;P[1].y=300;P[2].x=-250;P[2].y=50;P[3].x=-150;P[3].y=-250;P[4].x=0;P[4].y=-50;P[5].x=100;P[5].y=-250;P[6].x=300;P[6].y=150;}voidCdemoView::DrawPolygon(CDC*pDC){for(inti=0;i7;i++)//计算多边形边界{if(P[i].xMaxX)MaxX=P[i].x;if(P[i].xMinX)MinX=P[i].x;if(P[i].yMaxY)MaxY=P[i].y;if(P[i].yMinY)MinY=P[i].y;}CLine*line=newCLine;CP2t;for(inti=0;i7;i++)//绘制多边形{if(i==0){line-MoveTo(pDC,P[i]);t=P[i];}else{line-LineTo(pDC,P[i]);}}line-LineTo(pDC,t);//闭合多边形line-MoveTo(pDC,CP2(MinX,MinY));//绘制包围盒line-LineTo(pDC,CP2(MinX,MaxY));line-LineTo(pDC,CP2(MaxX,MaxY));line-LineTo(pDC,CP2(MaxX,MinY));line-LineTo(pDC,CP2(MinX,MinY));deleteline;}voidCdemoView::FillPolygon(CDC*pDC){COLORREFBClr=RGB(255,255,255);//背景色COLORREFFClr=GetClr;//填充色intymin,ymax;//边的最小y值与最大y值doublex,y,k;//x,y当前点,k斜率的倒数for(inti=0;i7;i++)//循环多边形所有边{intj=(i+1)%7;k=(P[i].x-P[j].x)/(P[i].y-P[j
本文标题:实验三计算机图形学多边形填充算法
链接地址:https://www.777doc.com/doc-1741767 .html