多边形的填充实验经典.doc_第1页
多边形的填充实验经典.doc_第2页
多边形的填充实验经典.doc_第3页
多边形的填充实验经典.doc_第4页
多边形的填充实验经典.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

试验实验一:图形的区域填充一、实验目的区域填充是指先将区域内的一点(常称为种子点)赋予给定颜色,然后将这种颜色扩展到整个区域内的过程。区域填充技术广泛应用于交互式图形、动画和美术画的计算机辅助制作中。本实验采用递归填充算法或扫描线算法实现对光栅图形的区域填充。通过本实验,可以掌握光栅图形编程的基本原理和方法。二、实验内容掌握光栅图形的表示方法,实现种子算法或扫描线算法。通过程序设计实现上述算法。建议采用VC+实现OpenGL程序设计。三、实验原理、方法和手段递归算法在要填充的区域内取一点(X,Y)的当前颜色记为oldcolor,用要填充的颜色newcolor去取代,递归函数如下:procedure flood-fill(X,Y,oldcolor,newcolor:integer);begin if getpixel(framebuffer,x,y)=oldcolor then begin setpixel(framebuffer,x,y,newcolor); flood-fill(X,Y+1,oldcolor,newcolor); flood-fill(X,Y-1,oldcolor,newcolor); flood-fill(X-1,Y,oldcolor,newcolor); flood-fill(X+1,Y,oldcolor,newcolor); endend扫描线算法扫描线算法的效率明显高于递归算法,其算法的基本思想如下:(1)(初始化)将算法设置的堆栈置为空,将给定的种子点(x,y)压入堆栈。(2)(出栈)如果堆栈为空,算法结束;否则取栈顶元素(x,y)作为种子点。(3)(区段填充)从种子点(x,y)开始沿纵坐标为y的当前扫描线向左右两个方向逐个象素进行填色,其值置为newcolor,直到抵达边界为止。(4)(定范围)以xleft和xright分别表示在步骤3中填充的区段两端点的横坐标。(5)(进栈)分别在与当前扫描线相邻的上下两条扫描线上,确定位于区间xleft,xright内的给定区域的区段。如果这些区段内的象素的颜色值为newcolor或者boundarycolor(边界上象素的颜色值),则转到步骤2,否则取区段的右端点为种子压入堆栈,再转到步骤2继续执行。四、实验组织运行要求本实验采用集中授课形式,每个同学独立完成上述实验要求。五、实验条件每人一台计算机独立完成实验。六、实验步骤、结果及部分代码(1)将图形显示在初始位置。(2)给定种子点的坐标。(3) 显示从种子点开始的扩散过程。(4) 显示填充后的图形。部分代码/ 填充Dlg.cpp : implementation file/#include stdafx.h#include 填充.h#include 填充Dlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg();/ Dialog Data/AFX_DATA(CAboutDlg)enum IDD = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:/AFX_MSG(CAboutDlg)/AFX_MSGDECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CMyDlg dialogCMyDlg:CMyDlg(CWnd* pParent /*=NULL*/): CDialog(CMyDlg:IDD, pParent)/AFX_DATA_INIT(CMyDlg)/ NOTE: the ClassWizard will add member initialization here/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);num = 0;col = RGB(0,0,0);ldb = FALSE;step = 0;void CMyDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CMyDlg)/ NOTE: the ClassWizard will add DDX and DDV calls here/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CMyDlg, CDialog)/AFX_MSG_MAP(CMyDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON4, OnButton4)ON_BN_CLICKED(IDC_BUTTON1, OnButton1)ON_WM_LBUTTONDOWN()ON_WM_LBUTTONUP()ON_WM_MOUSEMOVE()ON_BN_CLICKED(IDC_BUTTON2, OnButton2)ON_BN_CLICKED(IDC_BUTTON3, OnButton3)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CMyDlg message handlersBOOL CMyDlg:OnInitDialog()CDialog:OnInitDialog();/ Add About. menu item to system menu./ IDM_ABOUTBOX must be in the system command range.ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework does this automatically/ when the applications main window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small icon/ TODO: Add extra initialization hereCPaintDC dc(this);pic.CreateCompatibleDC(&dc);CBitmap * bp1,*obp1;bp1 = new CBitmap;bp1-LoadBitmap(IDB_B);obp1 = pic.SelectObject(bp1);obp1-DeleteObject();return TRUE; / return TRUE unless you set the focus to a controlvoid CMyDlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialog:OnSysCommand(nID, lParam);/ If you add a minimize button to your dialog, you will need the code below/ to draw the icon. For MFC applications using the document/view model,/ this is automatically done for you by the framework.void CMyDlg:OnPaint() CPaintDC dc(this); / device context for paintingif (IsIconic()SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;/ Draw the icondc.DrawIcon(x, y, m_hIcon);elseCDialog:OnPaint();CDC mdc;CBitmap bp,*ob;bp.LoadBitmap(IDB_B);mdc.CreateCompatibleDC(GetDC();ob = mdc.SelectObject(&bp);mdc.BitBlt(0,0,520,330,&pic,0,0,SRCCOPY);mdc.MoveTo(mx0,my0);for(int i = 1;iGetDlgItem(IDC_S);dc1 = wd1-GetDC();dc1-BitBlt(1,1,517,327,&mdc,0,0,SRCCOPY);mdc.SelectObject(ob);/ The system calls this to obtain the cursor to display while the user drags/ the minimized window.HCURSOR CMyDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;void CMyDlg:OnButton4() / TODO: Add your control notification handler code hereOnOK();void CMyDlg:OnButton1() / TODO: Add your control notification handler code hereif(step = 1)step = 0;SetDlgItemText(IDC_BUTTON1,画-多边?形?);Invalidate(FALSE);elsenum = 0;step = 1;ldb = FALSE;SetDlgItemText(IDC_BUTTON1,绘?制?完毕?);CDC mdc;CBitmap bmp,*obp;bmp.LoadBitmap(IDB_B);mdc.CreateCompatibleDC(GetDC();obp = mdc.SelectObject(&bmp);pic.BitBlt(0,0,520,330,&mdc,0,0,SRCCOPY);mdc.SelectObject(obp);Invalidate(FALSE);void CMyDlg:OnLButtonDown(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call defaultif(step = 1&point.x11&point.x11&point.y339)if(num49)ldb = TRUE;if(!num)mxnum = point.x-12;mynum = point.y-12;num +;num +;/Invalidate(FALSE);CDialog:OnLButtonDown(nFlags, point);void CMyDlg:OnLButtonUp(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call defaultif(ldb)ldb = FALSE;mxnum-1 = point.x - 12;mynum-1 = point.y - 12;Invalidate(FALSE);CDialog:OnLButtonUp(nFlags, point);void CMyDlg:OnMouseMove(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call defaultif(ldb)mxnum-1 = point.x - 12;mynum-1 = point.y - 12;Invalidate(FALSE);CDialog:OnMouseMove(nFlags, point);void CMyDlg:OnButton2() / TODO: Add your control notification handler code hereCColo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论