种子填充算法最好使用非递归的方式,若使用递归方式,非常消耗栈内存容易溢出。
1.递归算法
void Polygon::fill(int x,int y,COLORREF color)
{
COLORREF currentColor=getColor(x,y);
//如果当前位置颜色不是边框颜色而且不是指定颜色则填充
if(currentColor!=borderColor && currentColor!=color)
{
putPixel/glVertex2i/..... //填充颜色函数
fill(x-1,y);
fill(x+1,y);
fill(x,y-1);
fill(x,y+1);
}
}
2.非递归算法
void Polygon::fill(int x,int y,COLORREF color)
{
std::stack<Point>s;
s.push(Point(x,y));
COLORREF currentColor;
while(!s.empty)
{
Point p=s.top();
s.pop();
currentColor=getColor(x,y);
//如果当前位置颜色不是边框颜色而且不是指定颜色则填充
if(currentColor!=borderColor && currentColor!=color)
{
putPixel/glVertex2i/..... //填充颜色函数
s.push(Point(x-1,y));
s.push(Point(x+1,y));
s.push(Point(x,y-1));
s.push(Point(x,y+1));
}
}
}