我想要没有堆栈和递归的洪水填充

发布于 2024-09-28 20:40:17 字数 118 浏览 7 评论 0原文

我想知道如何在数组上应用洪水填充,我的数组是二维的,其中包含倍新罗马字体类型字母边界。 边界线包含 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 技术交流群。

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

发布评论

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

评论(4

清风夜微凉 2024-10-05 20:40:17

我通常不会为其他人做作业,但我喜欢这个挑战:

int c = -1;
while (c < 0)
{    
    /* Store breadcrumb trail, look to carry on */
    a[x][y] = c--;
    if (!hunt(0))
    {
        /* Nowhere to go, so back-track by looking for breadcrumb */
        a[x][y] = 1;
        c += 2;
        hunt(c);
    }
}

bool_t hunt(int v)
{
    if (a[x-1][y] == v)  { x--; return TRUE; }
    if (a[x+1][y] == v)  { x++; return TRUE; }
    if (a[x][y-1] == v)  { y--; return TRUE; }
    if (a[x][y+1] == v)  { y++; return TRUE; }
    return FALSE;
}

请注意,这不会检查是否触及数组的边缘。此外,它还假设您的数组元素是 int,并且您仅在图像中使用值 01

I don't normally do homework for other people, but I liked the challenge:

int c = -1;
while (c < 0)
{    
    /* Store breadcrumb trail, look to carry on */
    a[x][y] = c--;
    if (!hunt(0))
    {
        /* Nowhere to go, so back-track by looking for breadcrumb */
        a[x][y] = 1;
        c += 2;
        hunt(c);
    }
}

bool_t hunt(int v)
{
    if (a[x-1][y] == v)  { x--; return TRUE; }
    if (a[x+1][y] == v)  { x++; return TRUE; }
    if (a[x][y-1] == v)  { y--; return TRUE; }
    if (a[x][y+1] == v)  { y++; return TRUE; }
    return FALSE;
}

Note that this doesn't check for hitting the edges of the array. Also, it assumes your array elements are e.g. ints, and that you're only using the values 0 and 1 in your image.

撩动你心 2024-10-05 20:40:17

你的任务没有多大意义。如果您有字体,您不想用洪水填充来填充它,而是直接将其渲染为填充多边形。如果不能可靠地给出良好的结果,则确定哪些部分在字体内和不在字体内,尤其是衬线字体。

填充多边形的典型原理图算法如下(不需要堆栈或递归),并且在某些条件下它也可以应用于位图(我会谈到这一点):

对于每行(或列,无论适合什么 )您的数据结构更好),在您所遵循的虚拟线和所有多边形线(边界)的每个交叉点处切换填充。

假设这个(可能是 O 字符的中线):

00010010001001000
   ^  ^   ^  ^
   |  |   |  stop
   |  |   start
   |  stop
   start

结果:

00011110001111000

这也适用于位图,但如果您实际上始终有两个开始和停止边界。

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):

00010010001001000
   ^  ^   ^  ^
   |  |   |  stop
   |  |   start
   |  stop
   start

Result:

00011110001111000

This works for bitmaps as well, but only if you actually always have two boundaries for start and stop.

就是爱搞怪 2024-10-05 20:40:17
function LowMemFloodFill(pixel)
  FillPixel(pixel)
  Do
    didFill = false
    For each pixel
      If current pixel has been filled
        For each adjacent pixel
          If adjacent has not been filled
            FillPixel(adjacent)
            didFill = true
          End
        End
      End
    End
  While didFill
End

问题是您必须能够判断像素已被填充(用未使用的颜色填充它)。而且,这会非常慢。

function LowMemFloodFill(pixel)
  FillPixel(pixel)
  Do
    didFill = false
    For each pixel
      If current pixel has been filled
        For each adjacent pixel
          If adjacent has not been filled
            FillPixel(adjacent)
            didFill = true
          End
        End
      End
    End
  While didFill
End

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.

时间海 2024-10-05 20:40:17

你基本上不能。您必须将这些信息存储在某个地方,因为您必须知道在完成当前部分后还可以从哪里开始填写。递归可以让你隐式地完成它。保留自己的堆栈可以让您明确地执行此操作,并可能节省一些费用。 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.

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