CG Expt 5
CG Expt 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.
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:
Start
return
return
screen[x, y] := newColor
End
2. Boundary Fill Algorithm
getpixel(x, y) != fill_color)
putpixel(x, y, fill_color);
Program Code:
// 5 .A) Boundary fill algorithm
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
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);
void main()
int gm,gd=DETECT,radius;
int x,y;
initgraph(&gd,&gm,"C:\\TC\\BGI");
scanf("%d%d",&x,&y);
scanf("%d",&radius);
circle(x,y,radius);
boundaryfill(x,y,4,15);
//delay(500);
getch();
closegraph();
}
OUTPUT:
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
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);
void main()
int gm,gd=DETECT,radius;
int x,y;
initgraph(&gd,&gm,"C:\\TC\\BGI");
scanf("%d%d",&x,&y);
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.