Program By Rungchai */
โปรแกรม
#include"stdio.h"
#include"graphics.h"
main()
{ int gdriver=DETECT,gmode;
int x,y,colo,oldcolo;
clrscr();
initgraph(&gdriver,&gmode,"");
setcolor(8);
เป็นการสร้างตัวอย่างสี่เหลี่ยมแล้วแบ่งออกเป็น 8 ส่วนโดยใช้เส้นตรง
rectangle(200,140,500,189); /*Edit color tone*/
line(200,140,500,189);
line(200,189,500,140);
line(350,140,350,189);
line(200,165,500,165);
y=145;
setfillstyle(1,1); กำหนดแบบการระบายสี
floodfill(340,145,8); ระบายสีตามตำแหน่งที่กำหนด
setfillstyle(1,2);
floodfill(210,160,8);
setfillstyle(1,3);
floodfill(210,170,8);
setfillstyle(1,4);
floodfill(340,180,8);
setfillstyle(1,5);
floodfill(360,145,8);
setfillstyle(1,6);
floodfill(490,160,8);
setfillstyle(1,7);
floodfill(490,170,8);
setfillstyle(1,9);
floodfill(360,180,8);
rectangle(200,190,500,239);
x=200; กำหนดตำแหน่งเริ่มต้นของทางแกน X
สร้างตัวอย่างรูปวงรี
for(y=15;y>0;y--) /* Edit color ellipse */
{ setcolor(y);
setfillstyle(1,y);
fillellipse(x+10,215,10,25);
x+=20;
}
x=180; กำหนดตำแหน่งเริ่มต้นของทางแกน X
สร้างตัวอย่างรูปสี่เหลียม
for(y=0;y<=15;y++) /* Edit color table(rectangle) */
{ setcolor(y);
rectangle(x,240,x+19,290);
setfillstyle(1,y);
floodfill(x+1,241,y);
x+=20;
}
กำหนดตำแหน่งเริ่มต้นและสีที่ต้องการระบายแทนสีเก่า
printf("Enter X position (x=200 to 500) : ");
scanf("%d",&x); /*Recive position of fill*/
if(x<200||x>500)
main();
printf("Enter Y position (y=140 to 290) : ");
scanf("%d",&y); /*Recive position of fill*/
if(y<140||y>290)
main();
printf("Enter number of color (1-15) : ");scanf("%d",&colo);
if(colo<1||colo>15)
main();
oldcolo=getpixel(x,y); เก็บค่าสีเก่าเพื่อใช้เปรียบเทียบในการระบายสีใหม่
floodfill8(x,y,colo,oldcolo);
getch();
closegraph();
clrscr();
exit(0);
}
การทำงานของ floodfill8 จะเก็บค่าสีเก่าไว้เพื่อเปรียบเทียบกับสีในตำแหน่งใหม่
ถ้าสีที่ตำแหน่งใหม่เหมือนกับสีที่ตำแหน่งเก่าก็จะลงสีใหม่ที่กำหนดลงไป แต่ถ้าสีที่ตำแหน่งใหม่ไม่เหมือนกับ
สีที่ตำแหน่งเก่าก็จะข้ามไปหาตำแหน่งใหม่จนครบทั้ง 8 ทิศ
floodfill8(int x,int y,int colo,int oldcolo)
{ if(getpixel(x,y)==oldcolo)
{ setcolor(colo);
putpixel(x,y,colo);
delay(500);
floodfill8(x+1,y,colo,oldcolo); /*Fill right*/
floodfill8(x+1,y+1,colo,oldcolo); /*Fill south right*/
floodfill8(x+1,y-1,colo,oldcolo); /*Fill north right*/
floodfill8(x-1,y,colo,oldcolo); /*Fill left*/
floodfill8(x-1,y+1,colo,oldcolo); /*Fill south left*/
floodfill8(x-1,y-1,colo,oldcolo); /*Fill north left*/
floodfill8(x,y+1,colo,oldcolo); /*Fill bottom*/
floodfill8(x,y-1,colo,oldcolo); /*Fill top*/
}
}