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

    faizaan

    Member

    Updated: Oct 27, 2024
    Views: 1.1K
    hi friend's,

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

    #include<stdio.h>
    #include<conio.h>
    #include<graphics.h>
    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 .
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • mech_guy

    MemberAug 14, 2009

    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
    Are you sure? This action cannot be undone.
    Cancel
  • faizaan

    MemberAug 14, 2009

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • dipen30

    MemberAug 14, 2009

    you are right faizaan for

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

    this functions..
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register