C: Using floodfill to fill a circle

user3382203 picture user3382203 · Jan 11, 2015 · Viewed 27.1k times · Source

Whenever I execute this code below, the whole screen fills up with a grid pattern in red. I just want to fill the circular region in red.

#include<graphics.h>
#include<conio.h>
void main(){
    int gm, gd=DETECT;
    initgraph(&gd,&gm,"c:\\turboC3\\bgi");
    circle(100,100,50);
    setfillstyle(HATCH_FILL,RED);
    floodfill(100,100,RED);
    getch();
    closegraph();
}

Output:

enter image description here

Answer

shauryachats picture shauryachats · Jan 11, 2015

In the line floodfill(100,100,RED), the third parameter has to be the color of the border. As by default, your circle's border color is WHITE, so change your code to:

#include<graphics.h>
#include<conio.h>
void main(){
    int gm, gd=DETECT;
    initgraph(&gd,&gm,"c:\\turboC3\\bgi");
    circle(100,100,50);
    setfillstyle(HATCH_FILL,RED);
    //Change RED to WHITE.
    floodfill(100,100,WHITE);
    getch();
    closegraph();
}

Thanks to you, I learnt a new thing today. :)