我想要没有堆栈和递归的洪水填充
我想知道如何在数组上应用洪水填充,我的数组是二维的,其中包含倍新罗马字体类型字母边界。 边界线包含 1,并且内外全为 0。 我想只在里面填充全 1 而不是 0。 但我需要一个不需要更多内存的逻辑。 所以避免递归和堆栈或队列
I wanted to know how to apply flood fill on array , my array is two dimensional , which contains times new roman font type letter boundry.
The boundry line contains 1's and inside and outside all 0's.
I want to fill all 1's instead 0 in only inside.
But i need a logic which do not required more memory.
So avoid recursion and stack or queue
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我通常不会为其他人做作业,但我喜欢这个挑战:
请注意,这不会检查是否触及数组的边缘。此外,它还假设您的数组元素是
int
,并且您仅在图像中使用值0
和1
。I don't normally do homework for other people, but I liked the challenge:
Note that this doesn't check for hitting the edges of the array. Also, it assumes your array elements are e.g.
int
s, and that you're only using the values0
and1
in your image.你的任务没有多大意义。如果您有字体,您不想用洪水填充来填充它,而是直接将其渲染为填充多边形。如果不能可靠地给出良好的结果,则确定哪些部分在字体内和不在字体内,尤其是衬线字体。
填充多边形的典型原理图算法如下(不需要堆栈或递归),并且在某些条件下它也可以应用于位图(我会谈到这一点):
对于每行(或列,无论适合什么 )您的数据结构更好),在您所遵循的虚拟线和所有多边形线(边界)的每个交叉点处切换填充。
假设这个(可能是 O 字符的中线):
结果:
这也适用于位图,但仅如果您实际上始终有两个开始和停止边界。
Your task doesn't make much sense. If you have a typeface, you don't want to fill it with a flood fill, but rather render it directly as filled polygon instead. Determining which parts are in and out of the typeface, especially for a serif font, if not going to give good results reliably.
The typical schematic algorithm for a filled polygon goes like this (no stack or recursion required), and it can be applied to a bitmap as well under certain conditions (I'll come to that):
For each line (or column, whatever suits your data structure better), toggle the fill at each intersection of the virtual line you're following and all polygon lines (boundaries).
Assume this (could be the middle line of an O character):
Result:
This works for bitmaps as well, but only if you actually always have two boundaries for start and stop.
问题是您必须能够判断像素已被填充(用未使用的颜色填充它)。而且,这会非常慢。
The catch is that you must be able to tell that a pixel has been filled (fill it with an unused color). Also, this would be extremely slow.
你基本上不能。您必须将这些信息存储在某个地方,因为您必须知道在完成当前部分后还可以从哪里开始填写。递归可以让你隐式地完成它。保留自己的堆栈可以让您明确地执行此操作,并可能节省一些费用。 Oli Charlesworth 通过保留与图片大小相同的数组做了一件可爱的事情,但这比递归或保留位置堆栈使用更多的内存。
You basically can't. You have to store this information somewhere, because you have to know where else to start filling after you're done with your current section. Recursion lets you do it implicitly. Keeping your own stack lets you do it explicitly, with possibly some saving. Oli Charlesworth does a cute thing by keeping an array of the same size as the picture, but that uses even more memory than recursion or keeping a stack of positions.