Chapter 2 Scan Conversion Algorithm - 241121 - 135049
Chapter 2 Scan Conversion Algorithm - 241121 - 135049
Case I:
If |m| ≤ 1, we sample at unit x interval i.e △x = 1.
xk+1 = xk+1 ..…..……………..(iii)
Then we compute each successive y-values using equation i by setting △y = m
(since, m=△y/△x , △x=xk+1 – xk = 1, △y= yk+1 – yk).
So, yk+1 = yk + m ……………………..(iv)
The calculated y value must be rounded to the nearest integer. Repeat △x times.
Case II:
If |m| > 1, we sample at unit y-interval i.e △y = 1
yk+1 = yk + 1…………….(v)
Then we compute each successive x-values using equation i by setting △x =
1/m
(Since, m=△y/△x and △y=△y= yk+1 – yk =1, △x=xk+1 – xk )
So, xk+1 = xk + 1/m… ……..(vi)
The calculated x value must be rounded to the nearest integer. Repeat △y times.
The obtained equations also can be used to calculate the pixel position along a line with
negative slope.
Advantage of DDA
1. It is simple to understand.
2. It is faster method than direct use of line equation y=mx+c. (Removes multiplication
operation and only involve increments operation of x or y direction)
3. It requires no special skills for implementation
Disadvantages of DDA
1. A floating point addition is still needed in determining each successive point which is
time consuming.
2. The accumulation of round off error in successive addition of the floating point
increments may cause the calculated pixel position to drift away from the true line
path for long line segment.
C-Implementation
void DDA(int x1,int y1,int x2, int y2)
{
int dx,dy,xplot,yplot,i,steps;
float slope,x,y;
dx=x2-x1;
dy=y2-y1;
slope = (float)dy /(float)dx;
x=x1;
y=y1;
xplot=x;
yplot=y;
if(fabs(slope)<=1)
{
for(i=0;i<abs(dx);i++)
{
putpixel(xplot,yplot,RED);
x = x + 1;
y= y + slope;
xplot = x;
yplot=round(y);
}
}
else
{
for(i=0;i<abs(dy);i++)
{
putpixel(xplot,yplot,RED);
y=y+1;
x=x + (1.0/slope);
xplot=round(x);
yplot=y;
}
}
}
Numerical Examples
1. Consider a line from (2,1) to (8,3). Using simple DDA algorithm, rasterize this line.
Solution:
Given Starting Point :(x1, y1)= (2,1) and Ending Point: (x2,y2) = (8,3)
Xk+1 = Xk + 1
Yk+1 = Yk + (M)
0 2 1 2 1 (2,1)
1 3 1.33 3 1 (3,1)
2 4 1.66 4 2 (4,2)
3 5 1.993 5 2 (5,2)
4 6 2,326 6 2 (6,2)
5 7 2.659 7 3 (7,3)
6 8 2.999 8 3 (8,3)
Therefore, the digitized coordinates are (2,1), (3,1), (4,2), (5,2), (6,2), (7,3), (8,3).
2. Digitized the line with end points (0,0) and (4,5) using DDA.
Given Starting Point :(x1, y1)= (0,0) and Ending Point: (x2,y2) = (4,5)
yk+1 = yk + 1
xk+1 = xk + (1/m)
0 0 0 0 0 (0,0)
1 0.8 1 1 1 (1,1)
2 1.6 2 2 2 (2,2)
3 2.4 3 2 3 (2,3)
4 3.2 4 3 4 (3,4)
5 4 5 4 5 (4,5)
Therefore, the digitized coordinates are (0,0), (1,1), (2,2), (2,3), (3,4), (4,5).
3. Digitized the line with end points (3,7) and (8,3) using DDA.
Given Starting Point :(x1, y1)= (3,7) and Ending Point: (x2,y2) = (8,3)
Xk+1 = Xk + 1
Yk+1 = Yk + (M)
0 3 7 3 7 (3,7)
2 5 5.4 5 5 (5,5)
3 6 4.6 6 5 (6,5)
4 7 3.8 7 4 (7,4)
5 8 3 8 3 (8,3)
4. Consider a line from (0,0) to (5,5). Using simple DDA algorithm, rasterize this line.
Solution:
Given Starting Point :(x1, y1)= (0,0) and Ending Point: (x2,y2) = (5,5)
Xk+1 = Xk + 1
Yk+1 = Yk + (M)
0 0 0 0 0 (0,0)
1 1 1 1 1 (1,2)
2 2 2 2 2 (2,2)
3 3 3 3 3 (3,3)
4 4 4 4 4 (4,4)
5 5 5 5 5 (5,5)
Derivation
See class notes for Detail Derivation of Decision Parameter (both case)
If pk < 0
Plot pixel(xk, yk+1), and
Set pk+1 = pk +2Δx
Otherwise,
o Plot pixel(xk+1, yk+1), and
o Set pk+1 = pk + 2Δx - 2Δy
Step 5: Repeat step 4, Δy times.
Step 6: End
Note:
Horizontal lines (Δy = 0, vertical lines (Δx = O), and diagonal lines with Δx =Δy each can be loaded directly
into the frame buffer without processing them through the line-plotting algorithm.
For negative slopes, the procedures are similar, except that now one coordinate
decreases as the other increases.
C-Implementation
float m;
int dx,dy,p,xplot,yplot,i,x,y;
dx=abs(x2-x1);
dy=abs(y2-y1);
m=(float) dy/dx;
x=x1;
y=y1;
if(m<=1)
{
p= 2 * dy -dx;
for (i=0;i<abs(dx);i++)
{
if(p<0)
{
putpixel(x,y,RED);
x=x+1;
y=y;
p= p + 2*dy;
}
else
{
putpixel(x,y,RED);
x=x+1;
y=y+1;
p=p + 2 * dy - 2* dx;
}
}
}
else
{
p= 2 * dx - dy;
for (i=0;i<abs(dy);i++)
{
if(p<0)
{
putpixel(x,y,RED);
x=x;
y=y+1;
p= p + 2*dx;
}
else
{
putpixel(x,y,RED);
x=x+1;
y=y+1;
p=p + 2 * dx - 2* dy;
}
}
}
Questions:
1. Apply Bresenham’s algorithm to draw a line from (-3,0) and end point is (4,4)
Solution
Starting Point (x0,y0) = (-3, 0) Ending Point (xn, yn) = (4,4) such that scanning takes place from left to right
Slope m = (4-0)/ ( 4+3) = 4/5 = 0.57 i.e. |M|≤1
Here: Δx= 4 + 3 = 7 Δy= 4 - 0 = 4 2Δx = 2 * 7 = 14 2Δy = 2 * 4 = 8
Now from Bresenhams Algorithm for |M|<1, and scanning from left to right
K Pk (Xk+1, Yk+1
0 (2 * 4 -7)=1 (-3+1, 0+1) = (-2,1)
1 (1 + 8-14)= -5 (-2+1, 1)=(-1,1)
2 (-5+8)=3 (-1+1, 1+1)=(0,2)
3 -3 (1,2)
4 5 (2,3)
5 -1 (3,3)
6 7 (4,4)
Therefore the digitized coordinates are (x0, y0) = (-3, 0), (x1, y1) = (-2, 1),………..(x7,y7) = (4,4)
2. Apply Brenham’s algorithm to draw a line from (3,7) and end point is (8,3)
Solution
Starting Point (x0,y0) = (3, 7) Ending Point (xn, yn) = (8, 3) such that scanning takes place from left to right
Slope m = (3-7)/ ( 8-3) = -0.8 i.e. |M|≤1
Here: Δx= │8-3│ = 5 Δy= │3-7│ = 4 2Δx = 2 * 5 = 10 2Δy = 2 * 4 = 8
Now from Bresenhams Algorithm for |M|<=1, and scanning from left to right
K Pk (Xk+1, Yk+1
0 8-5=3 (4,6)
1 3 + 8 – 10 = 1 (5,5)
2 1 + 8 – 10 = -1 (6, 5)
3 -1 + 8 = 7 (7,4)
4 7 + 8 – 10 = 5 (8,3)
Therefore the digitized coordinates are (3,7), (4,6) , (5,5) , (6,5) , (7,4) , (8,3)
3. Apply Bresenham’s algorithm to draw a line from (20,15) and end point is (30,30)
Solution
Starting Point (x0,y0) = (20, 15) Ending Point (xn, yn) = (30, 30) such that scanning takes place from left to right
Slope m = (30-15)/ ( 30-20) = 1.5 i.e. |M|>1
Here: Δx= 30-20 = 10 Δy= 30-15 = 15 2Δx = 2 * 10 = 20 2Δy = 2 * 15 = 30
Now from Bresenhams Algorithm for |M|>1, and scanning from left to right
K Pk (Xk+1, Yk+1
0 5 (21,16)
1 -5 (21,17)
2 15 (22,18)
3 5 (23,19)
4 -5 (23,20)
5 15 (24,21)
6 5 (25,22)
7 -5 (25,23)
8 15 (26,24)
9 5 (27,25)
10 -5 (27,26)
11 15 (28,27)
12 5 (29,28)
13 -5 (29,29)
14 15 (30,30)
Therefore the digitized coordinates are (x0, y0) = (20,15), (x1, y1) = (21,16),………..(x15, y15) = (30,30)
4. Apply Bresenham’s algorithm to draw a line from (6, 12) and end point is (10, 5)
Solution
Starting Point (x0,y0) = (6, 12) Ending Point (xn, yn) = (10,5) such that scanning takes place from left to right
Slope m = (5-12)/ ( 10-6) = -7/4 = -1.75 i.e. |M|>1
Here: Δx= │`10-6│ = 4 Δy= │5-12│ =7 2Δx = 2 * 4 = 8 2Δy = 2 * 7 = 14
Now from Bresenhams Algorithm for |M|>1, and scanning from left to right
Initial Decision Parameter: P0 = p0 = 2Δx – Δy
if(pk<0) then
Xk+1 = Xk and
Yk+1 = Yk-1 and
Pk+1 = Pk + 2Δx
otherwise
Xk+1 = Xk+1 and
Yk+1 = Yk-1 and
Pk+1 = Pk + 2Δx - 2Δy
Repeat Δy times
K Pk (Xk+1, Yk+1
0 2 * 4 -7=1 (6+1, 12-1)=(7,11)
1 1 + 8 – 14=-5 (7, 11-1)=(7,10)
2 -5+8=3 (7+1, 10-1)=(8,9)
3 3+8-14=-3 (8,9-1)=(8,8)
4 -3+8=5 (8+1, 8-1)=(9,7)
5 5+8-14=-1 (9,7-1)=(9,6)
6 -1+8=7 (9+1, 6-1)=(10,5)
Therefore the digitized coordinates are (x0, y0) = (6, 12), (x1, y1) = (7, 11),………..(x7,y7) = (10,5)
5. Apply Bresenham’s algorithm to draw a line from (16,20) and end point is (20,15)
Solution
Starting Point (x0,y0) = (16, 20) Ending Point (xn, yn) = (20, 15) such that scanning takes place from left to right
Slope m = (15-20)/ ( 20-16) = -1.25 i.e. |M|>1
Here: Δx= │20-16│ = 4 Δy= │15-20│ = 5 2Δx = 2 * 4 = 8 2Δy = 2 * 5 = 10
Now from Bresenhams Algorithm for |M|>1, and scanning from left to right
K Pk (Xk+1, Yk+1
0 8-5=3 (17,19)
1 3 + 8 – 10 = 1 (18,18)
2 1 + 8 – 10= -1 (18,17)
3 -1 + 8 = 7 (19,16)
4 7 + 8 – 10 = 5 (20, 15)
Therefore the digitized coordinates are (x0, y0) = (20,15), (x1, y1) = (21,16),………..(x15, y15) = (30,30)
6. Apply Bresenham’s algorithm to draw a line from (-2,-4) and end point is (-6,-9)
Solution
Starting Point (x0,y0) = (-2, -4) Ending Point (xn, yn) = (-6, -9) such that scanning takes place from left to right
Slope m = (-9+4)/ ( -6+2) = 1.25 i.e. |M|>1
Here: Δx= │-6+2│ = 4 Δy= │-9-4│ = 5 2Δx = 2 * 4 = 8 2Δy = 2 * 5 = 10
Now from Bresenhams Algorithm for |M|>1, and scanning from left to right
K Pk (Xk+1, Yk+1
0 8-5=3 (-3,-5)
1 3 + 8 – 10 = 1 (-4,-6)
2 1 + 8 – 10= -1 (-4,-7)
3 -1 + 8 = 7 (-5,-8)
4 7 + 8 – 10 = 5 (-6, -9)
Therefore the digitized coordinates are (x0, y0) = (-2,-4), (x1, y1) = (-3,-5),………..(x5, y5) = (-6,-9)
Since a circle is a symmetrical figure, midpoint circle generating algorithm takes this advantage of symmetry
property to plot right points for each value that the algorithm calculates.
Eight-way symmetry is used by reflecting each calculated point around each 45-degree axis.
Suppose a calculated point is (x,y), then we can obtain eight different point as:
(x,y), (x,-y), (-x,y), (-x,-y), (y,x), (-y,x), (y,-x), (-y,-x)
So using this symmetry principal, we can reduce the amount of computation.
C-Implementation
Numerical
1. Digitized the circle with radius 10. (Digitized a circle X2 + Y2 = 100 on first octant.)
Solution:
Initial Point = (Xo, Yo) = (o, r) = (0,10) and origin = (0,0)
Initial decision parameter (p0) = 1 – r = 1 – 10 = -9
From midpoint circle algorithm we have
Now, the successive decision parameter values and position along the circle path are determined as follow.
K Pk Xk+1 YK+1 (XK+1, Yk=1)
0 -9 1 10 (1,10)
1 -9*2*1+1=-6 2 10 (2,10)
2 -6 + 2*2+1=-1 3 10 (3,10)
3 -1 + 2 * 3 + 1 = 6 4 9 (4,9)
4 6 + 2 * 4 – 2 * 9 + 1 =-3 5 9 (5,9)
5 -3 + 2 * 5 + 1= 8 6 8 (6,8)
6 8+2*6–2*8+1=5 7 7 (7,7)
Hence, the digitized coordinates for circle in first octant are (x0,y0) = (0, 10), (x1,y1) =
(1,10)…..(x7,y7)=(7,7).
Note: For each calculated point we need to apply symmetry principle to obtain seven other different
coordinates one for each of the seven different octants.
Now, the successive decision parameter values and position along the circle path are determined as follow.
K Pk Xk+1 Yk+1 (Xk+1, Yk+1) at (0,0) (Xk+1, Yk+1) at (250,300)
0 -11 1 12 (1,12) (250+1, 300+12)=(251,312)
1 -11 + 2* 1 + 1 = -8 2 12 (2,12) (250+2,300+12) =(252,312)
2 -8 + 2 * 2 + 1 = -3 3 12 (3,12) (250+3,300+12) =(253,312)
3 -3 + 2 * 3 + 1=4 4 11 (4,11) (250+4,300+11) =(254,311)
4 4 + 2 * 3 – 2 * 11 + 1= -9 5 11 (5,11) (250+5,300+11) =(255,311)
5 -9 + 2 * 5 + 1 = 2 6 10 (6,10) (250+6,300+10) =(256,310)
6 2 + 2*6 – 2* 10 + 1 = -5 7 10 (7,10) (250+7,300+10) =(257,310)
7 -5 + 2 * 7 + 1 = 10 8 9 (8,9) (250+8,300+9) =(258,309)
8 10+ 16 – 18 + 1= 9 9 8 (Discard and stop
since X>Y)
Therefore the digitized coordinates are (Xo,Yo) = (250+0, 300+12) = (250,312), (X1,Y1)= (251,312)……
(X8,Y8)= (258,312)
Derive Decision parameter for midpoint circle generating algorithm taking octant y=0 to y<=x and
moving in anti-clock direction.
Note: Here we need to scan convert a circle when starting point is (r,0) and moving in anticlockwise direction.
For this we take the octant y=0 to y<=x (here, │m│>1). Hence, we step in unit interval in y direction
i.e. yK+1 = Yk +1 and find each successive X i.e. Xk or Xk-1
1. The input ellipse parameters are rx = 8 and ry = 6. Using midpoint method, rasterize
this ellipse.
Solution:
Given rx = 8 and ry= 6
For Region 1
The initial point for the ellipse on the origin (xo, yo) = (0, ry) = (0,6)
Calculation the initial decision parameter
P10 = r2y + ( r2x / 4) – ryr2x
= 62 + (82 / 4) - 6 * (8)2
= -332
From midpoint algorithm, for Region 1 we know,
If(Pk<0) then
Xk+1 = Xk + 1
Yk+1 = Yk
PK+1 = P1k + r2y + 2r2y(XK + 1)
Else (i.e. Pk >=0)
Xk+1 = Xk + 1
Yk+1 = Yk - 1
PK+1 = Pk + r2y + 2r2y(XK + 1) - 2r2x(YK + 1)
And stop when 2r2yXk+1>= 2r2xYk+1
Now successive decision parameter values and positions along the ellipse path are
calculated using midpoint method as
K P1k (Xk+1, YK=1) at (o, o) 2r2yXk+1 2r2xYk+1
-332 (1, 6) 72 768
0
-224 (2,6) 144 768
1
-44 (3,6) 216 768
2
208 (4,5) 288 640
3
-108 (5,5) 360 640
4
288 (6,4) 432 512
5
244 (7,3) 504 384
6
Since 2r2yXk+1>= 2r2xYk+1, so we stop here for region 1.
(note: (7, 3) is the starting point for region 2)
P20 = (Xk + (1/2))2ry2 + (Yk -1)2rx2 – rx2ry2 (is calculated for the point when it
enters region 2)
If(Pk<=0) then
Xk+1 = Xk + 1
Yk+1 = Yk - 1
PK+1 = pk + 2ry2(xK+1) – 2rx2(YK+1 ) + rx2
Else (i.e. Pk >0)
Xk+1 = Xk
Yk+1 = Yk -1
PK+1 = pk – 2rx2(YK+1 ) + rx2
So, the initial point for region 2 is (x0, y0) = (7, 3) and the initial decision parameter is
P20 = (Xk + (1/2))2ry2 + (Yk -1)2rx2 – rx2ry2
= 36 * (7 + 0.5)2 + 64 * (3-1)2 – 64 * 36
= -23
The remaining pixels in region two for first quadrant are than calculated as
P2k (Xk+1, YK=1) at (o,o)
K
-23 (8,2)
0
361 (8,1)
1
297 (8,0) [(rx,0) so stop]
2
So remaining points in other quadrants can be calculated using the symmetry property of
ellipse.
Filled Area Primitives
The main idea behind the 2D or 3D object filling procedure is that it provides us more realism on the object of
interest.
There are two basic approaches to area filling in raster systems.
i. One way to fill an area is to determine the overlap intervals for scan lines that crosses the area.
1. Scan Line Polygon Fill Algorithm
2. Scan Line Fill of Curve Boundary Area
ii. Another method for area filling is to start from a given interior position and point outward from this
until a specified condition is met.
1. Boundary Fill Algorithm
2. Flood Fill Algorithm
S1
In the above figure, for the scanline S1, there are four intersection points: X10, X14, X18, and X24, thus giving two
intersection Pairs: Pair1 = (X10, X14) and Pair2 = (X18, X24).
Some scan line intersections at polygon requires extra processing when the scan line passes through vertex (An
intersection point that connect two edge).
In this method, the scan line intersection points count must be even so for vertex the intersection point must be
count twice.
Figure below shows two scan line y and y1 that intersects at polygon edges.
Count = 4
Count = 5
For scan line y’, as it passes through vertex we count that intersection point twice and hence total intersection
point is even i.e. count = 4. This strategy worked as expected.
But this consideration also may create problem for some instant, like scan line y. Here total intersection point
is odd i.e. count=5. So, we need to do some additional processing to determine the correct interior points.
The difference between scan line y and scan line y' is the position of the intersecting edges relative to the scan
line.
For scan line y, the two intersecting edges sharing a vertex are on opposite sides of the scan line. But for scan
line y', the two intersecting edges are both above the scan line.
Thus for vertices that have connecting edges on opposite sides of the scan line, the intersection point must be
counted as once not twice so that we get total count of four i.e. even for scan line y.
C-Implementation
C-Implementation / Algorithm:
Assignment-3
1. Apply Bresenham’s algorithm to draw a line from (0,40) and end point is (40,0)
2. Apply Bresenham’s algorithm to draw a line between the endpoints (-2,-4) and (-6,-9)
3. Apply Midpoint circle algorithm to draw a circle of radius 9 centered at (6,7).