You are on page 1of 80

COMPUTER GRAPHICS

LINE& CIRCLE
ALGORITHMS
Basic Concepts in Line
Drawing
In computer line is drawn by calculating intermediate points on the
line path between two end points of line

Output device then fills t he position between two end points

Analog devices like r andom scan display draws straight line between
two end points by applying horizontal and v ertical deflection voltages in
'x' and 'y' direction

D igital devices draws line by plotting d ifferent points between end

points Coordinates of points on the line are calculated using e quation

of line
Basic Concepts in Line
Drawing End point of
line
( x2, y2 )

discrete (distinct)
points of line

( x1, y1)

End point of
line
Basic Concepts of Line Drawing
Agorithms
Cartesian slope - intercept equation for straigh line is

y = m*x + b ................................... ( 1 )

where m = slope of line, b = y intercept of line

if (x1, y1) and (x2, y2) are end points of line then slope of line is calculated
as
y2 -
................
m= y1
x2 - (2)
x1
and b = y1 - m*x1 ................( 3 )

Algorithms for drawing straight lines uses equation no. ( 1 ), ( 2 ), and


(3)
Basic Concepts of Line Drawing
Agorithms
x interval x = x2 - x1 and y interval y = y2 -
y1
where y = m* x
................( 4 )
and x
................( 5 )

=
Equation ( 4 ) and ( 5 ) decides deflection voltages in analog display devices

if | m | < 1 then x can be set proportional to small h orizontal deflection


voltage y
m
and vertical deflection voltage is proportional to y

if | m | > 1 then y can be set proportional to small vertical deflection voltage


and
horizontal deflection voltage is proportional to x
Basic Concepts of Line Drawing
Agorithms
if m = 1 and x = y then horizontal and vertical deflection voltages
are equal and smooth line is drawn between end points withslope m

Scan - conversion process of straight line :

In raster systems lines are plotted with pixels

Step size in horizontal and vertical direction depends on pixel seperation

So, we must sample line at distinct position and find the nearest pixel to line at
the sample position

This is called s can conversion process


Digital Differential Analyzer (DDA) - Line Algorithm
DDA line algorit hm is a scan conversion algorit hm based on calculat ing values of
x and y

If slope of line | m | < 1 or | m | = 1 then we sample line at unit x interval ( x = 1 )


and calculate each next value of y as below :

= yk + m ................
yk+1
(6)

'm' can have value between 0 to 1 and values of y must be rounded to


nearest integer
Digital Differential Analyzer (DDA) - Line Algorithm

If slope of line | m | > 1 then we reverse the role of x and y

We sample line at unit y intervals ( y = 1 ) and calculate at each next x values


as :

= xk + 1/m ................( 7 )
xk+1

Equation ( 6 ) and ( 7 ) are useful if we process line from left endpoint to right
end point
Digital Differential Analyzer (DDA) - Line Algorithm

If we process the line from right endpoint to left endpoint and the value of slope of
line | m | < 1 or | m | = 1 then we sample line at unit x interval ( x = -1 ) and
calculate each next values of y as below :

= yk - m ................
yk+1
(8)

Similarly if | m | > 1 and line is processed from right endpoint to left endpoint then
we take y = -1 and each next x values are calculated as :

= xk - 1/m ................( 9 )
xk+1
Digital Differential Analyzer (DDA) - Line Algorithm

Step 1 : Read t he end point s of line i.e (x1,y1) and (x2,y2)

Step 2 : Calculate x and y as follow -


x

x2 - x1 y

=
St ep 4 : xIncrement = x / steps
yIncrement = y2 y- y1
/
steps
Step 3: If abs( x) > abs( y)
then steps = x
Digital Differential Analyzer (DDA) - Line Algorithm

St ep 5 : x = x1 + 0.5* sign(
x) y = y1 + 0.5* sign(
y)

Step 6 : k = 0
while (k < steps)
{
setPixel ( ROUND(x), ROUND(y)) x = x +
xIncrement
y =y + yIncrement
k =k +1
}

Step 7 : End
Digital Differential Analyzer (DDA) - Line Algorithm

Advant ages of DDA Algorit hm

It is a very simple algorithm

It is a faster method for calculating pixel

positions Disdvantages of DDA Algorithm

Accumulation of roundoff errors in successive adition floating-point


increment causes the calculated pixel position to drift away from actual line
path

Rounding operations and floating point arithmetic in DDA algorithm are


time consuming
Solved Example - DDA Line Algorithm
Consider the line from (0,0) to (4,6) , use simple DDA algorithm to rasterize this
line

Solution : Given points are (x1,y1) = (0,0) and (x2,y2) =


(4,6) therefor, x1=0, y1=0, and x2=4, y2=6
x = x2 - y = y2 - y1
x1 = 6-0
=4-0 = 6
=4
abs( x) = abs( y) =
4, 6
as abs( y) > abs( x) , steps = abs( y) = 6

initial values of x and y caculated as


x = x1 + 0.5*sign( x) = 0 + 0.5*(+1) = 0 + 0.5 =
0.5 y = y1 + 0.5*sign( y) = 0 + 0.5*(+1) = 0 + 0.5 =
0.5
Solved Example - DDA Line Algorithm
Solution xinc = (x2 - yinc = (y2 -
: x1)/steps, y1)/steps
= (4 - 0 )/6 = (6 - 0)/6
= 4/6 = 1/1
= 0.667 =1
starting point is (x,y) = (0.5,0.5)
therefore plot point (x,y) =
(0,0)
st ep calculation of (x,y) point Plot
s x = x + xinc = 0.5 + 0.667 =
1.167 (1,2)
1
y = y + yinc = 0.5 + 1 =
1.5 (x,y) = (1.167,1.5)
(2,3)
2 x = x + xinc = 1.167 + 0.667 =
1.834
y = y + yinc = 1 .5 + 1 = 2.5
Solved Example - DDA Line Algorithm
Solution
:
step calculation of (x,y) point Plot
s
x = x + xinc = 1.834 + 0.667 = (3,4)
3
2.5
y = y + yinc = 2.5 + 1 =
3.5 (x,y) = (2.5,3.5)

x = x + xinc = 2.5 + 0.667 = (3,5)


4
3.167
y = y + yinc = 3.5 + 1 =
4.5 (x,y) = (3.167,4.5)

x = x + xinc = 3.167 + 0.667 = (4,6)


5
3.834
y = y + yinc = 4.5 + 1 =
5.5 (x,y) = (3.834,5.5)
Solved Example - DDA Line Algorithm
Solution
:
step calculation of (x,y) point Plot
s
x = x + xinc = 3.834 + 0.667 = (5,7)
6
4.5
y = y + yinc = 5.5 + 1
=6.5 (x,y) = (4.5,6.5)
C Program of DDA Line
#include<stdio.h> Algorithm if(deltax > deltay)
{ steps = deltax;
#include<conio.h>
}
#include<graphics.h>
#include<math.h> void else
main() { steps = deltay;
{ }
int gd=DETECT,gm; xinc = (x2-x1)/steps;
float x,y,x1,y1,x2,y2,deltax,deltay; yinc = (y2-y1)/steps; x
float xinc,yinc,steps; =x1;
int i = 1; y= y1;
printf("\n Enter the first coordinates x1 and y1 "); while(i < steps)
scanf("%f%f",&x1,&y1); {
printf("\n Enter the second coordinates x2 and y2 "); putpixel(x,y,RED); x
scanf("%f%f",&x2,&y2); = x + xinc;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI"); y = y + yinc; i
deltax = abs(x2-x1); = i + 1;
}
deltay = abs(y2-y1);
getch();
closegraph();
}
Output of DDA Line
Program
Bresenham's Line Algorithm

Bresenham line algorithm is a raster line generating algorithm , developed by


B resenham

It uses only integer calculations that can be used to display display and other
curves
Bresenham's Line Algorithm

Step 1 : Input the two line endpoints and store the left endpoint in ( x 0, y
0)

Step 2 : Load ( x0 , y0 ) into the frame buffer ; that is , plot the first point
St ep 3: Calculat e x, y, 2 y, and 2 y - 2 x and obtain the starting value for
const ant s decision
parameter as

p0 =
Step 4: At each
2 x i along the
y - line starting at k=0, perform the following
test : if p< 0, the next
x point to be plot is (x k + 1, yk) and
k

pk + 1
= pk + 2
y
Bresenham's Line Algorithm

Step 4 : Otherwise, the next point to plot is ( xk + 1 , yk + 1) and

pk + 1
= pk + 2 y-2
x
Step 5 : Repeat step
x t imes
4

Step 5 :
End
Solved Example - Bresenham Line
Algorithm
Use Bresenham line algorithm to rasterize the line from (20,10) and
(30,18)

Solution : The given points are (x1,y1) = (20,10) and (x2,y2) =


(30,18) therefore x1=20, y1=10 and x2=30, y2=18
x = x2 - y = y2 -
x1 y1
= 30 - 20 = 18 - 10
= 10 =8
the starting point is x = x1 and y = y1 , therefore (x,y) = (x1,y1) =
(20,10)

steps = x = 10, therefore repeat 10 times and calculate 10

points the starting value (initial value ) of pk is calculated as


pk = 2* y- x
= 2*8 - 10
=6
Solved Example - Bresenham Line
Solution Algorithm
: st ep value of pk calculation of (x,y) Plot
s point As pk > 0, so (21,11)
6
1 x = x+1 = 20+1 =
21 y = y+1 = 10+1
= 11 (x,y)=(21,11)
pk = pk+2* y-2* x
= 6+16-20
pk = 2

As pk > 0, so (22,12)
2 2
x = x+1 = 21+1 =
22 y = y+1 = 11+1
= 12 (x,y)=(22,12)
pk = pk+2* y-2* x
= 2+16-20
= -2
Solved Example - Bresenham Line
Solution Algorithm
: st ep value of pk calculation of (x,y) Plot
s point As pk < 0, so (23.12)
-2
3 x = x+1 = 22+1 = 23
y = y = 12
(x,y)=(23,12)
pk = pk+2* y
= -2+16
pk =
14
As pk > 0, so (24,13)
4 14
x = x+1 = 23+1 =
24 y = y+1 = 12+1
=13 (x,y)=(24,13)
pk = pk+2* y-2* x
= 14+16-20
= 10
Solved Example - Bresenham Line
Solution Algorithm
: st ep value of pk calculation of (x,y) Plot
s point As pk > 0, so (25,14)
10
5 x = x+1 = 24+1 =
25 y = y+1 = 13+1
=14 (x,y)=(25,14)
pk = pk+2* y-2* x
= 10+16-20
pk = 6

As pk > 0, so (26,15)
6 6
x = x+1 = 25+1 =
26 y = y+1 = 14+1
= 15 (x,y)=(26,15)
pk = pk+2* y-2* x
= 6+16-20
=2
Solved Example - Bresenham Line
Solution Algorithm
: st ep value of pk calculation of (x,y) Plot
s point As pk > 0, so (27,16)
2
7 x = x+1 =
26+1=27 y = y+1
= 15+1=16
(x,y)=(27,16)
pk = pk+2* y-2*
x
= 2+16-20
Aspkpk < 0, so
= -2 (28,16)
8 -2
x = x+1 =
27+1=28
y = y = 16
(x,y)=(28,16)
pk = pk+2* y
= -2+16
Solved Example - Bresenham Line
Solution Algorithm
: st ep value of pk calculation of (x,y) Plot
s point As pk > 0, so (29,17)
14
9 x = x+1 =
28+1=29 y = y+1
= 16+1=17
(x,y)=(29,17)
pk = pk+2* y-2*
x
= 14+16-20
As
pkpk > 0, so
= 10 (30,18)
10 10
x = x+1 =
29+1=30 y = y+1
= 17+1=18
As we have completed 10 steps and
caculated 10 points we will stop here
C Program of Bresenham Line
while(i<deltax)
Algorithm
#include<conio.h> {
#include<graphics.h> if(pk<0)
#include<math.h> void {
main() x = x+1;
{ y = y;
int gd = DETECT,gm; pk =
int x,y,x1,y1,x2,y2,deltax,deltay,pk; int pk+2*de
i=0; ltay;
printf("\n Enter the first coordinates x1 and y1 "); }
scanf("%d%d",&x1,&y1); if(pk>=0)
printf("\n Enter the second coordinates x2 and y2 "); {
scanf("%d%d",&x2,&y2); x=x+1;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
deltax = x2-x1; y=y+1;
deltay = y2-y1; pk =
x=x1; pk+2*
deltay-
y=y1;
2*delta
pk x;
=(2*deltay) -
}
deltax;
Output of Bresenham Line
Program
Symmetry of a
Circle y
In circle generating algorithms ( -y, x ) ( y, x)
computations can be reduced, if
we consider symmetry of circle
( x,
( -x, y)
Shape of circle is similar in e y)
ach 450
quadrant x
We can draw section of second
quadrant from first quadrant as it ( -x,
is -y ) ( x,
symmetrical around y - axis -y )
Similarly circle section of third and
fourth qaudrant can be drawn ( -y, -x) ( y, -x )
from first and second quadrant as
it
symmetrical around x -axis
Symmetry of a
Circle y
There is also symmetry between ( -y, x ) ( y, x)
o ctants in a
circle
( x,
Two octants divided by 450 and in one ( -x, y)
quadrant are symmetric to each y)
450
other
x
Taking advantage of circle symmetry
in this way , we can generate all
( -x,
pixels of circle by calculating only
-y ) ( x,
points within a sector from x = 0 to -y )
x=y
( -y, -x) ( y, -x )
Bresenham's Circle Algorithm

Step 1 : Read the radius ( r ) of a circle Step 5 : Plot pixels in all octants
as : Plot( x, y )
Step 2: Calculate initial decision variable Plot( y, x)
Plot( -y, x )
PiStep 3: Set x = 0 and y = r Plot( -x, y )
Plot( -x, -y )
Step 4: if (Pi < 0 )
Plot(-y, -x )
{
Plot( y, -x )
x=x+1
Plot( x, -y )
Pi = P i + 4*x + 6
}
elseif ( Pi >= 0 ) Step 6 : Stop

{
x=x+
1 y=y
-1
P =
C Program of Bresenham Circle
#include<stdio.h>Algorithm
#include<conio.h> else if(pk>=0)
#include<graphics.h> {
void main() x=x+1;
{
y=y-1;
int gd=DETECT,gm;
int x,y,xc,yc;
pk=pk
int radius; +4*(x-
float pk; y)+10;
printf("\n Enter the coordinates of center of circle xc and yc: "); }
scanf("%d%d",&xc,&yc); putpixel(xc+x,yc+y,BLUE);
printf("\n Enter the radius of circle: ");
putpixel(xc+y,yc+x,BLUE);
scanf("%d",&radius);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI"); putpixel(xc-y,yc+x,BLUE);
x=0; putpixel(xc-x,yc+y,BLUE);
y=radius; putpixel(xc-x,yc-y,BLUE);
pk=3-2*radius;
putpixel(xc-y,yc-x,BLUE);
while(x<=y)
{
putpixel(xc+y,yc-x,BLUE);
if(pk<0) putpixel(xc+x,yc-y,BLUE);
{ }
x=x+1; getch();
y=y;
closegraph();
pk=pk
+4*x+6 }
Output of Bresenham Circle
Program
Midpoint Circle Algorithm

Step 1 : Input radius 'r' and circle center ( xc , yc ), and obtain the first point
on circumference of a circle whose center is at origin as :

( x0 , y0 ) = (0,r)

Step 2: Calculate the initial value of decision parameter as :

p0 = ( 5/4 ) - r

Step 3: At each x k position, starting at k = 0, perform the following test :

k
if p < 0, the next point along the circle centered on (0,0) is (x k
otherwise, ,y k ) next point along the circle is (x
the k + 1 , y k - 1 ) and
+1

pk + 1 = pk + 2*x k +1 +1
Midpoint Circle Algorithm

otherwise if p k < 0, the next points along the circle is (xk +1, yk - 1)
and
= pk + 2*x k + 1 + 1 - 2*y k + 1
pk + 1

where 2*xk + 1 = 2*x k + 2 and 2*y k +1 = 2*y k - 2

Step 4: Determine the symmetry points in the other seven octants

Step 5: Move each calculated pixel position ( x, y ) on to the circular path centered
on ( xc , yc ) and plot the coordinate values as :

x = x + xc and y = y + yc

Step 6 : Repeat the steps 3 through 5 until x >= y


Solved Example - Midpoint Circle
Algorithm
Given the radius of circle r = 7, with center at origin, rasterize the circle
using midpoint circle drawing algorithm
Solution : The center of given circle is at origin (0,0) and radius of circle is r = 7

therefore we take starting point as (x0,y0) =


(0,r) (x0,y0) = (0,7)

The initial value of pk is given


as pk = 1 - r
=1-7
pk = -6

We will calculate points till x<=y


as below
Solved Example - Midpoint Circle
Solution : Algorithm
st eps value of pk calculation of (x,y) Plot
point As pk < 0, so
1 -6
x = x+1 = 0+1 = 1 (1,7)
y=y=7
(x,y) = (1,7)
pk =
pk+2*(x+1)+1
= -6+2*(0+1)+1
= -3
As pk < 0, so (2,7)
2 -3
x = x+1 = 1+1=2
y=y=7
(x,y) = (2,7)
pk = pk+2*(x+1)+1
= -3+2*(1+1)+1
=2
Solved Example - Midpoint Circle
Solution : Algorithm
st eps value of pk calculation of (x,y) Plot
point As pk > 0, so
3 2
x = x+1 = (3,6)
2+1=3 y = y-1
=7-1 = 6
(x,y) = (3,6)
pk =
pk+2*(x+1)+1
-2*(y+1)
As pk < 0, so (4,6)
4 -7 =
x =2+2*(2+1)+1
x+1 = 3+1 = 4
y = -2*(7+1)
y=6
(x,y)==-7(4,6)
pk = pk+2*(x+1)+1
= -7+2*(3+1)+1
=2
Solved Example - Midpoint Circle
Solution : Algorithm
st eps value of pk calculation of (x,y) Plot
point As pk > 0, so
5 2
x = x+1 = 4+1 = (5,5)
5 y = y-1 =6-1 =
5
(x,y) = (5,5)
pk =
pk+2*(x+1)+1
-2*(y+1)
As pk < 0, so ---
6 -1 =
x =2+2*(4+1)+1-
x+1 = 5+1 = 6
y = 2*(6+1)
y=5
As x>y
= -1, so we will stop here and
not calculate new points
C Program of Midpoint Circle
#include<conio.h> Algorithm else if(pk>=0)
#include<graphics.h> {
void main() x=x+1;
{
y=y-1;
int gd = DETECT,gm;
int x,y,xc,yc;
pk =
int radius;
pk+2*(
float pk;
x+1)+1
-
printf("\n Enter the coordinates of center of circle xc and yc : ");
2*(y+1
scanf("%d%d",&xc,&yc);
);
printf("\n Enter the radius of circle: ");
scanf("%d",&radius); }
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI"); putpixel(xc+x,yc+y,RED);
x = 0; putpixel(xc+y,yc+x,RED);
y = radius; putpixel(xc-y,yc+x,RED);
pk = (5/4)-radius; putpixel(xc-x,yc+y,RED);
while(x<=y)
putpixel(xc-x,yc-y,RED);
{
putpixel(xc-y,yc-x,RED);
if(pk<0)
{ putpixel(xc+y,yc-x,RED);
x=x+1; putpixel(xc+x,yc-y,RED);
y=y; }
pk = getch();
pk+2*(
closegraph();
x+1)+1
Output of Midpoint Circle
Progam
Polygons
vert ex
edge
Polygon is a plane figure that is created
by joining straight lines to form a c
losed polygon chain or polygon circuit

The line segment s of a polygon


circuit are called as edges or sides
Polygon
The points where two edges of
polygon meet are called as v
ertices or corners

The interior of the solid polygon


is called as its body

Not a
polygon
Types of Polygons

1) Convex Polygon :

Any line drawn through meets its boundary exactly t wice

All interior angles are less than 1800

Any line segment with endpoints on the boundary passes through only
interior points between its endpoints

Convex Polygon
Types of Polygons

2) Simple Polygon :

The boundary of the polygon does not cross


itself
All convex polygons are
simple. Simple Polygon

3) Concave Polygon :
A line may be found which meets its boundary
more than twice ( non-convex and simple )

There is a line segment between two


boundary points that passes o utside the
polygon
Concave Polygon
There is at least one interior angle greater than 1
80°
Types of Polygons

4) Star-shaped Polygon

The whole interior is visible from at least


one point, without crossing any edge

The polygon must be simple and all


convex polygons are star shaped
Star-shaped Polygon

5) Self-intersecting Polygon

the boundary of the polygon crosses


itself

Self-intersecting Polygon
Types of Polygons
6) Star Polygon

A polygon which self-intersects in a regular way

A polygon cannot be both a star and star-


shaped

Star Polygon
Polygons : Inside - Out side Test

Area filling algorithms with colors and other graphics process require to identify
i nterior and e xterior regions of objects such as polygons

It is not always clear that which region in xy plane should be called as interior
region and which region should called as exterior region

Graphics package uses following two methods to identify interior regions of objects

1) Odd-Even Rule / Odd Parity Rule

2) Non-zero Winding Number Rule


Odd - Even
Rule
Odd-even rule is also called Odd-parity rule or E ven-odd rule

In odd-even rules straight line is drawn from any point 'P' to distant point outside
the coordinate extent of object or polygon

The path of straight line should not i ntersect or pass through any vertices of a
polygon

If the number of edges crossed by this line is o dd then point 'P' is interior point of
the polygon

If the number of edges crossed by this line is e ven then point 'P' is exterior point of
the polygon
Odd - Even Rule - Example

1) Draw a straight line point from poits P, Q, M as shown


in figure

2) The number of edges crossed by straight line from


P
point P is 2 , which is even hence point P is exterior
M
point to polygon
Q
3)The number of edges crossed by straight line from
point Q is 2 which is even hence point Q is also
exterior point to polygon

4)The number of edges crossed by straight line from


point M is 1, which is odd hence point M is interior
point to polygon
Nonzero Winding Number
Rule
This method counts the number of times the polygon edges w ind around a
particular point in c ounterclockwise direction

This count is called as winding number

The initial ( starting ) value of winding number is set to 0

Imagine a straight line is drawn from any point 'P' to distant point outside
the coordinate extent of object or polygon

The path of straight line should not i ntersect or pass through any vertices of
a polygon
Nonzero Winding Number
Rule
As we move along the line from position 'P' tothe distant point, we calculate n
umber
of edges that crosses the lin in each direction

1 is added to the winding number if edge of the polygon crosses the line from right
to
left

1 is subtracted from the winding number if edge of the polygon crosses the line from
left to right

If the final value of the winding number after all crossings have been calculated is
n onzero thenpoint 'P' is i nterior point of polygon

If the final value of the winding number is z ero then point 'P' is exterior point
Nonzero Winding Number Rule -
Example
1)Draw a straight line point from points H and K as
shown in figure

2) For point H set starting value of winding number


=0 H
3) For point K set starting value of winding number =
0
4) First edge of the polygon crosses line from point H K

from right to left hence 1 is added to winding


number
4) Second edge of the polygon crosses line from point
H from left to right hence 1 is subtracted from
winding number
Polygon Filling Algorithms

Scan Line Algorithm Boundary Fill Algorithm Flood Fill Algorithm

4- 8- 4- 8-
Connect ed Connect ed Connect ed Connect ed
Algorithm Algorithm Algorithm Algorithm
Scan Line Polygon Fill Algorithm
For each scan that crosses the polygon, the
algorithm locates intersection point between
s can line and edge of polygon as shown in fig. y

The intersection points are sorted from left to


right in x-direction and paired together

All the pixels between each intersection pair


of points are then filled with given color
m1 m2 m3 m4

In the given fig. a scan line intersects


polygon at points m1, m2, m3 and m4

The intersection point are paired as m 1-m2


and 20 24 28 32 x
m2-m3 and m3-m4
All the interior pixels from x=20 to x=24 and x=28 to
x=32 are filled with color as they are inside the
Boundary-Fill Algorithm

We can fill the polygon with color by taking an interior point (seed point) and then
start filling color from interior to outward till the boundary of polygon is reached

If the boundary is specified in single color, the fill algorithm proceeds outward pixel
by pixel until the boundary color is identified

A boundary fill procedure accepts three inputs

interior point ( x, y ) = this point is i nside the boundary of

polygon fill color - this is color to be filled in the polygon

boundary color - this is the color of boundary


Boundary-Fill Algorithm

The algorithm starts from interior point ( x, y ) and checks the neighboring pixels
to decide whether they are of boundary color or not

If not, those pixels are painted with fill color and and their neighbors are tested

This process continues till all the pixels upto the boundary color for the area have
been tested

boundary color ( green )

fill color ( red )


Types of Boundary-Fill Algorithm

4 - Connected - Four neighbors are 8- Connected - Eight neighbors are tested


tested
neighouring pixels

1 2 3
1

8 4
4 2

7 6 5
3
current pixel
4 - Connected Boundary-Fill Algorithm

Step 1: Initialize the value (starting values ) of seed point


i.e
interior point ( x, y ) , fill color and default color ( original
interior color of polygon ) ( x,
y+1 )

Step 2: Define the boundary values and c olor of


polygon seed point
( x-1, (x+1, y )
Step 3: If the current seed point is of default y)
( x, y )
color , repeat steps 4 and 5

Step 4: Change the default color with fill color at (x, y-1 )
the seed point

Step 5: Recursively follow the same procedure with


four neighborhood points
Step 6: Exit
C Program of Boundary Fill Algorithm - 4
Connected
#include<stdio.h> void boundaryfill4(int x,int y,int
#include<conio.h> fcolor,int bcolor)
#include<graphics.h> {
void boundaryfill4(int x,int y,int fcolor,int bcolor); if(getpixel(x,y)!=fcolor &&
void main() getpixel(x,y)!=bcolor)
{ {
int gd=DETECT,gm; delay(2);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI"); putpixel(x,y,fcolor);
setcolor(WHITE); boundaryfill4(x+1,y,fcolor,bcolor);
rectangle(100,100,300,300); boundaryfill4(x,y+1,fcolor,bcolor);
boundaryfill4(120,120,RED,15); boundaryfill4(x-1,y,fcolor,bcolor);
getch(); boundaryfill4(x,y-1,fcolor,bcolor);
closegraph(); }
} }
Output of Boundary Fill 4-Connected
Program
8 - Connected Boundary-Fill Algorithm

Step 1: Initialize the value (starting values ) of seed


point
i.e interior point ( x, y ) , fill color and default color
( original interior color of polygon ) ( x, (x+1, y +1
( x-1, y
+1) y+1 )

Step 2: Define the boundary values and c olor of


polygon seed point
( x-1, (x+1, y )
Step 3: If the current seed point is of default y)
( x, y )
color , repeat steps 4 and 5

Step 4: Change the default color with fill color at ( x-1, y (x, y-1 ) (x+1, y -1
-1)
the seed point

Step 5: Recursively follow the same procedure with


eight neighborhood points

Step 6: Exit
Output of Boundary Fill 8-Connected
Program
Flood-Fill Algorithm

Flood-fill algorithm is used to fill or r ecolor an area boundary with different


which colors
do not have single color
boundary

In Flood-fill algorithm we replace the specified


interior color with fill color and do not search
for
boundary
We start with interior point and replace the
color of interior point with desired fill color interior area to
be painted

We recursively repeat the procedure either using 4


- connected or 8 -connected method, till all all
interior points are painted with fill color
Flood-Fill Algorithm

Step 1 : Read any seed pixel or interior point ( x, y )

Step 2 : Check the interior pixel ( x, y ) has old color or not


if it has old color replace the o ld color with n ew color or given fill color

Step 3 : Repeat step 3 for all other neighboring pixels

Step 4: If all the interior pixels have been painted and no pixel of old color is
found then go to step 5

Step 5: Stop
C Program of Flood Fill
Algorithm
#include<stdio.h> void flood(int x,int y,int fillcolor,int defaultcolor)
#include<conio.h> {
#include<graphics.h> if(getpixel(x,y)==defaultcolor)
void flood(int x,int y,int fillcolor,int defaultcolor); {
void main() delay(1);
putpixel(x,y,fillcolor);
{
flood(x+1,y,fillcolor,defaultcolor);
int oldcolor;
flood(x,y+1,fillcolor,defaultcolor);
int gd = DETECT,gm;
flood(x-1,y,fillcolor,defaultcolor);
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI"); flood(x,y-1,fillcolor,defaultcolor);
rectangle(100,100,300,300); }
oldcolor = getpixel(120,120); }
flood(120,120,GREEN,oldcolor);
getch();
closegraph();
}
Output of Flood Fill
Program
Scan Conversion

Scan conversion is a process of representing graphics image or object as


a collection of pixels

Video display device of a computer converts binary values ' 0' or ' 1' into pixel's on and
off s tate

' 0' means pixel is off and ' 1' means pixel is o n

Using this information graphics computer draws image with d istinct dots

Any image or object can be drawn from d ense matrix of dots or points or pixel

Scan conversion process is also called as R asterization


Scan Converting a Point

Suppose point P( 2.50, 1.75 ) is a point in an image area, the coordinates of point are r
eal
numbers. We need to scan convert this point to draw it on display device. For scan
converting this point we only take integer part of the point like FLOOR(x) and
FLOOR(y) which equal to P( 2, 1 ). Then this point is point is drawn on screen.
y Location of point P( 2, 1) after
scan conversion

2
first row second column
1

0 1 2 3 4 x
Scan Conversion Process of Straight
Line
y-
axis
Straight line
y2

y1

x-
axis
x1 x2
Sampling of line
Frame Buffers

Frame buffer is a portion of Random Accesss Memory (RAM) which stores picture
defination (information)

This memory area holds set of intensity values for all the screen points

Stored intensity values are then taken from frame buffer and painted on
screen on row (scan line) at a time

It is also called as refresh buffer or framestore


Frame Buffers

System Frame Video


CPU
Memory buffer Controller Monitor

System Bus

I/O Devices

Fig : Architecture of a raster system with a fixed portion of the


system memory reserved for the frame buffer
Architecture of Raster
System
CPU : It is central processing unit that performs arithmetic, logical and control
operations

System memory : This memory stores current program and d ata, it is


RAM memory and does not store data permanently

Frame Buffer : It is part of RAM which stores information about pictures


and
images

Video Controller : Special processor used to control operations of display


device

Monitor : It is output display device like CRT, which shows images and
pictures

System bus : This is the main bus that connects main components of
computer system, it transfers d ata, a ddresses and also decide control
Character Generation Methods

In computer graphics we use following three methods to display letters,


characters and numbers in various different style and size

Stroke Method Starbust Method Bitmap Method


Stroke Method
Stroke method uses small line segment to draw a character

Just like we use strokes of pen to write characters , in


the same way we line segments
are drawn to create a character

As characters are drawn using series of line segments,


we can various line drawing algorithms to generate a
character
st art

We can create our own stroke method by deciding


Fig: Stroke Method
which line segments to draw for which character

In this method we can increase or decrease (scaling) the length of line


segment used for generating the character method
Starbust Method
In starbust method there are fixed pattern of line segments which are used
to draw characters

There are total 2 4 line segments

Out of these 24 line segments only those line segments are drawn which
are required to display a particular character

The pattern of lines for each character are stored in 2 4 bit code, each bit in
this code is a line segment

For example - 24 bit code for character ' A' is 0 11 0000 0011 1100 1110 0001
Similarly 24 bit code for character ' M' is 0 000 0011 0000 1100 1111 0011
Starbust Method
Disadvantages of starbust method :

Requires more memory as 24 bits are required for each character

Quality of characters drawn is poor, curved shaped characters are not


drawn properly

Requires software to convert 24 bit code to character display on


screen

Fig: Character 'A' and 'M' drawn using starbust method

Fig source: slideplayer.com


Bitmap Method
In bitmap method characters are drawn using array(series) of dots in matrix form
(columns and rows)

This method is also called as d ot matrix method

Each dot in the matrix is actually a pixel

It has two dimensional array of columns and rows

5x7 array is commonly used to draw characters but, 7 x9 and 9 x13 are also
used

Higher resolution devices like inkjet printers and laser printers even use array
of more than 1 00x100
Bitmap Method
dot matrix with 5
columns and 7 dot (pixel)
rows

Fig: Character 'C' drawn using 5x7 dot


matrix
Thank you!

You might also like