Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Terna Engineering College: LAB Manual Part A

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Terna Engineering College

Computer Engineering Department


Program: Sem III

Course: Computer Graphics

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:

Boundary Fill Algorithm


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.

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

Flood Fill Algorithm


(x,y) are the interior points, background is the background 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 <> background 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
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of
the practical. The soft copy must be uploaded on the Blackboard or emailed to the
concerned lab in charge faculties at the end of the practical in case the there is no
Black board access available)

Roll No. C28 Name: Prathmesh Krishna Gaikwad


Class : SE Batch : B-1
Date of Experiment: 24/09/2021 Date of Submission: 24/09/2021
Grade :
B.1 Document created by the student:
(Write the answers to the questions given in section 5.1 during the 2 hours of practical in the lab here)
B.3 Observations and learning:
1.Boundary Fill Algorithm:

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-

Flood Fill Algorithm:

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.

B.5 Question of Curiosity


Q.1] Compare Boundary fill and flood fill algorithm for a polygon?
Ans-
1.Boundary Fill Algorithm:
i) It can only process the image containing single boundary colour.
ii) Boundary-fill algorithm is faster than the Flood-fill algorithm.
iii) In Boundary-fill algorithm Interior points are painted by continuously searching
for the boundary colour.
iv) Memory consumption is relatively low in Boundary-fill algorithm.

2.Flood Fill Algorithm:


i) It can process the image containing more than one boundary colours.
ii) Flood-fill algorithm is comparatively slower than the Boundary-fill algorithm.
iii) In Flood-fill algorithm a random colour can be used to paint the interior portion
then the old one is replaced with a new one.
iv) It requires huge amount of memory.

Q.2] Write Advantages and Disadvantages of Both algorithms?


Ans-
1.Advantages of Flood fill Algorithm:
i) Flood fill algorithm is simplest algorithm.

Disadvantages of Flood fill Algorithm:


i)Flood fill algorithm is slow.
ii)For large polygon flood fill algorithm id fail because it requires a large frame.

2. Advantages of Boundary fill Algorithm:


i) This method is good for a polygon with the multicolour area and single-
coloured boundary.

Disadvantages of Boundary fill Algorithm:


i)Boundary-fill algorithms can leak. There can be no leakage in flood-fill
algorithms.
ii)if the polygon is having boundaries with different colors then boundary fill
algorithm fails.
Q.3] Explain 4-connected and 8-connected Method ?
Ans-
• 4 connected polygon:
In this technique 4-connected pixels, 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.
• 8 connected polygon :
In this technique 8-connected pixels, we are putting pixels above, below,
right and left side of the current pixels as we were doing in 4-connected
technique.
In addition to this, we are also putting pixels in diagonals so that entire area
of the current pixel is covered. This process will continue until we find a
boundary with different color.

************************

You might also like