这是洪水填充计划8路吗?

时间:2013-03-20 19:47:28

标签: java algorithm flood-fill

嘿这个工作正常,但在将它与4路比较之后我找不到任何差异...... 如果我把它交给它会被认为是实现8路泛洪算法的正确方法吗? 是/否答案就足够了,但我想在继续之前我会问专家

private void flood8(int col, int row, Color startColour) {

    if (startColour.equals(getPixel(col, row))) {
        setPixel(col, row);

        // this bit makes the animation work by
        // drawing intermediate results, and slowing the updates down
        synchronized (this) {
            draw();
       }

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
        }

        // now we call the routine recursively for each neighbour
        // the "guard" surrounding each call ensures that we do
        // not try to keep going past the edge of the raster
        if (col + 1 < COLS) {
            flood4(col + 1, row, startColour);
            System.out.println("Position1 " + col + ", " + row );
        }
        if (col - 1 >= 0) {
            flood4(col - 1, row, startColour);
            System.out.println("Position2 " + col + ", " + row );
        }
        if (row + 1 < ROWS) {
            flood4(col, row + 1, startColour);
            System.out.println("Position3 " + col + ", " + row );
        }
        if (row - 1 <= 0) {
            flood4(col, row - 1, startColour);
            System.out.println("Position4 " + col + ", " + row );
        }
        if (col + 1 < COLS && row + 1 < ROWS) {
            flood4(col + 1, row, startColour);
            System.out.println("Position1 " + col + ", " + row );
        }
        if (col + 1 < COLS && row - 1 >= 0) {
            flood4(col - 1, row, startColour);
            System.out.println("Position2 " + col + ", " + row );
        }
        if (row - 1 >= 0 && row + 1 < ROWS) {
            flood4(col, row + 1, startColour);
            System.out.println("Position3 " + col + ", " + row );
        }
        if (row - 1 >= 0 && row - 1 >= 0) {
            flood4(col, row - 1, startColour);
            System.out.println("Position4 " + col + ", " + row );            
        }
    }
}

感谢您阅读

1 个答案:

答案 0 :(得分:0)

将几条评论转化为答案,将其从“未答复”的队列中取出。社区维基,请随意添加。

  

它会被认为是实现8路泛洪算法的正确方法吗?

可能不是,由于以下原因:

  • 您致电flood4,而正确的递归应该再次致电flood8
  • 您对对角线邻居进行了绑定检查,但(推测)递归调用只会更改一个坐标。