MFC实现种子填充算法-图形学4-9

算法原理:
种子元素入栈,如果栈不为空,执行以下三步:
(1)栈顶元素出栈
(2)按颜色绘制出栈元素
(3)按左,左上,上,右上,右,……八个方向顺序搜索与出栈像素相邻的像素,若该像素的颜色不是边界色,并且未被设置为填充色则入栈,否则丢弃。
实现:
MFC新建单文档工程Test,在TestView.h中的类定义中public中添加代码
CPoint p[9];//定义多边形 CPoint seed;//种子 CPoint one,two,three,four,five,six,seven,eight;//邻接点 void Fill();//填充函数
TestView.cpp中加头文件

#include "Test.h"
#include<stack>  

构造函数中初始化点
p[0]=CPoint(300,300); p[1]=CPoint(401,300); p[2]=CPoint(401,201); p[3]=CPoint(500,201); p[4]=CPoint(500,100); p[5]=CPoint(400,100); p[6]=CPoint(400,200); p[7]=CPoint(300,200);
然后OnDraw函数中
pDC->Polygon(p,8);//绘制多边形
然后实现Fill函数

void CTestView::Fill(){
	CClientDC *dc;
	dc=new CClientDC(this);
	COLORREF fc,bc,pc;//填充色,边界颜色,像素点颜色
	bc=RGB(0,0,0);//边界色为黑色
	fc=RGB(0,0,255);//填充色为蓝色
	
	CPoint head;
	head=seed;
	stack<CPoint> s;
	s.push(head);
	while(!s.empty())
	{
		
		CPoint base=s.top();//访问栈顶
		s.pop();//栈顶元素出栈
		dc->SetPixel(base,fc);

		one.x=base.x-1;
		one.y=base.y;
		pc=dc->GetPixel(one.x,one.y);
		if(pc!=bc&&pc!=fc)
		{
			s.push(one);
		}

		two.x=base.x-1;
		two.y=base.y-1;
		pc=dc->GetPixel(two.x,two.y);
		if(pc!=bc&&pc!=fc)
		{
			s.push(two);
		}
	
		three.x=base.x;
		three.y=base.y-1;
		pc=dc->GetPixel(three.x,three.y);
		if(pc!=bc&&pc!=fc)
		{
			s.push(three);
		}
		
		four.x=base.x+1;
		four.y=base.y-1;
		pc=dc->GetPixel(four.x,four.y);
		if(pc!=bc&&pc!=fc)
		{
			s.push(four);
		}
		
		five.x=base.x+1;
		five.y=base.y;
		pc=dc->GetPixel(five.x,five.y);
		if(pc!=bc&&pc!=fc)
		{
			s.push(five);
		}

		six.x=base.x+1;
		six.y=base.y+1;
		pc=dc->GetPixel(six.x,six.y);
		if(pc!=bc&&pc!=fc)
		{
			s.push(six);
		}

		seven.x=base.x;
		seven.y=base.y+1;
		pc=dc->GetPixel(seven.x,seven.y);
		if(pc!=bc&&pc!=fc)
		{
			s.push(seven);
		}
		eight.x=base.x-1;
		eight.y=base.y+1;
		pc=dc->GetPixel(eight.x,eight.y);
		if(pc!=bc&&pc!=fc)
		{
			s.push(eight);
		}
		
	}

}

添加按下左键事件响应,初始化seed种子
步骤查看-》建立类向导-》Message-》WM_LBUTTONDOWN-Add Class
在这里插入图片描述然后在TestView.cpp的以下函数中添加三行代码


void CTestView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	seed.x=point.x;//设置seed.x
	seed.y=point.y;
	Fill();//填充
	CView::OnLButtonDown(nFlags, point);
}

总共要添加的代码就这六处

  • 2
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
种子填充算法,自己写的,希望对大家有用 // 种子法View.cpp : implementation of the CMyView class // #include "stdafx.h" #include "种子法.h" #include "种子法Doc.h" #include "种子法View.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif struct point { int x; int y; }p[10]={200,100,100,200,150,100,200,300,250,100,300,200,-1}; point stack[1024000]; int top; void push(int x,int y) { if(top>1024000)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-1].x; y=stack[top-1].y; top--; } void gettop(int &x,int &y) { if(top==0) exit(0); x=stack[top-1].x; y=stack[top-1].y; } ///////////////////////////////////////////////////////////////////////////// // CMyView IMPLEMENT_DYNCREATE(CMyView, CView) BEGIN_MESSAGE_MAP(CMyView, CView) //{{AFX_MSG_MAP(CMyView) ON_WM_LBUTTONDOWN() //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMyView construction/destruction CMyView::CMyView() { // TODO: add construction code here } CMyView::~CMyView() { } BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CMyView drawing void CMyView::OnLButtonDown(UINT nFlags, CPoint point) { int x,y; CClientDC dc(this); // TODO: Add your message handler code here and/or call default 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); // TODO: add draw code for native data here } ///////////////////////////////////////////////////////////////////////////// // CMyView printing BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CMyView diagnostics #ifdef _DEBUG void CMyView::AssertValid() const { CView::AssertValid(); } void CMyView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMyDoc* CMyView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc))); return (CMyDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMyView message handlers

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值