




已阅读5页,还剩12页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
洛阳理工学院实验报告系别计算机与信息工程系班级B120531学号B12053113姓名课程名称计算机图形学实验日期2013-11-7实验名称多边形填充算法编程成绩实验目的: 熟悉多边形填充算法,掌握MFC图形编程的基本方法和调试技巧。实验条件: 计算机;VS2008;OpenGL实验内容: 1使用MFC技术实现多边形有效边表填充算法,参考界面效果如下:/ ChildView.cpp : CChildView 类的实现#include stdafx.h#include demo.h#include ChildView.h#include #define Round(d) int(floor(d+0.5)/四舍五入宏定义#ifdef _DEBUG#define new DEBUG_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 消息处理程序BOOL CChildView:PreCreateWindow(CREATESTRUCT& cs) if (!CWnd:PreCreateWindow(cs)return FALSE;cs.dwExStyle |= WS_EX_CLIENTEDGE;cs.style &= WS_BORDER;cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, :LoadCursor(NULL, IDC_ARROW), reinterpret_cast(COLOR_WINDOW+1), NULL);return TRUE;void CChildView:OnPaint() CPaintDC dc(this); / 用于绘制的设备上下文/ TODO: 在此处添加消息处理程序代码DrawGraph();/ 不要为绘制消息而调用CWnd:OnPaint()void CChildView:ReadPoint()/点表P0.x = 50; P0.y = 100;P1.x = -150;P1.y = 300;P2.x = -250;P2.y = 50;P3.x = -150;P3.y = -250;P4.x = 0; P4.y = -50;P5.x = 100; P5.y = -250;P6.x = 300; P6.y = 150;void CChildView:DrawPolygon(CDC *pDC)/绘制多边形边界CLine *line = new CLine;CP2 t;for(int i = 0; i MoveTo(pDC, Pi);t = Pi;elseline-LineTo(pDC, Pi);line-LineTo(pDC, t);/闭合多边形delete line;void CChildView:DrawGraph()/绘制图形CRect rect; /定义客户区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);/释放DCvoid CChildView:FillPolygon(CDC *pDC)/填充多边形for(int i = 0; i SetPoint(P1, 7);/初始化Fill对象fill-CreateBucket();/建立桶表fill-CreateEdge();/建立边表 fill-Gouraud(pDC);/填充多边形delete fill;/撤销内存int CChildView:OnCreate(LPCREATESTRUCT lpCreateStruct)if (CWnd:OnCreate(lpCreateStruct) = -1)return -1;/ TODO: 在此添加您专用的创建代码bFill = FALSE;ReadPoint();return 0;void CChildView:OnDrawPic()/ TODO: 在此添加命令处理程序代码COLORREF GetClr = RGB(0, 0, 0);/调色板颜色CColorDialog ccd(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 类的实现#include stdafx.h#include demo.h#include demoDoc.h#include demoView.h#include #define Round(d) int(floor(d+0.5)/四舍五入宏定义#ifdef _DEBUG#define new DEBUG_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()BOOL CdemoView:PreCreateWindow(CREATESTRUCT& cs)/ TODO: 在此处通过修改/ CREATESTRUCT cs 来修改窗口类或样式return CView:PreCreateWindow(cs);/ CdemoView 绘制void CdemoView:OnDraw(CDC* /*pDC*/)CdemoDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);if (!pDoc)return;/ TODO: 在此处为本机数据添加绘制代码DrawGraph();/ CdemoView 打印void CdemoView:OnFilePrintPreview()AFXPrintPreview(this);BOOL CdemoView:OnPreparePrinting(CPrintInfo* pInfo)/ 默认准备return DoPreparePrinting(pInfo);void CdemoView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: 添加额外的打印前进行的初始化过程void CdemoView:OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: 添加打印后进行的清理过程void CdemoView:OnRButtonUp(UINT nFlags, CPoint point)ClientToScreen(&point);OnContextMenu(this, point);void CdemoView:OnContextMenu(CWnd* pWnd, CPoint point)theApp.GetContextMenuManager()-ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);void CdemoView:ReadPoint()/点表P0.x = 50; P0.y = 100;P1.x = -150;P1.y = 300;P2.x = -250;P2.y = 50;P3.x = -150;P3.y = -250;P4.x = 0; P4.y = -50;P5.x = 100; P5.y = -250;P6.x = 300; P6.y = 150;void CdemoView:DrawPolygon(CDC *pDC) for(int i = 0; i MaxX)MaxX = Pi.x;if(Pi.x MaxY)MaxY = Pi.y;if(Pi.y MinY)MinY = Pi.y;CLine *line = new CLine;CP2 t;for(int i = 0; i MoveTo(pDC, Pi);t = Pi;elseline-LineTo(pDC, Pi);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);delete line;void CdemoView:FillPolygon(CDC *pDC) COLORREF BClr = RGB(255, 255, 255);/背景色COLORREF FClr = GetClr;/填充色int ymin, ymax;/边的最小y值与最大y值double x, y, k;/x,y当前点,k斜率的倒数for(int i = 0; i 7; i+)/循环多边形所有边int j = (i + 1) % 7;k = (Pi.x - Pj.x) / (Pi.y - Pj.y);/计算/kif(Pi.y Pj.y)/得到每条边y的最大值与最小值ymin = Round(Pi.y);ymax = Round(Pj.y);x = Pi.x;/得到x|yminelseymin = Round(Pj.y);ymax = Round(Pi.y);x = Pj.x;for(y = ymin; y ymax; y+)/沿每一条边循环扫描线for(int m = Round(x); m GetPixel(m, Round(y)/如果是填充色pDC-SetPixelV(m, Round(y), BClr);/置为背景色elsepDC-SetPixelV(m, Round(y), FClr);/置为填充色x += k;/计算下一条扫描线的x起点坐标void CdemoView:DrawGraph()/绘制图形CRect rect;/定义客户区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/ CdemoView 诊断#ifdef _DEBUGvoid CdemoView:AssertValid() constCView:AssertValid();void CdemoView:Dump(CDumpContext& dc) constCView:Dump(dc);CdemoDoc* CdemoView:GetDocument() const / 非调试版本是内联的ASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CdemoDoc);return (CdemoDoc*)m_pDocument;#endif /_DEBUG/ CdemoView 消息处理程序void CdemoView:OnInitialUpdate()CView:OnInitialUpdate();/ TODO: 在此添加专用代码和/或调用基类bFill = FALSE;ReadPoint();GetClr = RGB(0,0,0);MinX = MaxX = P0.x;MinY = MaxY = P0.y;void CdemoView:OnDrawPic()/ TODO: 在此添加命令处理程序代码CColorDialog ccd(GetClr, CC_SOLIDCOLOR);if(IDOK = ccd.DoModal()/调用颜色对话框选取填充色GetClr = ccd.GetColor();elsereturn;bFill = TRUE;Invalidate(FALSE); 3.使用MFC技术实现种子填充算法,参考界面效果如下:/ demoView.cpp : CdemoView 类的实现#include stdafx.h#include demo.h#include demoDoc.h#include demoView.h#include #define Round(d) int(floor(d+0.5)/四舍五入宏定义#ifdef _DEBUG#define new DEBUG_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_WM_LBUTTONDOWN()ON_COMMAND(ID_DRAW_PIC, &CdemoView:OnDrawPic)END_MESSAGE_MAP()/ CdemoView 构造/析构CdemoView:CdemoView()/ TODO: 在此处添加构造代码bFill = FALSE;SeedClr = RGB(255, 0, 0);CdemoView:CdemoView()BOOL CdemoView:PreCreateWindow(CREATESTRUCT& cs)/ TODO: 在此处通过修改/ CREATESTRUCT cs 来修改窗口类或样式return CView:PreCreateWindow(cs);/ CdemoView 绘制void CdemoView:OnDraw(CDC* /*pDC*/)CdemoDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);if (!pDoc)return;/ TODO: 在此处为本机数据添加绘制代码DrawGraph();/ CdemoView 打印void CdemoView:OnFilePrintPreview()AFXPrintPreview(this);BOOL CdemoView:OnPreparePrinting(CPrintInfo* pInfo)/ 默认准备return DoPreparePrinting(pInfo);void CdemoView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: 添加额外的打印前进行的初始化过程void CdemoView:OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: 添加打印后进行的清理过程void CdemoView:OnRButtonUp(UINT nFlags, CPoint point)ClientToScreen(&point);OnContextMenu(this, point);void CdemoView:OnContextMenu(CWnd* pWnd, CPoint point)theApp.GetContextMenuManager()-ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);/ CdemoView 诊断#ifdef _DEBUGvoid CdemoView:AssertValid() constCView:AssertValid();void CdemoView:Dump(CDumpContext& dc) constCView:Dump(dc);CdemoDoc* CdemoView:GetDocument() const / 非调试版本是内联的ASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CdemoDoc);return (CdemoDoc*)m_pDocument;#endif /_DEBUGvoid CdemoView:DrawGraph()/绘制图形CDC *pDC = GetDC();/定义设备上下文指针GetClientRect(&rect);/获得客户区的大小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);/矩形与客户区重合CDC MemDC;/内存DCCBitmap NewBitmap, *pOldBitmap;/内存中承载图像的位图MemDC.CreateCompatibleDC(pDC);/建立与屏幕pDC兼容的MemDC NewBitmap.LoadBitmap(IDB_BITMAP1);/导入空心汉字位图pOldBitmap = MemDC.SelectObject(&NewBitmap);/将兼容位图选入MemDC MemDC.SetMapMode(MM_ANISOTROPIC);/MemDC自定义坐标系MemDC.SetWindowExt(rect.Width(), rect.Height();MemDC.SetViewportExt(rect.Width(), -rect.Height();MemDC.SetViewportOrg(rect.Width() / 2, rect.Height() / 2);pDC-BitBlt(-rect.Width() / 2, -rect.Height() / 2, rect.Width(), rect.Height(), &MemDC, -rect.Width() / 2, -rect.Height() / 2, SRCCOPY);/将内存位图拷贝到屏幕if(bFill)CharFill(pDC);/填充空心汉字MemDC.SelectObject(pOldBitmap);/恢复位图NewBitmap.DeleteObject();/删除位图MemDC.DeleteDC();/删除MemDCReleaseDC(pDC);/释放DCvoid CdemoView:CharFill(CDC *pDC)/文字填充函数COLORREF BoundaryClr = RGB(0,0,0);/边界色BOOL bSpanFill;pHead = new CStackNode;/建立栈结点pHead-pNext = NULL;/栈头结点的指针域总为空Push(Seed);/种子像素入栈int x, y, x0 = Round(Seed.x), y0 = Round(Seed.y);/x,y用于判断种子与图形的位置关系x = x0 - 1;while(pDC-GetPixel(x, y0) != BoundaryClr & pDC-GetPixel(x, y0) != SeedClr )/左方判断x-;if(x GetPixel(x0, y) != BoundaryClr & pDC-GetPixel(x0, y) != SeedClr)/上方判断y+;if(y = rect.Height() / 2)/到达客户区最上端MessageBox(L种子不在图形之内, L警告);return;x = x0 + 1;while(pDC-GetPixel(x, y0) != BoundaryClr & pDC-GetPixel(x, y0) != SeedClr)/右方判断x+;if(x = rect.Width() / 2)/到达客户区最右端MessageBox(L种子不在图形之内, L警告);return;y = y0 - 1;while(pDC-GetPixel(x0, y) != BoundaryClr & pDC-GetPixel(x0, y) != SeedClr)/下方判断y-;if(y pNext != NULL)/如果栈不为空Pop(PopPoint);if(pDC-GetPixel(Round(PopPoint.x), Round(PopPoint.y) = SeedClr)continue;/ 分别向左和向右填充扫描线PointTemp = PopPoint;while(pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) != BoundaryClr & pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) != SeedClr)pDC-SetPixelV(Round(PointTemp.x), Round(PointTemp.y), SeedClr);PointTemp.x+;xright = PointTemp.x - 1;PointTemp.x = PopPoint.x - 1;while(pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) != BoundaryClr & pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) != SeedClr)pDC-SetPixelV(Round(PointTemp.x), Round(PointTemp.y), SeedClr);PointTemp.x-;xleft=PointTemp.x + 1;/处理上一条扫描线PointTemp.x = xleft;PointTemp.y = PointTemp.y + 1;while(PointTemp.x GetPixel(Round(PointTemp.x), Round(PointTemp.y) != BoundaryClr & pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) != SeedClr)bSpanFill = TRUE;PointTemp.x+;if(bSpanFill)if(PointTemp.x = xright & pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) != BoundaryClr & pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) != SeedClr)PopPoint = PointTemp;elsePopPoint.x = PointTemp.x - 1; PopPoint.y = PointTemp.y;Push(PopPoint);bSpanFill = FALSE;while(pDC-GetPixel(Round(PointTemp.x), Round(PointTemp.y) = BoundaryClr & PointTemp.x GetPixel(Round(PointTemp.x), Round(PointTemp.y) = SeedClr & PointTemp.x xright) PointTemp.x+;/处理下一条扫描线PointTemp.x = xleft;PointTemp.y = PointTemp.y - 2;while(PointTemp.x GetPixel(Round(PointTemp.x), Round(PointTemp.y) != BoundaryClr & pDC-GetPixel(Round(PointT
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 旅游管理案例研究练习题
- 学科交叉融合促进应用型人才综合素质发展
- 零售电商行业销售趋势统计表
- 汽车工程维修技术知识点解析
- 2025年文化传播与互联网的综合能力考核考试卷及答案
- 2025年现代诗歌鉴赏能力考试试卷及答案
- 2025年数理逻辑与数学思维考试试题及答案
- 2025年审计学基础理论与实务能力提高测试卷及答案
- 2025年人工智能伦理与社会影响知识测试卷及答案
- 2025年绿色经济与可持续发展考试卷及答案
- 2025年庐山市国有投资控股集团有限公司招聘笔试冲刺题(带答案解析)
- 生物基可降解地膜行业深度调研及发展项目商业计划书
- 出租车租凭合同协议书
- GB/T 24217-2025洗油
- 2025年天津市西青区八年级会考模拟生物试卷(含答案)
- 宁波辅警考试题库2024
- 2025年中考地理真题试题(含解析)
- 2025年社区工作者考试试题及答案
- 软件知识产权授权管理框架与合规性研究
- ISO9001质量管理体系培训考试试题含答案
- 2025-2030中国雷达告警接收机行业市场发展趋势与前景展望战略研究报告
评论
0/150
提交评论