Floodfill 实施 [英] Floodfill implementetion

查看:24
本文介绍了Floodfill 实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的洪水填充功能有问题:

I have a problem with my flood fill function:

void floodfill(int x, int y,Pixel old, Pixel new){

Pixel current = getPixel(x,y);

if(current.r == old.r && current.g == old.g && current.b == old.b){

    setPixel(x,y,new);

    floodfill(x+1,y,old,new);
    floodfill(x-1,y,old,new);
    floodfill(x,y+1,old,new);
    floodfill(x,y-1,old,new);
    floodfill(x+1,y+1,old,new);
    floodfill(x-1,y-1,old,new);
    floodfill(x+1,y+1,old,new);
    floodfill(x-1,y+1,old,new);
}
}

在 struct 'Pixel' 中,我有像素的 rgb 值.我正在尝试填充一个正方形,当我到达正方形的边界时(颜色从白色变为黑色,边界位于 x=200 点),该函数不会更改为其他像素,而是无休止地将 x 值更改为198 和 199(不管 y 值).有人能告诉我我做错了什么吗?

In struct 'Pixel' I have rgb values of the pixel. I am trying to fill a square and when I come to the border of the square (color changes from white to black, the border is at point x=200) the function is not changing to other pixels but just endlessly changing the x value to 198 and 199 (nevermind the y value). Can someone tell me what I am doing wrong?

推荐答案

这听起来像是 setPixel 没有将 (x,y) 的颜色设置为新".因此,发生的是以下递归调用序列:

This sounds like setPixel isn't setting the colour of (x,y) to the "new". So, what's happening is the following sequence of recursive calls:

(199,y)->(200,y) [这会停止,因为边框的颜色与旧的不同]->(198, y)->(199,y) [(199, y) 被调用,因为 setPixel 可能没有改变 (199, y)]->...

(199,y)->(200,y) [This stops because the border doesn't have the same colour as old] ->(198, y)->(199,y) [(199, y) is getting called because setPixel likely didn't change the colour of (199, y)]->...

这篇关于Floodfill 实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆