0

Hey this works correctly, but after comparing it to the 4-way i can't find any differences... If i were to hand this in would it be considered a correct method of implementing an 8-way flood algorithm? A yes/no answer will suffice, but i figured I'd ask the experts before continuing

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 );            
        }
    }
}

Thanks for reading

Jube
  • 184
  • 2
  • 15

1 Answers1

0

Turning several comments into an answer, to get this out of the “unanswered” queue. Community wiki, so feel free to add.

would it be considered a correct method of implementing an 8-way flood algorithm?

Probably not, due to the following reasons:

  • You call flood4, while a proper recursion should call flood8 again.
  • You do bound checks for diagonal neighbours, but the (presumably) recursive call only changes one coordinate.
MvG
  • 57,380
  • 22
  • 148
  • 276