iPhone 中的洪水填充

发布于 2024-10-15 07:42:36 字数 300 浏览 2 评论 0原文

我对 iPhone 开发非常陌生。我想在 iPhone 中使用 FloodFill 算法创建一个应用程序。我对 FloodFill 一无所知。请向我解释 FloodFill 算法的目的。如果您提供 FloodFill(iPhone)的示例应用程序,意味着我真的非常高兴。因为我从早上开始就在探索 FloodFill,但是我什么也没找到。

我的任务是,我想用颜色逐部分填充图像。这意味着,如果我选择一种颜色并单击图像的特定区域,则意味着它会使用所选颜色着色。

请帮助我做到这一点。

编辑:我无法为 iPhone 编写洪水填充算法实现的代码。

I'm very new to iPhone development. I want to create a application using FloodFill algorithm in iPhone. I'm have no idea about FloodFill. Please Explain me the purpose of FloodFill algorithm.If you give a sample application for FloodFill(iPhone)means I'm really, really happy..Because I'm exploring about FloodFill since morning but, nothing I'm found.

My task is, I want to fill the image with color as part by part. Which means, if I'm choosing one color and click on the particular area of the image means it gets colored with the selected color.

Please help me to do this.

Edit: I am not able to write a code for floodfill algorithm implementation for iPhone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稚气少女 2024-10-22 07:42:36

它是一种递归算法,这意味着它调用自身来完成整个过程的较小部分。

洪水填充将从一个像素开始。然后它将检查四个相邻像素(上、下、左、右)。对于与我们开始的颜色相同的四个像素中的任何一个,它将调用从该像素开始的另一个洪水填充。

想象一下一张小图片在哪里。是一种颜色,X 是另一种颜色。这些数字仅供参考,因此 7,0 是右上角。 (忽略红/黑语法高亮显示!)

 01234567
0........
1.XXXX...
2.X..X...
3.X...X..
4.XXX..X.
5...X..X.
6...XXX..
7........

现在想象一下您在 3,3 处开始洪水填充。它将把 3,3 更改为新颜色。
然后它会检查上、下、左、右。
对于向上 (3,2),那里的颜色是相同的(一个点),因此它将从那里开始另一个洪水填充。
对于down(3,4),颜色不同,所以这个分支会停止。
左、(2,3) 和右(4,3) 也相同(一个点),因此更多的洪水填充分支从那里开始。

假设新颜色是 O,所以我们现在有这样的:

 01234567
0........
1.XXXX...
2.X.OX...
3.XOOOX..
4.XXX..X.
5...X..X.
6...XXX..
7........

“向上”分支从 (3,2) 开始新的洪水填充。
从这里开始,向上是X,所以就停止了。右边是 X,所以停止,向下是 O,所以停止,但左边 (2,2) 是相同的(一个点),所以它从那里开始新的洪水填充。

同样,原始洪水填充的“右”分支从 (4,3) 开始。它可以使用的唯一分支是 down (4,4)。现在我们得到了:

 01234567
0........
1.XXXX...
2.XOOX...
3.XOOOX..
4.XXXO.X.
5...X..X.
6...XXX..
7........

因此洪水填充从 (4,4) 继续,向右和向下分支。然后,这两个分支之一将分支到 (5,5)。到那时,将不再有可能的分支。

这就是我的午餐时间:)

It's a recursive algorithm, meaning that it calls itself to accomplish smaller parts of the whole process.

Flood fill will start at one pixel. Then it will check the four adjacent pixels (up, down, left, right). For any of those four pixels that are the same colour as the one we started with, it will call another flood fill starting from that pixel.

Imagine a small picture where . is one colour, and X is another colour. The numbers are just there for reference, so 7,0 is the top right. (Ignore the red / black syntax hilighting!)

 01234567
0........
1.XXXX...
2.X..X...
3.X...X..
4.XXX..X.
5...X..X.
6...XXX..
7........

Now imagine you start a flood fill at 3,3. It will change 3,3 to the new colour.
It will then check up,down,left,right.
For up (3,2), the colour there is the same (a dot), so it will start another flood fill from there.
For down (3,4), the colour is different, so this branch will stop.
Left, (2,3), and right (4,3) are also the same (a dot), so more branches of flood fill start from there.

Let's say that new colour is O, so we now have this:

 01234567
0........
1.XXXX...
2.X.OX...
3.XOOOX..
4.XXX..X.
5...X..X.
6...XXX..
7........

The "up" branch started a new flood fill from (3,2).
From here, up is X, so that stops. Right is X, so that stops, down is O, so that stops, but left (2,2) is the same (a dot), so it starts a new flood fill from there.

Similarly, the "right" branch from the original flood fill began from (4,3). The only branch it can use is down (4,4). Now we have this:

 01234567
0........
1.XXXX...
2.XOOX...
3.XOOOX..
4.XXXO.X.
5...X..X.
6...XXX..
7........

And so the flood fill continues from (4,4), by branching right and down. One of those two branches will then branch into (5,5). By that time, there will be no further possible branches.

And that is my lunchtime filled :)

焚却相思 2024-10-22 07:42:36

这是一个非常好奇的问题。您的意思是:

  1. “洪水填充”多边形吗?其中多边形意味着从平凡多边形一直到复杂的偶数/奇数规则任意多边形填充物(其中多边形可以由贝塞尔曲线路径定义)?

  2. “选择一个点并洪水填充”类型的算法?有点像你在绘画程序中看到的旧的“油漆罐”洪水填充工具(好吧,至少你曾经看到过,对我来说已经很长时间了)(我在这里想到的是 MacPaint / Deluxe Paint 时代)。这些算法的范围从简单像素的二维平面到到达“边缘”(通常是与洪水填充开始的颜色不同的“颜色”)时“停止”的任何地方,一直到到非常复杂的自动边缘检测算法,您可能会在高端绘画/照片处理程序(即 PhotoShop)中看到。

我假设您不会寻找任何微不足道的案例,因为……好吧,在您尝试解决这个问题之前,您可能还需要解决一些其他问题。

因此,在#1的情况下,您可能应该从 阅读文档。 Mac OS X / iOS 拥有极其复杂的 2D 图形 API,主要基于 PDF / PostScript 渲染模型。 CoreGraphics / Quartz 会自动为您执行“洪水填充”,并且您可以在启动贝塞尔曲线的级别上执行此操作。

对于#2,您可能应该从

在同一上下文中混合 CoreGraphics 和 OpenGL 是一项高级技术,因此您可能必须选择 CoreGraphics 或 OpenGL 作为主要渲染 API。然而,CoreGraphics 也有一些非常先进和时髦的 API 来完成相同类型的事情,但这远远超出了您的基本 CoreGraphics API 使用范围(但是您可以通过浏览各种 iOS 图形文档来找到所有信息,这些文档都是广泛且有据可查)。

This is a very curious question. Do you mean:

  1. A "flood filled" polygon? Where polygon means anything from trivial polygons all the way up to a sophisticated Even / Odd rule arbitrary polygon filler (where polygon may be defined by Bézier curve paths)?

  2. A "pick a point and flood fill" type algorithm? Sort of like the old "paint can" flood fill tool you see (well, at least you used to see, it's been a long time for me) in paint programs (I'm thinking MacPaint / Deluxe Paint era here). These algorithms range anywhere from the trivial 2D plane of pixels that "stop" when it hits an "edge" (usually a different "color" than the one where the flood fill started), all the way up to very sophisticated automagic edge detection algorithms you might see in high end paint / photo manipulation programs (i.e., PhotoShop).

I'm going to assume you're not looking for any of the trivial cases because... well, you've probably got some other nuts you need to crack before you try to crack this one.

So, in the case of #1, you should probably start by reading the documentation. Mac OS X / iOS has an extremely sophisticated 2D graphics API, largely based on the PDF / PostScript rendering model. CoreGraphics / Quartz will do "flood fill" for you automatically, and you can do it at the level of Bézier curves to boot.

In the case of #2, you should probably start by reading the exact same documentation. You're probably going to have to interact with it at this level anyways. However, OpenGL + programable shaders may be a better choice, depending on how sophisticated you want your "automagic" edge detection to be. But it's been my experience that people who are banging on the rendering pipe directly like this usually have a pretty good grasp of the fundamental algorithms used in manipulating pixel data, so I suspect this technique may not be appropriate for you.

Mixing CoreGraphics and OpenGL in the same context is a bit of an advanced technique, so you'll probably have to pick either CoreGraphics or OpenGL as your primary rendering API. However, CoreGraphics also has some pretty advanced and snazzy API's for doing the same type of stuff, but that gets way beyond your basic CoreGraphics API usage (but you'll find all the info by going through the various iOS graphics documentation, which is both extensive and very well documented).

末が日狂欢 2024-10-22 07:42:36

您应该遵循计划编写您自己的实现。

希望这有帮助。

You should follow the program to write your own implementations.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文