Advertisement
Shailrshah

Boundary and Flood Fill

Oct 28th, 2014
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <graphics.h>
  4.  
  5. void startGraphics(){
  6.     int gd=DETECT, gm;
  7.     initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
  8.     cleardevice();
  9. }
  10.  
  11. void fFill(int x, int y, int o, int n){
  12.     if(getpixel(x, y)==o){ //fill until pixel's color matches with old color
  13.         putpixel(x, y, n);
  14.  
  15.         fFill(x+1, y, o, n);
  16.         fFill(x-1, y, o, n);
  17.         fFill(x, y+1, o, n);
  18.         fFill(x, y-1, o, n);
  19.     }
  20. }
  21.  
  22. void bFill(int x, int y, int bg, int fg){
  23.     if(getpixel(x, y)!=bg && getpixel(x, y)!=fg){ //fill until boundary's not reached
  24.         putpixel(x, y, fg);
  25.  
  26.         bFill(x+1, y, bg, fg);
  27.         bFill(x-1, y, bg, fg);
  28.         bFill(x, y+1, bg, fg);
  29.         bFill(x, y-1, bg, fg);
  30.     }
  31. }
  32.  
  33. void main(){
  34.     startGraphics();
  35.     setcolor(GREEN); //boundary's color will be green
  36.     rectangle(50, 50, 75, 75); //make sure area of rectangle is small; Otherwise stack overflow ho jaayega
  37.     fFill(60, 60, 0, 4); //0=black, 4=red
  38.     getch();
  39.     bFill(60, 60, 2, 9); //2=green, 9=blue
  40.     getch();
  41.     closegraph();
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement