Lab 5- WAP to implement 4 connected and 8 connected boundary-fill algorithms
Theory:
• 4-connected flood fill: In this variant, a pixel is considered connected to its top, bottom,
left, and right neighbors. So, it can fill adjacent pixels in all four cardinal directions.
• 8-connected flood fill: In this variant, a pixel is considered connected to its top, bottom,
left, right, and diagonal neighbors. So, it can fill adjacent pixels in all eight directions.
Algorithm:
4-connected flood fill:
1. Initialize a queue with the seed point.
2. While the queue is not empty:
• Dequeue a pixel from the queue.
• Fill the pixel with the desired color.
• Check and enqueue its four adjacent pixels if they are of the same color.
8-connected flood fill:
1. Initialize a queue with the seed point.
2. While the queue is not empty:
• Dequeue a pixel from the queue.
• Fill the pixel with the desired color.
• Check and enqueue its eight adjacent pixels if they are of the same color.
Source Code
#include <stdio.h>
#define MAX 10
void floodFill4(int screen[][MAX], int x, int y, int prevColor, int newColor) {
if (x < 0 || x >= MAX || y < 0 || y >= MAX)
return;
if (screen[x][y] != prevColor)
return;
screen[x][y] = newColor;
floodFill4(screen, x + 1, y, prevColor, newColor);
floodFill4(screen, x - 1, y, prevColor, newColor);
floodFill4(screen, x, y + 1, prevColor, newColor);
floodFill4(screen, x, y - 1, prevColor, newColor);
void floodFill8(int screen[][MAX], int x, int y, int prevColor, int newColor) {
if (x < 0 || x >= MAX || y < 0 || y >= MAX)
return;
if (screen[x][y] != prevColor)
return;
screen[x][y] = newColor;
floodFill8(screen, x + 1, y, prevColor, newColor);
floodFill8(screen, x - 1, y, prevColor, newColor);
floodFill8(screen, x, y + 1, prevColor, newColor);
floodFill8(screen, x, y - 1, prevColor, newColor);
floodFill8(screen, x + 1, y + 1, prevColor, newColor);
floodFill8(screen, x + 1, y - 1, prevColor, newColor);
floodFill8(screen, x - 1, y + 1, prevColor, newColor);
floodFill8(screen, x - 1, y - 1, prevColor, newColor);
int main() {
int screen[MAX][MAX] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 0, 1, 1, 1, 1, 0, 1, 1},
{1, 1, 0, 1, 1, 1, 1, 0, 1, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
int x = 3, y = 3, newColor = 2;
int prevColor = screen[x][y];
floodFill4(screen, x, y, prevColor, newColor);
printf("4-connected flood fill result:\n");
for (int i = 0; i < MAX; ++i) {
for (int j = 0; j < MAX; ++j) {
printf("%d ", screen[i][j]);
printf("\n");
// Reset the screen for 8-connected flood fill
for (int i = 0; i < MAX; ++i) {
for (int j = 0; j < MAX; ++j) {
screen[i][j] = (screen[i][j] == newColor) ? prevColor : screen[i][j];
floodFill8(screen, x, y, prevColor, newColor);
printf("\n8-connected flood fill result:\n");
for (int i = 0; i < MAX; ++i) {
for (int j = 0; j < MAX; ++j) {
printf("%d ", screen[i][j]);
printf("\n");
return 0;
Output