还是那份图形学作业。作业的要求使用4-种子填充算法,这个算法非常简单,但是效率很低,需要使用一个非常大的栈来进行保存。
在无聊的时候,自己实现了扫描线种子填充算法。
(因为每种图形界面工具的绘图方式不同,此处用Qt 4.4实现)
void scanlineFill(seed)
{
QColoroldColor =image->pixel(seed); //获取填充点颜色
if(oldColor==currentColor) //要填充的颜色和原始颜色相同,不进行填充
{
return;
}
QStack<QPoint> *stack = newQStack<QPoint>();
stack->push(seed);
while(!stack->isEmpty())
{
QPointcurrentPoint =stack->top(); //获取当前填充点
stack->pop(); //去除栈顶点
image->setPixel(currentPoint,currentColor.rgb()); //填充当前点
QPointleftPoint = currentPoint;
QPointrightPoint = currentPoint;
//对当前点左侧进行填充
while(leftPoint.x() >0) //没有超出图形左边界,继续向左遍历
{
leftPoint.rx()--; //向左移一个单位
if(image->pixel(leftPoint) ==oldColor.rgb()) //如果需要填充,则填充成当前颜色
{
image->setPixel(leftPoint,currentColor.rgb());
}
else //如果不需要填充,则跳出循环
{
leftPoint.rx()++;
break;