
计算机图形学中的种子填充算法(MFC、VC)
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
本段落介绍计算机图形学中基于MFC和VC环境下的种子填充算法原理及其应用,探讨其实现细节与优化方法。
种子填充算法实现代码如下:
```cpp
// 种子法View.cpp : CMyView类的实现
#include stdafx.h
#include 种子法.h
#include 种子法Doc.h
#include 种子法View.h
#ifdef _DEBUG
#define new DEBUG_NEW
static char THIS_FILE[] = __FILE__;
#endif
struct point {
int x;
int y;
};
point p[10] = {200, 100, 100, 200, 150, 100, 200, 300, 250, 100, 300, 200};
point stack[1024];
int top;
void push(int x,int y) {
if(top > sizeof(stack)/sizeof(point)) exit(0);
stack[top].x = x;
stack[top].y = y;
top++;
}
void pop(int &x, int &y) {
if(top == 0) exit(0);
x = stack[--top].x;
y = stack[--top].y;
}
void gettop(int &x, int &y) {
if(top == 0) exit(0);
x = stack[top - 1].x;
y = stack[top - 1].y;
}
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_WM_LBUTTONDOWN()
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrintDirect)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
END_MESSAGE_MAP()
// CMyView 构造和析构函数
CMyView::CMyView() {}
CMyView::~CMyView() {}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs) {
return CView::PreCreateWindow(cs);
}
void CMyView::OnLButtonDown(UINT nFlags, CPoint point) {
int x,y;
CClientDC dc(this);
origin = point;
push(origin.x,origin.y);
while(top != 0) {
pop(x,y);
if(dc.GetPixel(x-1,y)!=0)
{dc.SetPixel(x-1,y,0);push(x-1,y);}
if(dc.GetPixel(x+1,y)!=0)
{dc.SetPixel(x+1,y,0);push(x+1,y);}
if(dc.GetPixel(x,y-1)!=0)
{dc.SetPixel(x,y-1,0); push(x,y-1);}
if(dc.GetPixel(x,y+1)!=0)
{dc.SetPixel(x,y+1,0); push(x,y+1);}
}
CView::OnLButtonDown(nFlags, point);
}
void CMyView::OnDraw(CDC* pDC){
CClientDC dc(this);
dc.TextOut(1,5,请为每个区选种子,务必在图形内);
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int i;
for(i=0;p[i+1].x!=-1;i++)
{dc.MoveTo(p[i].x,p[i].y); dc.LineTo(p[i+1].x,p[i+1].y);}
dc.MoveTo(p[i].x, p[i].y);
dc.LineTo(p[0].x, p[0].y);
}
// CMyView 打印准备
BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo) {
return DoPreparePrinting(pInfo);
}
void CMyView::OnBeginPrinting(CDC*,CPrintInfo*) {}
void CMyView::OnEndPrinting(CDC*,CPrintInfo*) {}
#ifdef _DEBUG
// 调试支持函数
void CMyView::AssertValid() const {CView::AssertValid();}
void CMyView::Dump(CDumpContext& dc) const{CView::Dump(dc);}
#endif //_DEBUG
```
该代码实现了种子填充算法,允许用户在图形中选择一个点作为起点,并通过递归地向相邻像素进行填充来形成区域。
全部评论 (0)


