计算机图形学图形区域填充效果
- 格式:doc
- 大小:322.00 KB
- 文档页数:10
宁夏师范学院数学与计算机科学学院
《计算机图形学》实验报告
实验序号:6 实验项目名称:图形区域填充效果
学 号 姓 名 专业、班
实验地点 指导教师 时 间
一、实验目的:
编写区域填充算法程序,验证算法的正确性。
任务:
1、学习种子填充算法的 3 种类型;
2、设计扫描线填充算法;
3、对给定多边形实现填充的程序设计。
二、实验内容与步骤
(1)种子填充算法原理
在多边形内部找到一个已知的象素点作为种子点,由此开始,利用区域的连通性找到多边形内部的 其它所有象素点进行填充。
(i)四向连通区域
①四向连通区域概念:从区域上任一点出发,在不超出区域边界的前提下,可通过4个方向:上、下、左、右的移动组合到达区域中的任意象素点,称此区域为四向连通区域。
②四向填充算法 允许从 4 个方向搜索下一个象素的填算法称为是四向填充算法。
(ii)八向连通区域
①八向连通区域概念:从区域上任一点出发,在不超出区域边界的前提下,可通过 8个方向:上、下、左、右、左上、左 下、右上、右下的移动组合,到达区域中的任意象素,称此区域为四向连通区域。
②八向填充算法允许从8个方向搜索下一个象素的填充算法称为是八向填充算法。
(2)种子填充算法分类
(i)递归填充算法
初始化:种子象素入栈;
步骤 1:栈顶象素出栈,作为种子点;
步骤 2:种子点被置为填充色;
步骤 3:按照左、上、右、下顺序检查与种子点相邻的象素:若非边界且
未被填充,则入栈(8 向连通区域需考虑更多相邻象素);
步骤 4:若栈不空,则重复第一步。
(ii)扫描线算法
初始化:由指定的种子象素点(x,y)生成种子(y,xl,xr)填充区间并入 栈(xl,xr分别为种子点所在扫描线上多边形内部区间的左、右端点);
步骤 1:若种子栈空则算法终止,否则栈顶种子出栈;
步骤 2:确定新种子:分别确定 y+1,y-1 扫描线上与(y,xl,xr)连通的
区间;填充新区间并将新 种子压入堆栈;
步骤 3:上述过程循环执行。
(3)扫描区域填充算法设计
Step1:创建 MFC 单文档应用程序,并命名为 FloodFill,如图所示;
Step2:向工程中添加 Generic Class,命名为 CStack,创建界面如下图(左)所示;
Step3:向该工程中添加 MFC Class,命名为 CFill,且该类继承于 CView 类,对应的创建界面如上图(右)所示;
CStack.h参考代码:
// Stack.h: interface for the CStack class.
#if !defined(AFX_STACK_H__D198F788_4ED1_4C09_98E5_433BAB24D864__INCLUDED_) #define
AFX_STACK_H__D198F788_4ED1_4C09_98E5_433BAB24D864__INCLUDED_
#if _MSC_VER > 1000
#pragma once #endif // _MSC_VER > 1000
class CStack
{
public:
void SetEmpty();
int IsEmpty();
void Push(int data);
int Pop();
int top;
int Data[10000];
CStack();
virtual ~CStack();
};
#endif
// !defined(AFX_STACK_H__D198F788_4ED1_4C09_98E5_433BAB24D864__INCLUDED_)
CStack.cpp参考代码:
// Stack.cpp: implementation of the CStack class.
#include "stdafx.h"
#include "FloodFill.h"
#include "Stack.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// Construction/Destruction
CStack::CStack()
{
}
CStack::~CStack()
{
}
int CStack::Pop()
{
int temp = Data[top-1];
top-=1;
return temp;
}
void CStack::Push(int data) {
Data[top]=data;
top+=1;
} int CStack::IsEmpty()
{
if(top>0)
{
return 1;
}
else
{
return 0;
}
}
void CStack::SetEmpty()
{
for(int i=0;i
{
Data[i]=0;
}
top=0;
}
CFill.h 参考代码:
#if !defined(AFX_FILL_H__237EA388_BCAB_4977_B748_FA421BF12C85__INCLUDED_)
#define AFX_FILL_H__237EA388_BCAB_4977_B748_FA421BF12C85__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Fill.h : header file
// CFill view
class CFill : public CView
{
protected:
DECLARE_DYNCREATE(CFill)
public:
CFill();
virtual ~CFill();
void floodfill4(CDC *pDC, int x0, int y0, COLORREF newcolor,
COLORREF oldcolor); // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CFill)
protected:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
//}}AFX_VIRTUAL
// Implementation
#ifdef _DEBUG
virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const;
#endif
// Generated message map functions
protected:
//{{AFX_MSG(CFill)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_FILL_H__237EA388_BCAB_4977_B748_FA421BF12C85__INCLUDED_)
CFill.cpp 参考代码:
// Fill.cpp : implementation file
#include "stdafx.h"
#include "FloodFill.h"
#include "Fill.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CFill
IMPLEMENT_DYNCREATE(CFill, CView)
CFill::CFill()
{
}
CFill::~CFill()
{
}
BEGIN_MESSAGE_MAP(CFill, CView)
//{{AFX_MSG_MAP(CFill)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CFill drawing
void CFill::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();