?? 3-1.c
字號:
/*種子填充*/
/*坐標系:屏幕左上角為原點,向下為縱坐標正方向*/
/*可修改源程序多邊形定義(同時定義下面的按頂點數(shù)作為的循環(huán)次數(shù)),并修改相應的種子點*/
/*單步執(zhí)行到某階段發(fā)現(xiàn)屏幕不出現(xiàn)改變,因為堆棧正拋掉大量無用的點,
暴露本算法的弱點——待填充點入棧過程中填入大量不必要或重復的點信息*/
#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++)/*循環(huán)次數(shù)為多邊形頂點數(shù)*/
{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;/*初始化顯示模式參數(shù)*/
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);
/*以下判斷當前點上下左右四點狀態(tài)*/
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 + -