[go: up one dir, main page]

0% found this document useful (0 votes)
196 views4 pages

Lab Report 5, Computer Graphics BCA 5th Sem

This document discusses and provides code for implementing 4-connected and 8-connected boundary-fill algorithms. It explains that 4-connected flood fill fills adjacent pixels in the top, bottom, left and right directions, while 8-connected additionally fills diagonally. Pseudocode and C code are provided to implement both variants using queues to iteratively fill pixels of the seed color with the new color.

Uploaded by

Bikram Sah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views4 pages

Lab Report 5, Computer Graphics BCA 5th Sem

This document discusses and provides code for implementing 4-connected and 8-connected boundary-fill algorithms. It explains that 4-connected flood fill fills adjacent pixels in the top, bottom, left and right directions, while 8-connected additionally fills diagonally. Pseudocode and C code are provided to implement both variants using queues to iteratively fill pixels of the seed color with the new color.

Uploaded by

Bikram Sah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like