当前位置: 首页 > 面试题库 >

Android Flood-fill算法

白淇
2023-03-14
问题内容

有谁知道迭代和高效的洪水填充算法?

还是有没有办法实现floodfill没有堆栈溢出错误的递归算法?

尝试了使用堆栈的@ Flood填充, 但是我找不到在白色和黑色图像上工作的方法。


问题答案:

这个算法对我很好。

private void FloodFill(Bitmap bmp, Point pt, int targetColor, int replacementColor) 
{
    Queue<Point> q = new LinkedList<Point>();
    q.add(pt);
    while (q.size() > 0) {
        Point n = q.poll();
        if (bmp.getPixel(n.x, n.y) != targetColor)
            continue;

        Point w = n, e = new Point(n.x + 1, n.y);
        while ((w.x > 0) && (bmp.getPixel(w.x, w.y) == targetColor)) {
            bmp.setPixel(w.x, w.y, replacementColor);
            if ((w.y > 0) && (bmp.getPixel(w.x, w.y - 1) == targetColor))
                q.add(new Point(w.x, w.y - 1));
            if ((w.y < bmp.getHeight() - 1)
                    && (bmp.getPixel(w.x, w.y + 1) == targetColor))
                q.add(new Point(w.x, w.y + 1));
            w.x--;
        }
        while ((e.x < bmp.getWidth() - 1)
                && (bmp.getPixel(e.x, e.y) == targetColor)) {
            bmp.setPixel(e.x, e.y, replacementColor);

            if ((e.y > 0) && (bmp.getPixel(e.x, e.y - 1) == targetColor))
                q.add(new Point(e.x, e.y - 1));
            if ((e.y < bmp.getHeight() - 1)
                    && (bmp.getPixel(e.x, e.y + 1) == targetColor))
                q.add(new Point(e.x, e.y + 1));
            e.x++;
        }
    }
}


 类似资料:
  • Auto fill in all directions Auto fill in vertical direction only with creating new rows Auto fill in all directions Notice the little square (fill handle) in the corner of the selected cell. You can d

  • 此属性属于Paint类型,它表示要在形状内填充的颜色。 您可以使用方法setFill()设置形状的填充颜色,如下所示 - path.setFill(COLOR.BLUE); 默认情况下,笔触颜色的值为BLACK 。 以下是具有不同颜色的三角形图。

  • ⛽️ ember-fill-up ⚠ No Longer Maintained This project is no longer maintained. This addon was a proof-of-concept for my EmberFest talk previewing a user-friendly API for container/element queries and s

  • 淡出过渡 以下是在JavaFX中演示Fade Transition的程序。 将此代码保存在名为FadeTransitionExample.java的文件中。 import javafx.animation.FadeTransition; import javafx.application.Application; import javafx.scene.Group; import javaf

  • 问题内容: 我想明白之间的差别,只是简单地?功能上有区别吗?据我所知,它们是完全相同的。您是否可以对其中之一进行其他操作? 问题答案: –此属性使您可以为文本指定填充颜色。如果未设置,则将使用该属性进行填充。 是的,它们是相同的,但是如果两者具有不同的值,则它们将优先。 我认为这样做的理由是,您可以在使用时根据需要选择其他颜色,但是如果不可用(因此也不可行),它将优雅地退回到。在某些情况下,您可能

  • 本文向大家介绍c# SqlDataAdapter中的Fill是怎么实现的,包括了c# SqlDataAdapter中的Fill是怎么实现的的使用技巧和注意事项,需要的朋友参考一下 1. 讲故事 最近因为各方面原因换了一份工作,去了一家主营物联柜的公司,有意思的是物联柜上的终端是用 wpf 写的,代码也算是年久失修,感觉技术债还是蛮重的,前几天在调试一个bug的时候,看到了一段类似这样的代码: 是不