Scan Conversion Algorithm
It is the process of representing graphics objects as a collection of discrete pixel. Each pixel can
have either on or off state.
1. Line Drawing algorithm
2. Circle Drawing algorithm
3. Ellipse Drawing algorithm.
1. Line Drawing algorithm
a. Direct use of line Drawing.
b. Digital differential analyzer algorithm
c. Bresenham Line drawing algorithm.
A line can be defined as s straight one dimension figure. It has no thickness and can be extends
endless by in both direction. A line segment is only the part of line. It is described as distance
between any two points.
Equation of Line
Y=mx+b
m=dy/dx
dx=x2-x1
dy=y2-y1
In computer graphics line can be draw by plotting the pixel in sequence.
Direct Method For Line Drawing
It is the simplest method for line drawing . First read two end points of the line the using line
equation y=mx+c, find out other point lie on the line to draw.
Algorithm
Step 1: Read two ends points p1(x1,y1) and p2(x2,y2)
Step2: calculate
dx=x2-x1
dy=y2-y1
step 3: m=dy/dx
step 4: set(x,y) to straight point
if dx>0
x=x1
y=y1
xend=x2
if dx<0
x==x2
y=y2
xend=x1
Step 5: now calculate c=y-mx
Step 6: plot the point at current (x,y) coordinates.
Step 7: increment x=x+1, y=mx+c
1
Setp 8: if x=xend
Stop
Otherwise go to step 6.
DDA line drawing algorithm
The digital differential analyzer is a scan conversion line drawing algorithm based on calculating
either dx or dy from the equaltion. m=dy/dx.
There are three cases in DDA algorithm.
Case 1: m<1 (xk=x+1)(yk=y+m)
Case 2: m>1(xk=x+(1/m))(yk=y+1)
Case 3:m=1(xk=x+1)(yk=y+1)
Algorithm for DDA line drawing algorithm
Step 1: start
Step2: input the line endpoints and store the left endpoints(x1,y1)and right end points (x2,y2)
Step3: calculate the value of dx,dy,
dx=x2-x1 ,dy=y2-y1
step 4: if(abs(dx)>abs(dy) then
steplength=abs(dx)
else
steplength=abs(dy)
step 5: calculate the value of x-increment and y-increments
x-increment=dx/setplength
y-inclrement=dy/steplength
setp 7: draw the pixel (round(x),round(y))
step 8: for k=0to steplength do
x=x+x-increments
y=y+y-increments
draw the pixel(round(x), round(y)
step 9: End
Q. Digitize the line with endpoints (1,5)and (7,2) using DDA algorithm.
Solution:-
dx=7-1=6
dy=2-5=-3
steplength=6
x-inc=6/6=1
y-inc=-3/6=-0.5
K xk+1 yk+1 (xk+1,yk+1) Plot in screen
1 2 4.5 2,4.5 2,5
2
2 3 4 3,4 3,4
3 4 3.5 4,3.5 4,4
4 5 3 5,3 5,3
5 6 2.5 6,2.5 6,3
6 7 2 7,2 7,2
1. Digitize the line with endpoints (1,-6) and (4,4) using DDA algorithm.
2. Digitize the line with endpoints (1,6), (6,10) using DDA algorithm.
3. Trace DDA algorithm for line with endpoints (1,2),(5,6)
4. Trace DDA algorithm for endpoints (1,7),(6,3)
Bresenham Line Drawing Algorithm(BSA)
Step 1: plot initial point
Step 2: find dx, dy, 2dy, 2dy-dx
Where dx=x2-x1, dy=y2-y1
Step 3: calculate the initial decision parameter
P0=2dy-dx
Step 4: start at iteration k=0 at each x,k do this
a) If pk<0
Plot(xk+1,yk)
Pk+1=pk+2dy
b) If pk>=0
Plot(xx+1,yk+1)
Pk+1=pk+2dy-2dx
Step 5: Repeat step4 dx no of times(0 to dx-1)
Setp 6: End
Q. digitize a line with end points (10,15) and (15,18) using BSA
dx=15-10=5
dy=18-15=3
P0=2dy-dx=6-5=1
k x Y pk (xk+1,yk+1)
0 10 15 1 11,16
1 11 16 -3 12,16
2 12 16 3 13,17
3 13 17 -1 14,17
4 14 17 5 15,18
1. Draw a line using BSA with the end points(20,10),(30,18)
3
2. (15,15)(10,11)
3. (5,10)(10,7)
4. (20,30)(30,22)
General Bresenham’s algorithm for all octants
Input: start point (x1,y1), end points(x2,y2)
x=x1, y=y1
dx=abs(x2-x1)
dy=abs(y2-y1)
s1=sign(x2-x1)
s2=sign(y2-y1)
if dy>dx then
T=dx,dx=dy,dy=T, interchange 1
Else
Interchange 0;
End if
E=2dy-dx
A=2dy
B=2dy-2dx
Plot( x,y)
For i=1 to dx if
(E<0)
If interchange 1
y=y+s2
else
x=x+s1
E=E+A
Else
Y=y+s2
X=x+s1
E=E+B
End if
Set pixel (x,y)
End for
End
Note: sign function return :- -1 if its argument is <0
0 if its argument is=0
+1 if argument is >0
4
Q. End points (10,18),(15,8)
x=10
y=18
dx=5
dy=10
s1=sign(x2-x1)=1
s2=sign(y2-y1)=-1
since dy>dx
T=dx, dx=dy, dy=T
dx=10,dy=5
E=2dy-dx=10-10=0
A=2dy=10
B=10-20=-10
Interation E x y plot
1 0 11 17 11,17
2 - 11 16 11,16
10
3 0 12 15 12,15
4 - 12 14 12,14
10
5 0 13 13 13,13
6 - 13 12 13,12
10
7 0 14 11 14,11
8 - 14 10 14,10
10
9 0 15 9 15,9
10 - 15 8 15,8
10
Q.(15,15)(10,11)
x=15, y=15
dx=5, dy=4
s1=x2-x1=10-15=-1
s2=y2-y1=11-15=-1
E=2dy-dx=8-5=3
A=8
B=8-10=-2
k E x y Plot
1 3 14 1 14,14
4
2 1 13 1 13,13
3
3 -1 12 1 12,13
5
3
4 7 11 1 11,12
2
5 5 10 1 10,11
1
Circle Drawing algorithm
Direct method or polynomial method to drawing circle
(x-xc)2+(y-yc)2=r2
(y-yc)2=r2-(x-xc)2
(y-yc)=± √ r 2−(x −xc)2
y=yc± √ r 2−(x −xc)2
Algorithm
Step 1: Input center (xc,yc) and radius =r
Step 2: Set initial value x=xc-r ,y=yc
Step 3: plot pixel(x,y)
Step 4: while (x<=xc+r)
Increment x=x+1
Compute y= yc± √ r 2−(x −xc)2
Round off value of y
Plot (x,y)
Step 5: End
Midpoint circle Drawing Algorithm
It is difficult to draw a circle of given center (x c,yc) with radius r, so we will calculate pixel
position at origin(0,0)and radius r then add(xcyc) to (x & y) coordinate respectively.
Algorithm
Step 1: Input center(xc,yc) and radius
Step 2: initial value x=0, y=r
Step 3:plot pixel (x+xc, y+yc)
Step 4: Decision parameter 1-r
Step 5: While x<=y
If p<0 then
x=x+1
p=p+2x+1
plot pixel(x+xc, y+yc)
else
x=x+1, y=y-1
6
p=p+2x+1-2y
plot pixel(x+xc, y+yc)
step 6: End
Q. Calculate the point to draw circle having radius10 and center at 0,0.
r=10
x=0,y=10
p=1-10=-9
k pk xk+1,yk+1
1 -9 1,10
2 -6 2,10 X,y
3 -1 3,10
4 6 4,9
5 -3 5,9
6 8 6,8
7 5 7,7
(x,y) 0,10 1,10 2,10 3,10 4,9 5,9 6,8 7,7
(-x,y) 0,10 -1,10 -2,10 -3,10 -4,10, -5,9 -6,8 -7,7
(x,-y) 0,-10
(-x,-
y)
(y,x)
(-y,x)
(y,-x)
(-y,-
x)
Q. Calculate the point to draw circle having radius 5 and center at 2,3.
xc=2, yc=3
r=5
x=0,y=5
p=-4
k pk xk+1,yk+1 Actual pixel
1 -4 1,5 1+2,5+3(3,8)
2
3
4
5
6
7
7
Bresenhams Circle Drawing Algorithm
Step 1: Determine the radius r of the circle
Step 2: input the center of the circle.
Step 3: plot the pixel of octant as(0,r)(x=0,y=r)
Step 4: Calculate the initial decision parameter
P=3-2r
Step 5: Repeat till x<=y
If p<0
Pk+1=pk+4xk+6
Xk+1=xk+1
Yk+1=yk
Else
Pk+1=pk+4(xk-yk)+10
Xk+1=xk+1
Yk+1=yk-1
Step 6: plot (xk+1,yk+1)
Step 7: Determine and plot the symmetric point in other octant as well.
Ellipse Algorithm
Step 1: read input center(xc,yc) and rx and ry for the ellipse and obtain the first points.
Step 2: Initialize starting point of region 1as x=0,y=ry (0,ry)
1
Step 3: p10=ry2-rx2ry+ rx2
4
Step 4: Calculate dx=2ry2x, dy=2rx2y
Step 5: Repeat while (dx<dy)
Plot(x,y)
R2
If(p1<0)
R1
x=x+1
p1k+1=p1+2ry2x+xy2
else
x=x+1, y=y-1
update dx(2ry2x),dy(2rx2y)
p1k+1=p1+dx-dy+ry2
When dx>=dy plot region 2 as:
1 2
Find p20=ry2( x + ¿ ¿ +rx2(y-1)2-rx2ry2
2
Repeat till (y>0)
Plot(x,y)
If (p2>0)
x=xk, y=y-1
update dy
8
pk+1=p2-dy+rx2
else
x=x+1
y=y-1
dy=dy-2rx2
dx=dx+2ry2
pk+1=p2+dx-dy+rx2
9
Q. Draw ellipse where input ellipse parameters rx =8 and ry=6.
1
p10=ry2-rx2ry+ rx2
4
p1k+1=p1+2ry x+xy2(-44+216+36
2
p1k+1=p1+dx-dy+ry2(208+288+36-640
36-(64*6)+1/4(64)
k xk ,yk p1k xk+1, yk+1 (dx)2xk+1ry2 (dy)2yk+1rx2
0 0,6 -332 1,6 72 768
1 1,6 -224 2,6 144 768
2 2,6 -44 3,6 216 768
3 3,5 208 4,5 288 640
4 4,5 -108 5,5 360 640
5 5,5 288 6,4 432 512
6 6,4 244 7,3 504 384
Plot for Region 2
0 7,3 -23 8,2 576 256
1 8,2 361 8,1 576 128
2 8,1 297 8,0
Area filling Algorithm
A polyline is a chain of connected line segment. When polyline is closed then it is called
polygon.
Types of polygons
The classification of the polygons is based on where the line segment are joining
a. Convex:- The polygon in which the line segment joining any two points within the
polygon lies completely inside the polygon.
b. Concave:- The polygon in which the line segment joining any two points within the
polygon may not lie completely inside the polygon.
There are two basic approaches to area filling in raster system. One way to fill area is to
determine the overlap intervals for scan lines that cresses the area. Another method for area
10
feeling is to start from a given interior position and point outward from this until a specified
boundary is met.
There are three popular methods for area fill algorithm
i. Scan line polygon fill algorithm
ii. Boundary fill algorithm
iii. Flood fill algorithm
Scan line polygon fill algorithm
Scan line polygon filling algorithm is used for solid color feeling in polygons.
y-axis
x-axis
Steps to performs
For scan line polygon feeling there are three steps to perform in the following order.
1. Find the intersection of the scanline withal edges of the polygon.
2. Sort the intersection by intersecting x-coordinate i.e. from left to right.
3. Make pairs o the intersection and fill in color within all the pixels inside the pair.
Special Cases
Some Scan line intersection at polygon vertices require special handling.
If scanline pass through on vertex both the edges that ate connected to the vertex are on
the same side of the scanline then we need to count the vertex twice.
If scanline pass through on vertex both the edges that are connected to the vertex are on
opposite site of the scanline then we need to count the vertex as a single intersection
point.
Boundary Fill Algorithm
11
In boundary fill algorithm the basic concept is filling the color in closed area by starting
at a point inside a region and paint the interior outward towards the boundary.
One requirement for boundary fill algorithm is that the boundary has to have a single
color.
In boundary fill algorithm you start from a point inside the region and fill the color
interior towards the boundary pixel by pixel.
So the process is that you check the default color of the pixel before filling. If the color is
not a boundary color then you fill it with the fill color and move to the next pixel and
check for same criteria fill you encounter the boundary colored pixel or boundary.
Method of Implementation
There are two methods in which boundary fill algorithm can be implemented.
1. 4 connected pixel
2. 8 connected pixel
12