My goal was to create an efficient version of flood fill polygon filling algorithm. The code works correctly for filling rectangles but for some reason it jumps out of the circle while filling the South-East Pixels.
The disadvantage of flood fill algorithm is that it does not work for really big shapes by causing a stack overflow because of many extra functions stored in the stack while recursively calling itself. TO overcome this, I have written this algorithm where I first fill all North,East,South and West pixels as they do not require the recursive function stack. Then, i call NorthEast, SouthEast , SouthWest and NorthWest pixels filling functions which make use of recursion stack. I have ensured that at any particular time, my recursion stack wont have more than 2 methods to avoid a stack overflow.
Please help me to fix the leak
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void northFill(int a,int b,int newcolor,int oldcolor){
int current=getpixel(a,b);
if(current==oldcolor){
putpixel(a,b,newcolor);
northFill(a,b+1,newcolor,oldcolor);
}
}
void westFill(int a,int b,int newcolor,int oldcolor){
int current=getpixel(a,b);
if(current==oldcolor){
putpixel(a,b,newcolor);
westFill(a-1,b,newcolor,oldcolor);
}
}
void southFill(int a,int b,int newcolor,int oldcolor){
int current=getpixel(a,b);
if(current==oldcolor){
putpixel(a,b,newcolor);
southFill(a,b-1,newcolor,oldcolor);
}
}
void eastFill(int a,int b,int newcolor,int oldcolor){
int current=getpixel(a,b);
if(current==oldcolor){
putpixel(a,b,newcolor);
eastFill(a+1,b,newcolor,oldcolor);
}
}
void northEastFill(int a,int b,int newcolor,int oldcolor){
int current=getpixel(a,b);
if(current==oldcolor){
putpixel(a,b,newcolor);
northFill(a,b+1,newcolor,oldcolor);
eastFill(a+1,b,newcolor,oldcolor);
northEastFill(a+1,b+1,newcolor,oldcolor);
}
}
void southEastFill(int a,int b,int newcolor,int oldcolor){
int current=getpixel(a,b);
if(current==oldcolor){
putpixel(a,b,newcolor);
southFill(a,b-1,newcolor,oldcolor);
eastFill(a+1,b,newcolor,oldcolor);
southEastFill(a+1,b-1,newcolor,oldcolor);
}
}
void northWestFill(int a,int b,int newcolor,int oldcolor){
int current=getpixel(a,b);
if(current==oldcolor){
putpixel(a,b,newcolor);
northFill(a,b+1,newcolor,oldcolor);
westFill(a-1,b,newcolor,oldcolor);
northWestFill(a-1,b+1,newcolor,oldcolor);
}
}
void southWestFill(int a,int b,int newcolor,int oldcolor){
int current=getpixel(a,b);
if(current==oldcolor){
putpixel(a,b,newcolor);
southFill(a,b-1,newcolor,oldcolor);
westFill(a-1,b,newcolor,oldcolor);
southWestFill(a-1,b-1,newcolor,oldcolor);
}
}
void fillPoint(int x,int y,int color){
int pixelColor=getpixel(x,y);
putpixel(x,y,color);
northFill(x,y+1,color,pixelColor);
eastFill(x+1,y,color,pixelColor);
southFill(x,y-1,color,pixelColor);
westFill(x-1,y,color,pixelColor);
northEastFill(x+1,y+1,color,pixelColor);
southEastFill(x+1,y-1,color,pixelColor);
southWestFill(x-1,y-1,color,pixelColor);
northWestFill(x-1,y+1,color,pixelColor);
}
void main(){
int gd = DETECT , gm;
int x,y;
initgraph(&gd,&gm,"");
circle(320,240,230); //<- Doesnt work for this
//rectangle(40,40,630,470); <-Works for this
printf("Enter x,y: ");
scanf("%d %d",&x,&y);
fillPoint(x,y,GREEN);
getch();
closegraph();
}