Fix : Floodfill infinite recursion due to same color (#4359)
Fix : Floodfill infinite recursion due to same color
This commit is contained in:
parent
81f38174a6
commit
a88abb7ac2
@ -39,6 +39,7 @@ public class FloodFill {
|
||||
* @param oldColor The old color which is to be replaced in the image
|
||||
*/
|
||||
public static void floodFill(int[][] image, int x, int y, int newColor, int oldColor) {
|
||||
if (newColor == oldColor) return;
|
||||
if (x < 0 || x >= image.length) return;
|
||||
if (y < 0 || y >= image[x].length) return;
|
||||
if (getPixel(image, x, y) != oldColor) return;
|
||||
|
@ -93,4 +93,14 @@ class FloodFillTest {
|
||||
FloodFill.floodFill(image, 0, 1, 4, 1);
|
||||
assertArrayEquals(expected, image);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForSameNewAndOldColor() {
|
||||
int[][] image = {{1, 1, 2}, {1, 0, 0}, {1, 1, 1}};
|
||||
|
||||
int[][] expected = {{1, 1, 2}, {1, 0, 0}, {1, 1, 1}};
|
||||
|
||||
FloodFill.floodFill(image, 0, 1, 1, 1);
|
||||
assertArrayEquals(expected, image);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user