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

Computer Graphics (Co-313) LAB: Submitted By: Mohit Ranjan 2K17/CO/188

The document describes 4 experiments related to computer graphics algorithms: 1. Implementing the DDA line drawing algorithm in C++. Key findings are that DDA is faster than direct line equations but uses floating point calculations. 2. Implementing the symmetric DDA algorithm which is more efficient than simple DDA by using smaller increments. 3. Implementing Bresenham's line drawing algorithm which uses only integer calculations, making it faster and more precise than DDA. 4. Drawing a circle using the mid-point circle algorithm, which calculates points in quadrants to draw the full circle efficiently.

Uploaded by

Meenakshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Computer Graphics (Co-313) LAB: Submitted By: Mohit Ranjan 2K17/CO/188

The document describes 4 experiments related to computer graphics algorithms: 1. Implementing the DDA line drawing algorithm in C++. Key findings are that DDA is faster than direct line equations but uses floating point calculations. 2. Implementing the symmetric DDA algorithm which is more efficient than simple DDA by using smaller increments. 3. Implementing Bresenham's line drawing algorithm which uses only integer calculations, making it faster and more precise than DDA. 4. Drawing a circle using the mid-point circle algorithm, which calculates points in quadrants to draw the full circle efficiently.

Uploaded by

Meenakshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

COMPUTER GRAPHICS (CO-313)

LAB
(D1 – G3)

Submitted by:
Mohit Ranjan
2K17/CO/188
INDEX
S. No. Experiment Date Signature
Experiment-1
Aim:
Program to implement simple DDA algorithm.
Description:
In a 2-D plane if we connect two points (x0, y0) and (x1, y1), we get a
line segment. But in computer graphics we can’t directly join any two
coordinate points, for that we calculate intermediate point’s coordinate
and put a pixel for each intermediate point, of the desired color with
help of functions. For generating any line segment we need
intermediate points and for calculating them we have can use a basic
algorithm called DDA (Digital differential analyzer) line generating
algorithm.
Algorithm:
Digital Differential Analyzer (DDA) algorithm is the simple line
generation algorithm which is explained step by step here.

Step 1 − Get the input of two end points (X0, Y0) and (X1, Y1).
Step 2 − Calculate the difference between two end points.

dx = X1 - X0
dy = Y1 - Y0

Step 3 − Based on the calculated difference in step-2, you need to identify


the number of steps to put pixel. If dx>dy, then you need more steps in x
coordinate; otherwise in y coordinate.

if (absolute(dx) > absolute(dy))

Steps = absolute(dx);

else
Steps = absolute(dy);

Step 4 − Calculate the increment in x coordinate and y coordinate.

Xincrement = dx / (float) steps;


Yincrement = dy / (float) steps;

Step 5 − Put the pixel by successfully incrementing x and y coordinates


accordingly and complete the drawing of the line.

for ( int v = 0 ; v < Steps ; v++)

x = x + Xincrement ;

y = y + Yincrement ;

putpixel (Round(x), Round(y)) ;

Code:
#include<bits/stdc++.h>
#include<graphics.h>
#include<stdlib.h>
using namespace std;
void wait_for_char() {
int in = 0;
while (in == 0) {
in = getchar();
}
}
void DDA(int X0, int Y0, int X1, int Y1)
{
int dx = X1 - X0;
int dy = Y1 - Y0;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
float Xinc = dx / (float) steps;
float Yinc = dy / (float) steps;
float X = X0;
float Y = Y0;
for (int i = 0; i <= steps; i++)
{
putpixel (X,Y,RED);
X += Xinc;
Y += Yinc;

}
}

int main() {
int gd = DETECT, gm;
initgraph (&gd, &gm, NULL);
DDA(0,0, 500, 400);

wait_for_char();
closegraph();
return 0;
}
Output:

Discussion:
Advantages:
1. It is the simplest algorithm and it does not require special skills for
implementation.
2. It is a faster method for calculating pixel positions than the direct use of equation
y = mx + b. It eliminates the multiplication in the equation by making use of raster
characteristics.
Disadvantages:
1. Floating point arithmetic in DDA algorithm is still time-consuming.
2. The algorithm is orientation dependent. Hence end point accuracy is poor.

Findings and Learnings:


1. The DDA Algorithm is much faster than the direct line equation as there's
no floating point calculations involved.
2. It's the simplest algorithm and does not require and special skills for
implementation.
3. It's orientation dependent, due to which end point accuracy is poor.
4. Floating point additions are still very expensive operations.
5. The cumulative errors due to limited floating point precision may cause the
calculated points to drift away from the actual line.
Experiment-2
Aim:
Program to implement symmetric DDA algorithm.
Description:
The symmetric DDA is again a line generating algorithm. It is more or
less simple DDA with extra steps. Symmetric DDA is more efficient and
uses less calculation as compared to Simple DDA. In the case of
symmetric DDA the increments in each direction are chosen at small
convenient values to satisfy the relationship
ex = m.ey. Where e is the increment factor along that direction. Also,
the small convenient value should be such that it does not exceed half
to one pixel dimension.
Algorithm:
Digital Differential Analyzer (DDA) algorithm is the simple line
generation algorithm which is explained step by step here.

Step 1 − Get the input of two end points (X0, Y0) and (X1, Y1).
Step 2 − Calculate the difference between two end points.

dx = X1 - X0
dy = Y1 - Y0

Step 3 – Calculate the steps according to the following formula:

steps = pow(2, ceil(log2(abs(dx) > abs(dy)? abs(dx):abs(dy))))

Step 4 − Calculate the increment in x coordinate and y coordinate.

Xincrement = dx / (float) steps;


Yincrement = dy / (float) steps;
Step 5 − Put the pixel by successfully incrementing x and y coordinates
accordingly and complete the drawing of the line.

for ( int v = 0 ; v < Steps ; v++)

x = x + Xincrement ;

y = y + Yincrement ;

putpixel (Round(x), Round(y)) ;

Code:
#include<stdio.h>
#include<graphics.h>

void DDA(int X0, int Y0, int X1, int Y1)


{

int dx = X1 - X0;
int dy = Y1 - Y0;

int steps = pow(2,ceil(log2(abs(dx) > abs(dy)? abs(dx):abs(dy))));


float Xinc = dx / (float) steps;
float Yinc = dy / (float) steps;
float X = X0;
float Y = Y0;
for (int i = 0; i <= steps; i++)
{
putpixel (X,Y,RED);
X += Xinc;
Y += Yinc;
}
}

void wait_for_char() {
int in = 0;
while (in == 0) {
in = getchar();
}
}

int main()
{
int gd = DETECT, gm;
initgraph (&gd, &gm, NULL);
int X0 = 3, Y0 = 8, X1 =150, Y1 = 170;
DDA(X0,Y0,X1,Y1);
delay(3000);
wait_for_char();
return 0;
}
Output:

Discussion:
Advantages
1. It is more faster than Simple DDA as it reduces calculations and
rounding off is done more efficiently.
2. In symmetric DDA e (i.e. the increment factor) is chosen such
that though both the co-ordinates of the resultant points has to be
rounded off, it can be done so very efficiently, thus quickly.
Specifically e is chosen as 1/2^n where 2^(n-1) <= max(|Δx|,|Δy|) <
2^n. In other words the length of the line is taken to be 2^n aligned.

3. End Point accuracy increases as compared to simple DDA.

Disadvantages

1. It shares the same disadvantage as that of Simple DDA that it


contains floating point arithmetic so it naturally takes longer time.

Findings and Learnings:


1. Symmetric DDA is a faster technique to find out the relative pixel
positions for a give line segment.
2. It does lesser calculations as compared to the simple DDA so it is
more efficient.
3. It is orientation dependent, due to which end point accuracy is poor.
4. Floating point additions are still very expensive operations.
5. The cumulative errors due to limited floating point precision may
cause the calculated points to drift away from the actual line.
Experiment-3
Aim:
Program to implement Bresenham algorithm.

Description:
An accurate and efficient raster line-generating algorithm, developed by
Bresenham, which uses only incremental integer calculations. In addition, it can
also be adapted to display circles and other curves. The basic principle is to find
the optimum raster location to represent the straight line. To accomplish this, the
algorithm always increments x/y by one unit based on the flow of the line. Then,
the increment in other variable is found on the basis of the distance between the
actual line location & the nearest pixel.

Algorithm:
1. Input the two line endpoints and store the left endpoint in (x0, y0).
2. Set the color for frame-buffer position (x0, y0); i.e., plot the first point.
3. calculate δx, δy, 2δy and 2δy-δx
4. obtain P0 as follows:
5. P0 = 2δy - δx
6. δx = |X2-X1|
7. δy = |Y2-Y1|
8. At each xk along the line, starting at k = 0, perform the following test:
9. if(Pk < 0):
10. next_point = (Xk+1,Yk)
11. Pk+1 = Pk + 2δy
12. else:
13. next_point = (Xk+1,Yk+1)
14. Pk+1 = Pk + 2δy -2δx
15. Repeat lines 9-14 for δx-1 times.
Code:
#include<bits/stdc++.h>
#include<graphics.h>
#include<stdlib.h>
using namespace std;
void wait_for_char() {
int in = 0;
while (in == 0) {
in = getchar();
}
}
void bresenham(int x0, int y0, int x1, int y1) {
int dx = abs(x1-x0), dy = abs(y1 - y0);
int twoDy = 2*dy;
int twoDyDx = 2*(dy - dx);
int p = 2*dy - dx;
int x, y, xEnd;
if(x0 > x1) {
x = x1;
y = y1;
xEnd = x0;
}
else {
x = x0;
y = y0;
xEnd = x1;
}
putpixel(x, y, YELLOW);
while(x < xEnd) {
x++;
if(p > 0) {
y++;
p += twoDyDx;
}
else {
p += twoDy;
}
cout<<"("<<x<<","<<y<<")\n";
putpixel(x , y , YELLOW);
}
}
int main() {
int gd = DETECT, gm;
initgraph (&gd, &gm, NULL);
bresenham(0, 0, 200, 100);
wait_for_char();
closegraph();
return 0;
}

Output:

Findings and Learnings:


1. The Bresenham Algorithm only uses integers and operations on them,
therefore it is much faster and precise.
2. As there is no rounding function there's not scope for the line to move
away from the actual line.
3. Compared to DDA it's a faster increment algorithm.
Experiment-4

Aim:
To draw a circle using the Mid-Point Algorithm

Description:
It runs the same screen pass as in the Bresenham’s Algorithm and in each screen
pass 8 points are drawn simultaneously. This is possible by the dual-axis symmetry
of the circle. Only the first half of the first quadrant is calculated, rest of the
points are their symmetric reflections along different axis.

Algorithm:
1. Input radius r and circle center (xc , yc )
2. Set the coordinates for first point as:
3. (X0,Y0) = (0,r)
4. Calculate the initial value of the decision parameter as:
5. P0 = 5/4 - r
6. At each xk position, starting at k = 0, perform the following test:
7. if(Pk < 0):
8. next_point = (Xk+1,Yk)
9. Pk+1 = Pk + 2Xk+1 + 1
10. else:
11. next_point = (Xk+1, Yk-1)
12. Pk+1 = Pk + 2Xk+1 + 1 - 2Yk+1
13. //2xk+1 = 2xk + 2 and 2yk+1 = 2yk − 2.
14. Determine symmetry points in the other seven octants.
15. Move each calculated pixel position (x, y) onto the circular path centered at (xc , yc ) and plot the coordinate
values as follows:
16. x = x + Xc
17. y = y + Yc
18. repeat Lines 6-17 until x>=y
Code:
#include<bits/stdc++.h>
#include<graphics.h>
#include<stdlib.h>
using namespace std;

void wait_for_char() {
int in = 0;
while (in == 0) {
in = getchar();
}
}

void midPointCircleDraw(int x_centre, int y_centre, int r)


{
int x = r, y = 0;

// Printing the initial point on the axes


// after translation
cout << "(" << x + x_centre << ", " << y + y_centre << ") ";
putpixel(x+x_centre, y+y_centre, RED);

// When radius is zero only a single


// point will be printed
if (r > 0)
{
cout << "(" << x + x_centre << ", " << -y + y_centre << ") ";
putpixel(x+x_centre, -y+y_centre, RED);

cout << "(" << y + x_centre << ", " << x + y_centre << ") ";
putpixel(y+x_centre, x+y_centre, RED);

cout << "(" << -y + x_centre << ", " << x + y_centre << ")\n";
putpixel(-y+x_centre, x+y_centre, RED);

// Initialising the value of P


int P = 1 - r;
while (x > y)
{
y++;

// Mid-point is inside or on the perimeter


if (P <= 0)
P = P + 2*y + 1;
// Mid-point is outside the perimeter
else
{
x--;
P = P + 2*y - 2*x + 1;
}

// All the perimeter points have already been printed


if (x < y)
break;

// Printing the generated point and its reflection


// in the other octants after translation
cout << "(" << x + x_centre << ", " << y + y_centre << ") ";
putpixel(x+x_centre, y+y_centre, RED);

cout << "(" << -x + x_centre << ", " << y + y_centre << ") ";
putpixel(-x+x_centre, y+y_centre, RED);

cout << "(" << x + x_centre << ", " << -y + y_centre << ") ";
putpixel(x+x_centre, -y+y_centre, RED);

cout << "(" << -x + x_centre << ", " << -y + y_centre << ")\n";
putpixel(-x+x_centre, -y+y_centre, RED);
// If the generated point is on the line x = y then
// the perimeter points have already been printed
if (x != y)
{
cout << "(" << y + x_centre << ", " << x + y_centre << ") ";
putpixel(y+x_centre, x+y_centre, RED);

cout << "(" << -y + x_centre << ", " << x + y_centre << ") ";
putpixel(-y+x_centre, x+y_centre, RED);

cout << "(" << y + x_centre << ", " << -x + y_centre << ") ";
putpixel(y+x_centre, -x+y_centre, RED);

cout << "(" << -y + x_centre << ", " << -x + y_centre << ")\n";
putpixel(-y+x_centre, -x+y_centre, RED);

}
}
}

// Driver code
int main()
{
int gd = DETECT, gm;
initgraph (&gd, &gm, NULL);
midPointCircleDraw(getmaxx()/2, getmaxy()/2, 100);
wait_for_char();
closegraph();
return 0;
}

Output:

Findings and Learnings:


1. The midpoint method is used for deriving efficient scan-conversion
algorithms to draw geometric curves on raster displays.
2. The method is general and is used to transform the nonparametric
equation f(x,y) = 0, which describes the curve, into an algorithms that draws
the curve.
3. Time consumption is high.
4. The distance between the pixels is not equal so we won’t get smooth circle.
Experiment-5

Aim:
To draw an ellipse using the Mid-Point Algorithm

Description:
● Ellipses in Computer Graphics:

o Just like lines, ellipses are another primitive shape used in computer
graphics.

o An ellipse is defined as the set of points such that the sum of the
distances from two fixed positions (foci) is the same for all points. If
the distances to the two foci from any point on the ellipse are labeled
d 1 and d 2 , then the general equation of an ellipse can be stated as :

o Expressing distances d 1 and d 2 in terms of the focal coordinates and


, we have:

o Ellipse equations are greatly simplified if the major and minor axes
are oriented to align with the coordinate axes. An ellipse in "standard
position" has major and minor axes oriented parallel to the x and y
axes respectively.

o Parameter rx labels the semi-major axis, and parameter ry labels the


semi-minor axis. The equation of the ellipse can be written in terms
of the ellipse center coordinates and parameters rx and ry as :
o Alternatively, one could use the polar coordinates, but yet again they
come at a higher computational cost, thus more efficient algorithms
have been designed to draw ellipses.

Algorithm:
1. Input radii rx and ry and ellipse center (xc, yc)
2. Set the coordinates for first point as:
3. (X0,Y0) = (0,r)
4. Calculate the initial value of the decision parameter as:
5. P10 = ry^2 + rx^2/4 - rx^2ry
6. At each xk position in octant 1, starting at k = 0, perform the following test:
7. if (P1k < 0):
8. next_point = (Xk+1,Yk)
9. P1 (k+1) = P1k + 2ry^2x(k+1) + ry^2
10. Else:
11. next_point = (Xk+1, Yk-1)
12. P1(k+1) = P1k + 2ry^2x(k+1) + ry^2 - 2rx^2y(k+1)
14. Determine symmetry points in the other three octants.
15. Move each calculated pixel position (x, y) onto the circular path centered at (xc , yc ) and
plot the coordinate values as follows:
16. x = x + Xc
17. y = y + Yc
18. Repeat Lines 7-17 until 2ry^2x >= 2rx^2y.
19. Calculate the initial value of the decision parameter as:
20. p20 = ry^2(x0 + 1/2)^2 + rx^2(y0 - 1)^2 - rx^2ry^2
21. At each xk position in octant 2, starting at k = 0, perform the following test:
22. if(P2k > 0):
23. next_point = (Xk,Yk-1)
24. P2(k+1) = P2k - 2rx^2y(k+1) + rx^2
25. else:
26. next_point = (Xk+1,Yk-1)
27. P2(k+1) = P2k - 2rx^2y(k+1) + rx^2 - 2rx^2y(k+1)
28. Determine symmetry points in the other three octants.
29. Move each calculated pixel position (x, y) onto the circular path centered at (xc , yc ) and
plot the coordinate values as follows:
30. x = x + Xc
31. y = y + Yc
32. Repeat lines 22 - 31 till y > 0
Code:
#include <bits/stdc++.h>
#include <graphics.h>
using namespace std;

void wait_for_char() {
int in = 0;
while (in == 0) {
in = getchar();
}
}

void midptellipse(int rx, int ry, int xc, int yc)


{
float dx, dy, d1, d2, x, y;
x = 0;
y = ry;

// Initial decision parameter of region 1


d1 = (ry * ry) - (rx * rx * ry) +
(0.25 * rx * rx);
dx = 2 * ry * ry * x;
dy = 2 * rx * rx * y;

// For region 1
while (dx < dy)
{

// Print points based on 4-way symmetry


cout << x + xc << " , " << y + yc << endl;
putpixel(x+xc, y+yc, RED);

cout << -x + xc << " , " << y + yc << endl;


putpixel(-x+xc, y+yc, RED);

cout << x + xc << " , " << -y + yc << endl;


putpixel(x+xc, -y+yc, RED);

cout << -x + xc << " , " << -y + yc << endl;


putpixel(-x + xc, -y + yc, RED);
// Checking and updating value of
// decision parameter based on algorithm
if (d1 < 0)
{
x++;
dx = dx + (2 * ry * ry);
d1 = d1 + dx + (ry * ry);
}
else
{
x++;
y--;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d1 = d1 + dx - dy + (ry * ry);
}
}

// Decision parameter of region 2


d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) +
((rx * rx) * ((y - 1) * (y - 1))) -
(rx * rx * ry * ry);

// Plotting points of region 2


while (y >= 0)
{

// Print points based on 4-way symmetry


cout << x + xc << " , " << y + yc << endl;
putpixel(x + xc, y + yc, RED);

cout << -x + xc << " , " << y + yc << endl;


putpixel(-x + xc, y + yc, RED);

cout << x + xc << " , " << -y + yc << endl;


putpixel(x + xc, -y + yc, RED);

cout << -x + xc << " , " << -y + yc << endl;


putpixel(-x + xc, -y + yc, RED);

// Checking and updating parameter


// value based on algorithm
if (d2 > 0)
{
y--;
dy = dy - (2 * rx * rx);
d2 = d2 + (rx * rx) - dy;
}
else
{
y--;
x++;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d2 = d2 + dx - dy + (rx * rx);
}
}
}

// Driver code
int main()
{
int gd = DETECT, gm;
initgraph (&gd, &gm, NULL);
midptellipse(200, 150, getmaxx()/2, getmaxy()/2);
wait_for_char();
closegraph();
return 0;
}

Output:
Findings and Learnings:
1. The midpoint method is used for deriving efficient scan-conversion
algorithms to draw geometric curves on raster displays.
2. The method is general and is used to transform the nonparametric
equation f(x,y) = 0, which describes the curve, into an algorithms that draws
the curve.
3. Time consumption is high.
4. The distance between the pixels is not equal so we won’t get smooth circle.
Experiment-6
Aim:
To write a program and implement Flood Fill Algorithm

Description:
Sometimes we come across an object where we want to fill the area and its
boundary with different colors. We can paint such objects with a specified interior
color instead of searching for particular boundary color as in boundary filling
algorithm.

Instead of relying on the boundary of the object, it relies on the fill color. In other
words, it replaces the interior color of the object with the fill color. When no more
pixels of the original interior color exist, the algorithm is completed.

In Flood Fill algorithm we start with some seed and examine the neighboring
pixels, however pixels are checked for a specified interior color instead of
boundary color and is replaced by a new color. It can be done using 4 connected
or 8 connected region method.

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, then repeat the steps 4 and 5 till the
boundary pixels reached.

Ifgetpixel(x, y)=dcol then repeat step 4and5

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

Code:
#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);
flood(x-1, y - 1, new_col, old_col);
flood(x-1, y + 1, new_col, old_col);
flood(x+1, y - 1, new_col, old_col);
flood(x+1, y + 1, new_col, old_col);
}
}

int main()
{
int gd, gm = DETECT;

initgraph(&gd, &gm, NULL);


int top, left, bottom, right;
top = left = 50;
bottom = right = 300;
rectangle(50, 50, 100, 100);
int x = 51;
int y = 51;
int newcolor = 12;
int oldcolor = 0;
flood(x, y, newcolor, oldcolor);
getch();
return 0;
}
OUTPUT:

FINDING AND LEARNING:


We can modify findFill() method to reduce storage requirements of the stack by
filling horizontal pixel spans ,i.e., we stack only beginning positions for those pixel
spans having oldcolour .In this modified version, starting at the first position of
each span, the pixel values are replaced until a value other than oldcolour is
encountered. We can also show an area bordered by several different color
regions too.
Experiment-7

Aim:
To implement Boundary Fill Algorithm

Description:
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.

Algorithm:
1. Create a function named as boundaryfill with 4 parameters
(x,y,f_color,b_color).

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);
}
}
2. Call it recursively until the boundary pixels are reached.
3. Stop.

Code:
#include <graphics.h>

void boundaryFill8(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);
boundaryFill8(x + 1, y, fill_color, boundary_color);
boundaryFill8(x, y + 1, fill_color, boundary_color);
boundaryFill8(x - 1, y, fill_color, boundary_color);
boundaryFill8(x, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y + 1, fill_color, boundary_color);
boundaryFill8(x + 1, y - 1, fill_color, boundary_color);
boundaryFill8(x + 1, y + 1, fill_color, boundary_color);
}
}

//driver code
int main()
{

int gd = DETECT, gm;


initgraph(&gd, &gm, NULL);
rectangle(50, 50, 150, 300);
boundaryFill8(55, 55, 4, 15);
getch();
closegraph();
return 0;
}
Output:

Findings and Learnings:

1. Boundary Fill is used for the coloring figures in computer graphics. Here,
area gets colored with pixels of a chosen color as boundary this giving the
technique its name.
2. Boundary fill fills the chosen area with a color until the given colored
boundary is found.
3. This algorithm is also recursive in nature as the function returns when the
pixel to be colored is the boundary color or is already the fill color.

You might also like