Skip to content

Commit 6be3c99

Browse files
Add Boundary Fill Algorithm (TheAlgorithms#2822)
Co-authored-by: Andrii Siriak <[email protected]>
1 parent dee4fc7 commit 6be3c99

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
/**
4+
* Java program for Boundary fill algorithm.
5+
* @author Akshay Dubey (https://github.com/itsAkshayDubey)
6+
*/
7+
public class BoundaryFill {
8+
9+
/**
10+
* Get the color at the given co-odrinates of a 2D image
11+
*
12+
* @param image The image to be filled
13+
* @param x_co_ordinate The x co-ordinate of which color is to be obtained
14+
* @param y_co_ordinate The y co-ordinate of which color is to be obtained
15+
*/
16+
public static int getPixel(int[][] image, int x_co_ordinate, int y_co_ordinate) {
17+
18+
return image[x_co_ordinate][y_co_ordinate];
19+
20+
}
21+
22+
/**
23+
* Put the color at the given co-odrinates of a 2D image
24+
*
25+
* @param image The image to be filed
26+
* @param x_co_ordinate The x co-ordinate at which color is to be filled
27+
* @param y_co_ordinate The y co-ordinate at which color is to be filled
28+
*/
29+
public static void putPixel(int[][] image, int x_co_ordinate, int y_co_ordinate, int new_color) {
30+
31+
image[x_co_ordinate][y_co_ordinate] = new_color;
32+
33+
}
34+
35+
/**
36+
* Fill the 2D image with new color
37+
*
38+
* @param image The image to be filed
39+
* @param x_co_ordinate The x co-ordinate at which color is to be filled
40+
* @param y_co_ordinate The y co-ordinate at which color is to be filled
41+
* @param new_color The new color which to be filled in the image
42+
* @param boundary_color The old color which is to be replaced in the image
43+
*/
44+
public static void boundaryFill(int[][] image, int x_co_ordinate, int y_co_ordinate, int new_color, int boundary_color) {
45+
if(x_co_ordinate >= 0 && y_co_ordinate >= 0 && getPixel(image, x_co_ordinate, y_co_ordinate) != new_color && getPixel(image, x_co_ordinate, y_co_ordinate) != boundary_color) {
46+
47+
putPixel(image, x_co_ordinate, y_co_ordinate, new_color);
48+
boundaryFill(image, x_co_ordinate + 1, y_co_ordinate, new_color, boundary_color);
49+
boundaryFill(image, x_co_ordinate - 1, y_co_ordinate, new_color, boundary_color);
50+
boundaryFill(image, x_co_ordinate, y_co_ordinate + 1, new_color, boundary_color);
51+
boundaryFill(image, x_co_ordinate, y_co_ordinate - 1, new_color, boundary_color);
52+
boundaryFill(image, x_co_ordinate + 1, y_co_ordinate - 1, new_color, boundary_color);
53+
boundaryFill(image, x_co_ordinate - 1, y_co_ordinate + 1, new_color, boundary_color);
54+
boundaryFill(image, x_co_ordinate + 1, y_co_ordinate + 1, new_color, boundary_color);
55+
boundaryFill(image, x_co_ordinate - 1, y_co_ordinate - 1, new_color, boundary_color);
56+
57+
58+
}
59+
60+
}
61+
62+
/**
63+
* This method will print the 2D image matrix
64+
*
65+
* @param image The image to be printed on the console
66+
*/
67+
public static void printImageArray(int[][] image) {
68+
69+
for(int i=0 ; i<image.length ; i++) {
70+
for(int j=0 ; j<image[0].length ; j++) {
71+
72+
System.out.print(image[i][j]+" ");
73+
}
74+
75+
System.out.println();
76+
}
77+
78+
}
79+
80+
// Driver Program
81+
public static void main(String[] args) {
82+
83+
//Input 2D image matrix
84+
int[][] image = {
85+
{0,0,0,0,0,0,0},
86+
{0,3,3,3,3,0,0},
87+
{0,3,0,0,3,0,0},
88+
{0,3,0,0,3,3,3},
89+
{0,3,3,3,0,0,3},
90+
{0,0,0,3,0,0,3},
91+
{0,0,0,3,3,3,3}
92+
};
93+
94+
95+
boundaryFill(image,2,2,5,3);
96+
97+
/* Output ==>
98+
* 0 0 0 0 0 0 0
99+
0 3 3 3 3 0 0
100+
0 3 5 5 3 0 0
101+
0 3 5 5 3 3 3
102+
0 3 3 3 5 5 3
103+
0 0 0 3 5 5 3
104+
0 0 0 3 3 3 3
105+
* */
106+
107+
//print 2D image matrix
108+
printImageArray(image);
109+
}
110+
111+
}

0 commit comments

Comments
 (0)