0

ねえ、これは正しく動作しますが、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 );            
        }
    }
}

読んでくれてありがとう

4

1 に答える 1

0

いくつかのコメントを回答に変えて、これを「未回答」のキューから取り出します。コミュニティウィキなので、気軽に追加してください。

8ウェイフラッドアルゴリズムを実装する正しい方法と見なされますか?

次の理由により、おそらくそうではありません。

  • を呼び出しますがflood4、適切な再帰をflood8再度呼び出す必要があります。
  • 対角近傍の境界チェックを実行しますが、(おそらく)再帰呼び出しは1つの座標のみを変更します。
于 2013-03-20T22:10:55.963 に答える