#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
// this function initializes graphics mode
// it will work only if you're using Borland C++ compiler & BGI drivers
// if you're using another compiler you should overwrite body of this function
void init_gr(void)
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
}
// this function shuts graphics mode down
// it will work only if you're using Borland C++ compiler & BGI drivers
// if you're using another compiler you should overwrite body of this function
void end_gr(void)
{
closegraph();
}
// this function puts pixel on the screen in (x,y) position using color 'color'
// it will work only if you're using Borland C++ compiler & BGI drivers
// if you're using another compiler you should overwrite body of this function
void PutPixel(int x, int y, int color)
{
putpixel(x,y,color);
}
// this function gets color of pixel in (x,y) position
// it will work only if you're using Borland C++ compiler & BGI drivers
// if you're using another compiler you should overwrite body of this function
int GetPixel(int x, int y)
{
return getpixel(x,y);
}
// this function gets maximum horizontal coordinate
// it will work only if you're using Borland C++ compiler & BGI drivers
// if you're using another compiler you should overwrite body of this function
int GetMaxX(void)
{
return getmaxx();
}
// this function gets maximum vertical coordinate
// it will work only if you're using Borland C++ compiler & BGI drivers
// if you're using another compiler you should overwrite body of this function
int GetMaxY(void)
{
return getmaxy();
}
void FloodFill(int x, int y,int fill_color, int border_color)
{
int x_left,x_right,YY,i;
int XMAX,YMAX;
XMAX=GetMaxX();
YMAX=GetMaxY();
// Light as many pixels as possible on line y
// and determine x_left and x_right
x_left=x_right=x;
while (GetPixel(x_left,y)!=border_color && x_left>0)
{
PutPixel(x_left,y,fill_color);
x_left--;
}
x_right++;
while (GetPixel(x_right,y)!=border_color && x_right<XMAX)
{
PutPixel(x_right,y,fill_color);
x_right++;
}
// Recursive calls of FloodFill for two remote points
x=(x_right+x_left)>>1; //shifting means division by 2
for(i=-1;i<=1;i+=2)
{
YY=y;
while (GetPixel(x,YY)!=border_color && YY<YMAX && YY>0) YY+=i;
YY=(y+YY)>>1;
if (GetPixel(x,YY)!=border_color && GetPixel(x,YY)!=fill_color) FloodFill(x,YY,fill_color,border_color);
}
// Recursive calls for all "dark" (not filled) pixels next
// to the line y (with x values between x_left and x_right
for(YY=y-1;YY<=y+1;YY+=2)
{
x=x_left+1;
while (x<x_right && YY>0 && YY<YMAX)
{
if (GetPixel(x,YY)!=border_color && GetPixel(x,YY)!=fill_color) FloodFill(x,YY,fill_color,border_color);
x++;
}
}
}
int main(void)
{
// initializing graphics mode
init_gr();
// drawing area to fill
moveto(520,90); lineto(520,190); lineto(470,240);
lineto(520,290); lineto(520,390);
lineto(320,315); lineto(120,390);
lineto(120,290); lineto(170,240);
lineto(120,190); lineto(120, 90);
lineto(270,90); lineto(270,120);
lineto(170,120); lineto(170,150);
lineto(470,150); lineto(470,120);
lineto(370,120); lineto(370,90); lineto(520,90);
moveto(370,240); lineto(320,290); lineto(270,240);
lineto(320,190); lineto(370,240);
/* example */
FloodFill(121,91,1,15);
FloodFill(320,240,9,15);
FloodFill(100,20,3,15);
/* clean up */
getch();
end_gr();
return 0;
}