can any one explain boundry fill c program recursive function call's ?

hi friend's,

i want to know about how the recursive call are made in program given below .(the program is running properly )

#include
#include
#include
void fillcolor(int,int,int,int);
void main()
{
int gdriver=DETECT,gmode;
int xc,yc,r,fc,bc;
clrscr();
initgraph(&gdriver,&gmode,"f:\\tc\\bgi");
printf("enter center & radius : ");
scanf("%d %d %d",&xc,&yc,&r);
printf("enter boundry & fill color : ");
scanf("%d %d",&bc,&fc);
setcolor(bc);
circle(xc,yc,r);
fillcolor(xc,yc,bc,fc);
getch();
}

void fillcolor(int x,int y,int bc,int fc)
{
int cp;
cp=getpixel(x,y);

if(cp!=bc && cp!=fc)
{
delay(20);
putpixel(x,y,fc);
fillcolor(x+1,y,bc,fc);
fillcolor(x-1,y,bc,fc);
fillcolor(x,y+1,bc,fc);
fillcolor(x,y-1,bc,fc);
}
}

my problem is that i think that when the function is called first time
then the recursive call fillcolor(x+1,y,bc,fc) will proceed in x direction and when boundry is reaches than the if(cp!=bc && cp!=fc) condition will be false and the control should return to main .

But it is not happening like this so can any one explain this recursive calls to me .

pls reply soon .

Replies

  • mech_guy
    mech_guy
    Hi faizaan,

    my problem is that i think that when the function is called first time
    then the recursive call fillcolor(x+1,y,bc,fc) will proceed in x direction and when boundry is reaches than the if(cp!=bc && cp!=fc) condition will be false and the control should return to main .
    No the control wont transfer back to main after fillcolor(x+1,y,bc,fc) reaches boundary.

    1. Initially you are passing Centre of circle (xc, yc) to fillcolor from main.
    2. Fillcolour will fill this (xc, yc) corresponding pixel and will call itself sending it (x+1) and y coordinate.
    3. This process of increasing x by 1 goes on till fillcolour catches boundary that is (xc + radius,y).
    4. Once the condition gets FALSE, control gets back to "fillcolor" with coordinares (xc + radius -1,y).
    5. For coordinate (xc + radius -1,y). condition says FALSE again because that pixel is already colored.
    6. Control gets back to fillcolor again with coordinates (xc + radius - 2,y). Which is FALSE again.
    7. This condition goes on till fillcolor reaches back to centre (xc,yc).

    NOTE: Till now fillcolor(x+1,y,bc,fc) line alone was executed. to colour all pixels till right boundary. It was moved back to origin due to second line fillcolor(x-1,y,bc,fc).

    Now Next line of fillcolor gets executed: fillcolor(x-1,y,bc,fc), leading to coloring of left (to centre) pixels and first line fillcolor(x+1,y,bc,fc) brings it back to origin.

    8. Now same thing goes till u reach left boundary of circle. And then control starts shifting back to origin again.
    9. Now 3rd line of fillcolor(x,y+1,bc,fc) gets executed. Come back to origin and finally start colouring (y-1) pixels.
    10. Come back to origin.
    11. Now none of the commands of fillcolour is left to be exceuted.
    12. It is now that command goes back to main.

    Hope its clear now.

    Thanks
  • faizaan
    faizaan
    thank's mech_guy,

    great explaination keep it up.

    But

    "Control gets back to fillcolor again with coordinates (xc + radius - 2,y). Which is FALSE again."

    i think that after reaching boundry it will go to fillcolor(x,y+1,bc,fc) because fillcolor(x-1,y,bc,fc) is again false.

    i tried introducing delay(100) in between function call's and i studied the pattern in which it was filling .

    but when i introduced delay between

    fillcolor(x,y+1,bc,fc);
    delay(100);
    fillcolor(x,y-1,bc,fc);

    the result is amazing it is not as simple as we think.
  • dipen30
    dipen30
    you are right faizaan for

    fillcolor(x,y+1,bc,fc);
    delay(100);
    fillcolor(x,y-1,bc,fc);

    this functions..

You are reading an archived discussion.

Related Posts

Check out - Germany, France out of recession with 0.3% growth- International Business-News-The Economic Times
Lets Continue the journey of GK Questions.. CE-GK Questions Part-I GK Questions by Pallavi Collection of Questions from Part-I 1. World woman's day-8 March 2. World environment day-5 June 3....
Hi Friends.... I got MTEC rank & want to do MTEC in Electronics. Which branch should I prefer among the following -Digital Electronics.VLSI Design &Embedded systems & some others. Which...
UNION uses just one location (max among all the menbers of the UNION) but then say my UNION has int and float initially I read a int value it gets...
hello guyz, I am rahul from faridabad. i am new here and i like this forum. thanks to the admin for creating such a good forum for future engineers. i...