呵呵,这个算法终于实现了,在运行这个程序的时侯, 不停的按回车键,可以看到种子填充的路径.呵呵挺好玩的.
/* WIN-TC BGI 图形编程模板 */
#include "Conio.h" #include "graphics.h" #define closegr closegraph
void initgr(void) /* BGI初始化 */ {int gd=DETECT,gm=0; /* 和gd=VGA,gm=VGAHI是同样效果 */ registerbgidriver(EGAVGA_driver);/* 注册BGI驱动后可以不需要.BGI文件的支持运行 */ initgraph(&gd,&gm,""); } void seedfilling(x,y,fill_color,boundary_color)
int x,y,fill_color,boundary_color;
{
int c;
c=getpixel(x,y); /*获取当前点的颜色*/
if((c!=boundary_color)&&(c!=fill_color)) /*如果颜色为边界色则不填充*/
{
putpixel(x, y, fill_color); /*画点*/ getch(); /*加上这条语句可以显示填充状态 */
seedfilling(x+1,y, fill_color, boundary_color);
seedfilling(x-1,y, fill_color, boundary_color);
seedfilling(x, y+1, fill_color, boundary_color);
seedfilling(x, y-1, fill_color, boundary_color);
}
}
void main() { int a,b,color;
int gd=DETECT , gm; int poly[10]; a=150 ; b=140; color=4; initgraph(&gd , &gm , "");
poly[0] = 110; /* 第一个点的x坐标以及y坐标 */ poly[1] = 110;
poly[2] = 200; /* 第二点 */ poly[3] = 105;
poly[4] = 170; /* 第三点 */ poly[5] = 120;
poly[6]=150; /*第四点*/ poly[7]=170;
poly[8]=110; /*多边形的起点与终点一样*/ poly[9]=110; drawpoly(5,poly);/* 显示各点连接起来的多边形 */ seedfilling(a,b,color,15); /*种子填充多边形*/
getch(); closegraph(); }

|