COMPUTER GRAPHICS - Implement Area Filling Algorithm: Boundary Fill, Flood Fill.
Experiment No.03
Aim: Implement Area Filling Algorithm: Boundary Fill, Flood Fill.
Prerequisite:C Language
Outcome: After successful completion of this experiment students will be able to Implement various output and filled area primitive algorithms
Theory:
- Boundary Fill Algorithm
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.
- 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.
Procedure:
- Boundary Fill Algorithm
Algorithm
Step 1 − Initialize the value of seed point (seedx, seedy), fcolor and dcol.
Step 2 − Define the boundary values of the polygon.
Step 3 − Check if the current seed point is of default color, which is boundary color, then repeat the steps 4 and 5 till the boundary pixels reached.
If getpixel(x, y) = dcol then repeat step 4 and 5
Step 4 − Change the default color with the fill color at the seed point.
setPixel(seedx, seedy, fcol)
Step 5 − Recursively follow the procedure with four neighborhood points.
FloodFill (seedx – 1, seedy, fcol, dcol)
FloodFill (seedx + 1, seedy, fcol, dcol)
FloodFill (seedx, seedy - 1, fcol, dcol)
FloodFill (seedx – 1, seedy + 1, fcol, dcol)
Step 6 − Exit
Procedure for filling a 4- connected region:
Color is specified by parameter fill color (f-color) and boundary color is specified by boundary color (b-color). getpixel() function gives the color of specified pixel and putpixel() fills the pixel with particular color.
boundary_ fill (x,y, f_color, b_color)
{
If ( getpixel (x,y) != b_color && getpixel (x,y) != f_color)
putpixel(x,y,f_color)
boundary_fill( x+1, y, f_color, b_color);
boundary_fill( x, y+1, f_color, b_color);
boundary_fill( x-1, y, f_color, b_color);
boundary_fill( x, y-1, f_color, b_color);
}
}
- Flood Fill Algorithm
Step 1 − Initialize the value of seed point (seedx, seedy), fcolor and dcol.
Step 2 − Define the boundary values of the polygon.
Step 3 − Check if the current seed point is of default color, which is interior color, then repeat the steps 4 and 5 till the boundary pixels reached
If getpixel(x,y) = dcol then repeat step 4 and 5
Step 4 − Change the default color with the fill color at the seed point.
setPixel(seedx, seedy, fcol)
Step 5 − Recursively follow the procedure with four neighbourhood points
FloodFill (seedx – 1, seedy, fcol, dcol)
FloodFill (seedx + 1, seedy, fcol, dcol)
FloodFill (seedx, seedy - 1, fcol, dcol)
FloodFill (seedx, seedy + 1, fcol, dcol)
FloodFill (seedx – 1, seedy + 1, fcol, dcol)
FloodFill (seedx + 1, seedy + 1, fcol, dcol)
FloodFill (seedx + 1, seedy - 1, fcol, dcol)
FloodFill (seedx – 1, seedy - 1, fcol, dcol)
Step 6 − Exit
Flood Fill Function using 4 Connected Method:
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);
}
}
Procedure for filling a 8- connected region:
flood_ fill (x,y, old_color, new_color)
{
putpixel(x,y,new_color)
flood_ fill (x+1, y, old_color, new_color)
flood_ fill (x-1, y, old_color, new_color)
flood_ fill (x, y+1, old_color, new_color)
flood_ fill (x, y-1, old_color, new_color)
flood_ fill (x+1, y+1, old_color, new_color)
flood_ fill (x-1, y-1, old_color, new_color)
flood_ fill (x+1, y-1, old_color, new_color)
flood_ fill (x-1, y+1, old_color, new_color)
}}
Code :
CODE FOR BOUNDARY FILL ALGORITHM
#include<stdio.h>
#include<conio.h>
#include<graphics.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-1,y,f_color,b_color);
boundaryfill(x,y+1,f_color,b_color);
boundaryfill(x,y-1,f_color,b_color);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
rectangle(250,200,310,260);
boundaryfill(280,250,4,15);
getch();
closegraph();
}
CODE FOR FLOOD FILL ALGORITHM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void flood(int x,int y,int f_color,int oldcolor)
{
if(getpixel(x,y)==oldcolor)
{
putpixel(x,y,f_color);
flood(x+1,y,f_color,oldcolor);
flood(x-1,y,f_color,oldcolor);
flood(x,y+1,f_color,oldcolor);
flood(x,y-1,f_color,oldcolor);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
rectangle(100,100,160,160);
flood(105,105,4,0);
getch();
closegraph();
}
Output :
OUTPUT OF FLOOD FILL ALGORITHM
OUTPUT OF FLOOD FILL ALGORITHM
Conclusion:
In this experiment we learnt to draw a colour filled square with required dimensions using Boundary fill algorithm and also with Flood fill algorithm. We can also draw other shapes using these algorithms.
Question of Curiosity
Q.1] Compare Boundary fill and flood fill algorithm for a polygon?
Ans :
- Initialize the value of seed point (seedx, seedy), fcolor and dcol.
- Define the boundary values of the polygon.
- Check if the current seed point is of default colour, then repeat the steps 4 and 5 till the boundary pixels reached.If getpixel(x,y) = dcol then repeat step 4 and 5
- Change the default colour with the fill colour at the seed point.
- Recursively follow the procedure with four neighbourhood points.
- Exit
- Initialize the value of seed point (seedx, seedy), fcolor and dcol.
- Define the boundary values of the polygon.
- Check if the current seed point is of default color then repeat the steps 4 and 5 till the boundary pixels reached
- Change the default color with the fill color at the seed point. setPixel( seedx, seedy, fcol)
- Recursively follow the procedure with four neighbourhood points
- Exit
Comments
Post a Comment