Terna Engineering College: LAB Manual Part A
Terna Engineering College: LAB Manual Part A
Terna Engineering College: LAB Manual Part A
Faculty:
LAB Manual
PART A
(PART A : TO BE REFFERED BY STUDENTS)
Experiment No.03
A.1 Aim:
Implement Area Filling Algorithm: Boundary Fill, Flood Fill.
A.2 Prerequisite:
1. C Language
A.3 Outcome:
After successful completion of this experiment students will be able to
Implement various output and filled area primitive algorithms
A.4 Theory:
• The boundary fill algorithm works as its name. This algorithm picks a point inside an object and
starts to fill until it hits the boundary of the object. The color of the boundary and the color that
we fill should be different for this algorithm to work.
• In this algorithm, we assume that color of the boundary is same for the entire object. The
boundary fill algorithm can be implemented by 4-connected pixels or 8-connected pixels.
4-Connected Polygon
• In this technique 4-connected pixels are used as shown in the figure. We are putting the pixels
above, below, to the right, and to the left side of the current pixels and this process will continue
until we find a boundary with different color.
Flood Fill Algorithm
Sometimes we want to fill in (or recolor) an area that is not defined within a single
color boundary.
Figure below shows an area bordered by several different color regions.
We can paint such areas by replacing a specified interior color instead of searching
for a boundary color value.
This approach is called a flood-fill algorithm.
We start from a specified interior point (x, y) and reassign all pixel values that are
currently set to a given interior color with the desired fill color.
If the area we want to paint has more than one interior color, we can first reassign
pixel values so that all interior points have the same color.
Using either a 4-connected or 8-connected approach, we then step through pixel
positions until all interior points have been repainted.
The following procedure flood fills a 4-connected region recursively, starting from
the input position.
Flood Fill Function:
void floodFill4 ( int x, int y, intfillColor , intoldColor)
{
i f (getpixel (x, y) == oldcolor)
{
setcolor ( fillColor ) ;
setpixel (x, y ) :
floodFill4 (x + l, y, fillColor, oldColor):
floodfill4 (x-1, y, fillcolor, oldcolor);
floodPill4 (x, y + l, fillcolor, oldcolor);
floodFill4 (x, y-1, fillColor, oldcolor);
}
}
We can modify procedure f loodFill4 to reduce the storage requirements of the stack
by filling horizontal pixel spans.In this approach, we stack only the beginning
positions for those pixel spans having the value oldColor. Starting at the first
position of each span, the pixel values are replaced until a value other than oldColor
is encountered.
A.5 Procedure:
Boundary Fill Algorithm
(x,y) are the interior points, boundary is the boundary color and fill color is the color
to be filled. Following is a recursive method for boundary fill.
1. present color = getcolor() // a function which returns the current color of
(x,y)
2. if present_color <> boundary and if present_color <> fill color then repeat
steps 3-7
3. set_pixel (x,y, fill color)
4. call the algorithm recursively for points (x + 1, y)
5. call the algorithm recursively for points (x – 1,y)
6. call the algorithm recursively for points (x,y + 1)
7. call the algorithm recursively for points (x,y - 1)
8. stop
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void boundaryfill(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);
boundaryfill(x+1,y,fill_color,boundary_color);
boundaryfill(x,y+1,fill_color,boundary_color);
boundaryfill(x-1,y,fill_color,boundary_color);
boundaryfill(x,y-1,fill_color,boundary_color);
}
}
int main()
{
int gd=DETECT,gm=0,x,y,r;
initgraph(&gd,&gm,"C:\\TURBOC4\\TC\\BGI");
rectangle(100,100,150,150);
boundaryfill(105,105,4,15);
delay(10000);
closegraph();
return 0;
}
Output-
Program:
#include<dos.h>
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
void flood(int x,int y,int new_col,int old_col)
{
if(getpixel(x,y) == old_col)
{
putpixel(x,y,new_col);
flood(x+1,y,new_col,old_col);
flood(x-1,y,new_col,old_col);
flood(x,y+1,new_col,old_col);
flood(x,y-1,new_col,old_col);
}
}
int main()
{
int gd=DETECT,gm=0,x,y,oldcolor=0,newcolor=12;
initgraph(&gd,&gm,"C:\\TURBOC4\\TC\\BGI");
setcolor(BLUE);
rectangle(50,50,100,100);
flood(51,51,newcolor,oldcolor);
delay(1000);
getch();
closegraph();
return 0;
}
Output-
B.4 Conclusion:
Ans- Boundary fill algorithm and Flood fill algorithm is implemented in C.
************************