0

嘿伙计们,我目前正在研究我自己的自定义帧缓冲区并将原语绘制到其中。哪个工作正常。

然而,我的问题是将基元填充为屏幕上的纯色。比如本例中的一个圆圈。

问题是我试图填补它,我得到一个堆栈溢出错误(这意味着递归函数永远不会结束)。

这是我用来填充该区域的递归函数...

private void filler(int position, byte r, byte g, byte b)
{

    buffer[position] = r;
    buffer[position + 1] = g;
    buffer[position + 2] = b;
    int northPosition = position - rowLength;
    int southPosition = position + rowLength;
    int westPosition = position - 3;
    int eastPosition = position + 3;
    //fills squares west of the current
    /**/
    System.out.println(position);
    //fills squares to the east of the current 
    if(buffer[eastPosition] != r && buffer[eastPosition + 1] != g && buffer[eastPosition + 2] != b)
    {
        System.out.println("runs");
        filler(eastPosition, r, g, b);
    }
    System.out.println(position);
    //fills squares north of the current
    if(buffer[northPosition] != r && buffer[northPosition + 1] != g && buffer[northPosition + 2] != b)
    {
        filler(northPosition,r,g,b);
    }
    //fills squares south of current
    if(buffer[southPosition] != r && buffer[southPosition + 1] != g && buffer[southPosition + 2] != b)
    {
        filler(southPosition,r,g,b);
    }
    //fills squares  west of current
    if(buffer[westPosition] != r && buffer[westPosition + 1] != g && buffer[westPosition + 2] != b)
    {   
        filler(westPosition, r, g, b);
    }
}

请注意,代码的北部和南部部分工作得非常好,并且仅当代码的东部和西部部分一起工作时才会发生错误(但如果我从函数中删除一个或另一个,它们自己工作得很好)。如果有人看到问题,您能否向我解释为什么东西方可能会相互影响?

非常感谢!

4

3 回答 3

1

你的方法永远不会返回。您填充的每个点通常旁边至少有 1 个像素,因此该方法暂时不会返回,而是沿着调用堆栈向下填充该像素。这会导致调用堆栈非常深,并且 stackoverflow 会限制要填充的区域的大小。因此,如果您想使用 java 并填充大面积区域,您应该考虑另一种算法。

于 2013-08-08T14:32:11.907 回答
0

我认为您正在尝试实现 Floodfill 算法(请参阅德语维基百科以获得很好的伪代码实现)。由于您显然使用的是一维数组,因此您必须确保您不会跨行(在东/西情况下)或数组的边界(对于所有情况)。

如果下一步将与当前像素在同一行并且如果它位于数组的边界内,则您只想在东/西方向前进。所以我会像这样改变你的代码(我没有测试它):

private void filler(int position, byte r, byte g, byte b)
{

    buffer[position] = r;
    buffer[position + 1] = g;
    buffer[position + 2] = b;
    int northPosition = position - rowLength;
    int southPosition = position + rowLength;
    int westPosition = position - 3;
    int eastPosition = position + 3;
    //fills squares west of the current
    /**/
    System.out.println(position);
    //fills squares to the east of the current 
    if(eastPosition < buffer.length && eastPosition/rowLength == position/rowLength && buffer[eastPosition] != r && buffer[eastPosition + 1] != g && buffer[eastPosition + 2] != b)
    {
        System.out.println("runs");
        filler(eastPosition, r, g, b);
    }
    System.out.println(position);
    //fills squares north of the current
    if(northPosition >= 0 && buffer[northPosition] != r && buffer[northPosition + 1] != g && buffer[northPosition + 2] != b)
    {
        filler(northPosition,r,g,b);
    }
    //fills squares south of current
    if(southPosition < buffer.length && buffer[southPosition] != r && buffer[southPosition + 1] != g && buffer[southPosition + 2] != b)
    {
        filler(southPosition,r,g,b);
    }
    //fills squares  west of current
    if(westPosition >= 0 && westPosition/rowLength == position/rowLength && buffer[westPosition] != r && buffer[westPosition + 1] != g && buffer[westPosition + 2] != b)
    {   
        filler(westPosition, r, g, b);
    }
}
于 2013-08-08T12:56:55.197 回答
0

还要添加边界条件:是否有南/北/东/西位置?

否则向西会跳向北,依此类推,递归很容易。同样,一旦没有递归,应该会发生索引越界。

于 2013-08-08T14:45:39.960 回答