Pages

Tuesday, November 23, 2010

Filling a bounded region, Turbo C

A program to fill a bounded region to be implemented in Turbo C, as a practical of Computer Graphics. As input, a point inside the bounded region is given. 


Implementation:
Starting from the given point apply dfs to the points at n,s,e and w.  As the base conditions check whether the color at the given pixel is equal to the boundary. If it is, return. Also even if it is equal to the fill color, return. Sadly I had to program in Turbo C, yes our college still uses that compiler. Here 4 is the fill color.


Function:
// x and y coordinates of point inside
// getmaxcolor() is the color of boundary
// fill the pixel with color 4


dfs(int x, int y) { 
  if (getpixel(x,y)==getmaxcolor() || getpixel(x,y)==4)
      return; 
  putpixel(x,y,4); 
  dfs(x-1,y);
  dfs(x+1,y);
  dfs(x,y-1);
  dfs(x,y+1); 
}


File:Wfm floodfill animation stack.gif
Floodfill implementation using dfs (stack)
Image Source: Wikipedia 


The output is pretty interesting when you use a delay function with it.    

2 comments:

  1. THIS IS FOUR WAY... CAN U PLZ TELL ME THE EIGHT WAY THING..

    THANXX

    ReplyDelete
  2. Yes Farhan, I can tell you...

    Add the following lines

    dfs (x-1,y-1);
    dfs (x-1,y+1);
    dfs (x+1,y-1);
    dfs (x+1,y+1);

    ReplyDelete