实验三 区域填充算法的实现
班级10信计2班 学号姓名
一、实验目的和要求:
1、掌握区域填充算法基本知识
2、理解区域的表示和类型,能正确区分四连通和八连通的区域
3、了解区域填充的实现原理,利用Microsoft Visual C++ 6.0或win-TC实现区 域种子填充的递归算法。
二、实验内容:
1、编程完成区域填色
2、利用画线函数,在屏幕上定义一个封闭区域。
3、利用以下两种种子填充算法,填充上述步骤中定义的区域
边界表示的四连通区域种子填充的实现
内点表示的四连通区域种子填充的实现
4、将上述算法作部分改动应用于八连通区域,构成八连通区域种子填充算法, 并编程实现。
三、实验结果分析
四连通图的实现:
程序代码:
#includeh>
#include
# include
#include
void BoundaryFill4(int x,int yjnt Boundarycolor^int newcolor)
{
if(getpixel(x,y) != newcolor && getpixel(x,y) !=Boundarycolor)
{
putpixel(x^newcolor);
Sleep(l);
BoundaryFin4(x-l9y,Boundarycolor,newcolor);
BoundaryFill4(x,y+l,Boundarycolor,newcolor);
BoundaryFill4(x+l^Boundarycolor,newcolor); BoundaryFill4(x,y-l,BoundarycoIor^newcolor);
}
}
void polygon(int xOJnt yO^int a,int n^float af)
int x,y,i;
double dtheta,theta;
if(n<3)
return;
dtheta=6.28318/n; theta=af*0.0174533; moveto(xO,yO);x=xO;y=yO;
for(i=l;i
{
x=x+a*cos(theta);y=y+a*sin(theta); lineto(x,y);theta=theta+dtheta;
}
Iineto(x0,y0);
}
void main()
{
int x=50,y=75;
int a,b,c,d,ij;
int graphdriver=DETECT;
int graphmode=0;
initgraph(&graphdriver,&graphmode9n "); cleardevice();
setcolor(RGB(0,255,0));
setfillstyle(WHITE);
polygon(x,y,60,5,0.);
a=100;
b=100;
c=RGB(0,255,0);
d=RGB(255,0,255);
BoundaryFill4(a,b,c,d);
getch();
closegraph();}
实验结果
■ liantong4
□—冈
八连通的实现
程序代码:
#include
#includeh>
#include
#include
#include
#define MaxSize 100
typedef struct!
int x;
inty;
}Seed,ElemType;
typedef struct
{
ElemTS pe data[MaxSize];
int top;〃栈顶指针
} SqStack;
void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-l;
}
int StackEmpty(SqStack *s)
{
return(s ?> top==-l);
}
int Push(SqStack *&s,EIemType e)
{
if (s->top==MaxSize-1)
return 0;
s->top++;
s->data[s->top]=e;
return 1;
}
int Pop(SqStack *&s,ElemType &e)
{
if (s->top==-l)
return 0;
e=s->data[s->top];
s->top—;
return 1;
void floodfi!18(int x,int yjnt oldcolorjnt nevvcolor)
if(getpixel(x,y)==oldcol