📄 3-1.c
字号:
/*种子填充*/
/*坐标系:屏幕左上角为原点,向下为纵坐标正方向*/
/*可修改源程序多边形定义(同时定义下面的按顶点数作为的循环次数),并修改相应的种子点*/
/*单步执行到某阶段发现屏幕不出现改变,因为堆栈正抛掉大量无用的点,
暴露本算法的弱点——待填充点入栈过程中填入大量不必要或重复的点信息*/
#include<graphics.h>/*加入c图形库*/
#include<conio.h>/*键盘控制*/
#define LEN sizeof(struct node)
#include <malloc.h>
struct node/*存放各点横纵坐标的指针链表*/
{int dx,dy;
struct node *next;
};
struct node * create()/*建立多边形*/
{
struct node *h, *p, *r;
int i;
int n[][2]={{150,50},{180,150},{200,80},{190,180},{150,180}};/*多边形各顶点顺时针定义*/
h=NULL;
for(i=0;i<=4;i++)/*循环次数为多边形顶点数*/
{p=(struct node *)malloc(LEN);
p->dx=n[i][0];
p->dy=n[i][1];
if(h==NULL)h=p;
else r->next=p;
r=p;
}
r->next=NULL;
return(h);
}
void draw_polo(h)/*多边形描边*/
struct node *h;
{
int sx,sy;
struct node *q;
setcolor(14);
q=h;
sx=q->dx;
sy=q->dy;
q=q->next;
while(q!=NULL)
{ if(getch()==17)exit();/*读入键盘按键*/
line(sx,sy,q->dx,q->dy);
sx=q->dx;
sy=q->dy;
q=q->next;
}
q=h;
line(sx,sy,q->dx,q->dy);
}
void main()
{
struct node *head,*sh,*q,*r;
int sx,sy,color;
int driver,mode;
driver=DETECT;/*初始化显示模式参数*/
initgraph(&driver,&mode,"..\\bgi");/*初始化显示为VGA驱动的640*480、16色模式*/
printf("Press any key to continue except 'Ctrl+Q' to quit.\n");
head=create();
draw_polo(head);
sx=180;sy=160;/*种子点*/
sh=(struct node *)malloc(LEN);
sh->dx=sx;
sh->dy=sy;
sh->next=NULL;
while(sh!=NULL)
{
sx=sh->dx;
sy=sh->dy;
r=sh;
sh=sh->next;
free(r);
color=getpixel(sx,sy);
if(color!=12)putpixel(sx,sy,12);
/*以下判断当前点上下左右四点状态*/
color=getpixel(sx,sy-1);
if((color!=14)&&(color!=12))
{
r=(struct node*)malloc(LEN);
if(r==NULL)printf("null!");/*申请空间失败*/
r->dx=sx;
r->dy=sy-1;
r->next=sh;
sh=r;
}
color=getpixel(sx,sy+1);
if((color!=14)&&(color!=12))
{
r=(struct node *)malloc(LEN);
if(r==NULL)printf("null!");/*申请空间失败*/
r->dx=sx;
r->dy=sy+1;
r->next=sh;
sh=r;
}
color=getpixel(sx-1,sy);
if((color!=14)&&(color!=12))
{
r=(struct node *)malloc(LEN);
if(r==NULL)printf("null!");/*申请空间失败*/
r->dx=sx-1;
r->dy=sy;
r->next=sh;
sh=r;
}
color=getpixel(sx+1,sy);
if((color!=14)&&(color!=12))
{
r=(struct node *)malloc(LEN);
if(r==NULL)printf("null!");/*申请空间失败*/
r->dx=sx+1;
r->dy=sy;
r->next=sh;
sh=r;
}
if(getch()==17)exit();}/*读入键盘按键*/
closegraph();/*关闭图形模式*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -