0% found this document useful (0 votes)
40 views

CG Expt 5

The document describes two area filling algorithms: boundary fill and flood fill. Boundary fill begins from a point inside a region and paints inward pixel-by-pixel until reaching the boundary color. Flood fill determines the area connected to a given point by replacing its color. Both algorithms can use 4-connected or 8-connected approaches. Pseudocode and C code implementations are provided for each algorithm. The conclusion states that both boundary fill and flood fill algorithms were implemented.

Uploaded by

mayanksanjay007
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)
40 views

CG Expt 5

The document describes two area filling algorithms: boundary fill and flood fill. Boundary fill begins from a point inside a region and paints inward pixel-by-pixel until reaching the boundary color. Flood fill determines the area connected to a given point by replacing its color. Both algorithms can use 4-connected or 8-connected approaches. Pseudocode and C code implementations are provided for each algorithm. The conclusion states that both boundary fill and flood fill algorithms were implemented.

Uploaded by

mayanksanjay007
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/ 7

EXPERIMENT NO 5

Aim:
Implement Area Filling Algorithm: Boundary Fill, Flood Fill.

Theory:
Boundary Fill Algorithm:
It follows an approach where the region filling begins from some extent residing inside the
region and paints the inside towards the boundary. In case the boundary contains a single color the
fill algorithm continues within the outward direction pixel by pixel until the boundary color is
encountered. The boundary-fill algorithm is often mainly implemented within the interactive
painting packages, where the inside points are easily chosen. The functioning of the boundary-fill
starts by accepting the coordinates of an indoor point (x, y), a boundary color and fill color
becomes the input.
Beginning from the (x, y) the method checks neighboring locations to spot whether or not they are
a part of the boundary color. If they’re not from the boundary color, then they’re painted with the
fill color, and their adjacent pixels are tested against the condition. The process ends when all the
pixels up until the boundary color for the world are checked.

The filling is done using four connected or eight connected approaches.

Four connected approaches is more suitable than the eight connected approaches.
1. Four connected approaches: In this approach, left, right, above, below pixels are tested.

2. Eight connected approaches: In this approach, left, right, above, below and four diagonals are
selected. Boundaries can be checked by seeing pixels from left and right first. Then pixels are
checked by seeing pixels from top to bottom. The algorithm takes time and memory because
some recursive calls are needed.
Flood Fill Algorithm:

Flood fill algorithm is also known as a seed fill algorithm. It determines the area which is
connected to a given node in a multidimensional array. This algorithm works by filling or
recolouring a selected area containing different colours at the inside portion and therefore the
boundary of the image. It is often illustrated by a picture having a neighborhood bordered by
various distinct color regions. To paint such regions we will replace a specific interior color
instead of discovering a boundary color value. This is the rationale the approach is understood
because of the flood-fill algorithm. Now, there are two methods which will be used for creating an
endless boundary by connecting pixels – 4-connected and 8-connected approach. In the
4-connected method, the pixel can have at maximum four neighbors that are positioned at the
proper, left, above and below the present pixel. On the contrary, in the 8-connected method, it can
have eight, and the neighboring positions are checked against the four diagonal pixels. So, any of
the 2 methods are often wont to repaint the inside points.

Algorithm:

1. Flood Fill Algorithm:

Start

if (x, y) not in the screen range, then

return

if color of (x, y) ≠ prevColor, then

return

screen[x, y] := newColor

fillScreen(x+1, y, prevColor, newColor)

fillScreen(x-1, y, prevColor, newColor)

fillScreen(x, y+1, prevColor, newColor)

fillScreen(x, y-1, prevColor, newColor)

End
2. Boundary Fill Algorithm

boundaryFill4(int x, int y, int fill_color,int boundary_color)

if(getpixel(x, y) != boundary_color &&

getpixel(x, y) != fill_color)

putpixel(x, y, fill_color);

boundaryFill4(x + 1, y, fill_color, boundary_color);

boundaryFill4(x, y + 1, fill_color, boundary_color);

boundaryFill4(x - 1, y, fill_color, boundary_color);

boundaryFill4(x, y - 1, fill_color, boundary_color);

Program Code:
// 5 .A) Boundary fill algorithm

#include<stdio.h>

#include<graphics.h>

#include<dos.h>

void boundaryfill(int x,int y,int f_color,int b_color)

if((getpixel(x,y)!=b_color) && (getpixel(x,y)!=f_color))

putpixel(x,y,f_color);
boundaryfill(x+1,y,f_color,b_color);

boundaryfill(x,y+1,f_color,b_color);

boundaryfill(x-1,y,f_color,b_color);

boundaryfill(x,y-1,f_color,b_color);

//getpixel(x,y) gives the color of specified pixel

void main()

int gm,gd=DETECT,radius;

int x,y;

initgraph(&gd,&gm,"C:\\TC\\BGI");

printf("Enter x and y positions for circle\n");

scanf("%d%d",&x,&y);

printf("Enter radius of circle\n");

scanf("%d",&radius);

circle(x,y,radius);

boundaryfill(x,y,4,15);

//delay(500);

getch();

closegraph();
}

OUTPUT:

// 5.B) Flood Fill Algorithm

#include<stdio.h>

#include<graphics.h>

#include<dos.h>

void floodFill(int x,int y,int oldcolor,int newcolor)

if(getpixel(x,y) == oldcolor)

{
putpixel(x,y,newcolor);

floodFill(x+1,y,oldcolor,newcolor);

floodFill(x,y+1,oldcolor,newcolor);

floodFill(x-1,y,oldcolor,newcolor);

floodFill(x,y-1,oldcolor,newcolor);

//getpixel(x,y) gives the color of specified pixel

void main()

int gm,gd=DETECT,radius;

int x,y;

initgraph(&gd,&gm,"C:\\TC\\BGI");

printf("Enter x and y positions for circle\n");

scanf("%d%d",&x,&y);

printf("Enter radius of circle\n");

scanf("%d",&radius);

circle(x,y,radius);

floodFill(x,y,0,15);

delay(5000);

getch();

closegraph();

OUTPUT:
Conclusion:

Thus we have implemented Area Filling Algorithms: Boundary Fill, Flood Fill.

You might also like