2. April 23, 2023 Computer Graphics 2
Scan Conversion
• Rasterization or Scan Conversion is defined as the
process of representing continuous graphic object as a
collection of discrete pixels.
– Various graphic objects are
• Point
• Line
• Rectangle, Square
• Circle, Ellipse
• Sector, Arc
• Polygons……
– Hardware, Software, Firmware Implementation
3. April 23, 2023 Computer Graphics 3
Scan Conversion
• Scan Conversion is required in order to convert vector data
to Raster format for a scan line display device
– convert each graphic object to a set of regular pixels.
– determine inside/outside areas when filling polygons.
– scan-convert curves
4. April 23, 2023 Computer Graphics 4
Scan Conversion Algorithms
1. Scan Conversion of Point
2. Scan Conversion of Line
3. Scan Conversion of Circle
4. Scan Conversion of Ellipse
5. Scan Conversion of Polygons
5. April 23, 2023 Computer Graphics 5
Scan Converting a Point
MODELLING CO-ORDINATES
• Mathematically vectors are defined in an
infinite, “real-number” Cartesian co-
ordinate system
SCREEN COORDINATES
• a.k.a device co-ordinates, pixel co-ordinates
• On display hardware we deal with finite, discrete
coordinates
• X, Y values in positive integers
• 0,0 is measured from top-left usually with +Y
pointing down
Y
X
A pixel (10, 6)
X
Y
Z
A point (-12.3,
10.3, 0)
6. April 23, 2023 Computer Graphics 6
Scan Converting a Point
• Each pixel on graphic display does not represent a
mathematical point like P(2.6,3.33). But it can be
accommodated to the nearest position by applying few
mathematical functions such as
– CEIL P (3,4)
– FLOOR P (2,3)
– GREATEST INTEGER FUNCTION P (3,3)
– ROUND P (3,3)
7. April 23, 2023 Computer Graphics 7
Scan Conversion Algorithms
1. Scan Conversion of Point
2. Scan Conversion of Line
3. Scan Conversion of Circle
4. Scan Conversion of Ellipse
8. April 23, 2023 Computer Graphics 8
Scan Converting a Line
How does a machine draw Lines?
1. Give it a start and end position.
2. Figure out which pixels to colour in between these…
– How do we do this?
– Line-Drawing Algorithms: DDA, Bresenham’s Algorithm
9. April 23, 2023 Computer Graphics 9
Scan Converting a Line
Line and its slope
– The slope of a line (m) is defined by its start and end
coordinates
– The diagram below shows some examples of lines and their
slopes
m = 0
m = -1/3
m = -1/2
m = -1
m = -2
m = -4
m = ∞
m = 1/3
m = 1/2
m = 1
m = 2
m = 4
m = 0
10. April 23, 2023 Computer Graphics 10
Line Drawing Algorithms
1. DDA Line Algorithm
2. Bresenham Line Algorithm
11. April 23, 2023 Computer Graphics 11
1. Introduction
– Digital Differential Analyser is an
an algorithm for scan-converting lines
– The original differential analyzer
was a physical machine developed by
Vannevar Bush at MIT in the 1930’s
in order to solve ordinary differential equations.
– It calculates pixel positions along a line by taking unit step
increment along one direction and calculating corresponding
coordinate position based on the rate of change of the
coordinate (Dx or Dy ) (Incremental Approach)
DDA Algorithm
12. April 23, 2023 Computer Graphics 12
2. Basic Concept
– For each part of the line the following holds true:
– If Dx = 1 i.e. 1 pixel then …
– i.e. for each pixel we move right (along the x axis), we need to
move down (along the y-axis) by m pixels.
– In pixels, the gradient represents how many pixels we step
upwards (Dy) for every step to the right (Dx)
DDA Algorithm
m
y
D
x
m
y
x
y
m D
D
D
D
13. April 23, 2023 Computer Graphics 13
3. Derivation
Assume that 0<m<1, Dx > 0 and Dy >0
For a point P(xi, yi ) on a line we know that
yi = mxi + b
At next position P(xi+1, yi+1 )
yi+1 = mxi+1+ b
Having unit step increment along x-axis means xi+1 = xi + 1
Therefore yi+1 = m(xi + 1)+ b
= mxi + m + b
= mxi + b + m
= yi + m
DDA Algorithm
14. April 23, 2023 Computer Graphics 14
DDA Algorithm
4. Simple Algorithm
1. Input (x1,y1) and (x2,y2)
2. Let x = x1; y = y1;
m = (y2-y1)/(x2-x1);
3. Draw pixel (x, y)
4. WHILE (x < x2) //i.e. we reached the second
endpoint
5. {
x = x + 1; //step right by one pixel
y = y + m; //step down by m pixels
Draw pixel (ROUND(x), ROUND(y));
}
15. April 23, 2023 Computer Graphics 15
DDA ALGORITHM
Sample at unit x:
1
1
D
k
k
k
x
x
x
x
Corresponding y pos.:
)
1
(
1
D
D
m
y
x
m
y
y
y
y
k
k
k
k
5. Example
16. April 23, 2023 Computer Graphics 16
DDA ALGORITHM
Sample at unit x:
1
1
D
k
k
k
x
x
x
x
Corresponding y pos.:
)
1
(
1
D
D
m
y
x
m
y
y
y
y
k
k
k
k
5. Example
17. April 23, 2023 Computer Graphics 17
DDA ALGORITHM
Sample at unit x:
1
1
D
k
k
k
x
x
x
x
Corresponding y pos.:
)
1
(
1
D
D
m
y
x
m
y
y
y
y
k
k
k
k
5. Example
18. April 23, 2023 Computer Graphics 18
DDA ALGORITHM
Sample at unit x:
1
1
D
k
k
k
x
x
x
x
Corresponding y pos.:
)
1
(
1
D
D
m
y
x
m
y
y
y
y
k
k
k
k
5. Example
19. April 23, 2023 Computer Graphics 19
DDA ALGORITHM
Sample at unit x:
1
1
D
k
k
k
x
x
x
x
Corresponding y pos.:
)
1
(
1
D
D
m
y
x
m
y
y
y
y
k
k
k
k
5. Example
20. April 23, 2023 Computer Graphics 20
DDA ALGORITHM
Sample at unit x:
1
1
D
k
k
k
x
x
x
x
Corresponding y pos.:
)
1
(
1
D
D
m
y
x
m
y
y
y
y
k
k
k
k
5. Example
21. April 23, 2023 Computer Graphics 21
DDA ALGORITHM
Consider endpoints:
P1(0,0), P2(7,4)
Sample at unit x:
1
1
D
k
k
k
x
x
x
x
Corresponding y pos.:
)
1
(
1
D
D
m
y
x
m
y
y
y
y
k
k
k
k
5. Example
22. April 23, 2023 Computer Graphics 22
DDA ALGORITHM
6. Exercise
1. Consider endpoints:
P1(0,0), P2(6, 4)
Calculate the points that made up the line P1P2
2. Now, consider endpoints:
P3(0,0), P4(4, 6)
Calculate the points that made up the line P3P4
What happened with P3P4??????
23. April 23, 2023 Computer Graphics 23
DDA Algorithm
7. Y-Version (|m|>1)
– If the slope is very steep (i.e. Dy is
much greater than Dx) then there’s a
problem. i.e. m is much greater
than 1
– This is because we only draw one
pixel for each x value (each time we
step right)
– Solution: if the slope is too big, step
for y instead
24. April 23, 2023 Computer Graphics 24
8. Generic solution
– Modify Basic idea
– Take unit step increment along direction having more rate of change, and
compute the coordinates for the other position to maximize the number of
points to be computed along the path of line and avoid discontinuities
y
m
x
m
x
m
y
D
D
D
D
1
0
1
m
0
:
1
:
1
1
m
y
y
x
x
i
i
i
i
or
m
1
1
:
1
:
1
1
m
x
x
y
y
i
i
i
i
DDA Algorithm
Why?
discontinuity !!
25. April 23, 2023 Computer Graphics 25
– What is m<0 ?
– We must see the sign of Dx and Dy
– Or can we have simple solution???????
DDA Algorithm - Generic solution
Vario
us
Cases
Dx>0
Dy>0
Dx>0
Dy<0
Dx<0
Dy>0
Dx<0
Dy<0
|m|<1
xi+1 = xi + 1
yi+1 = yi +
m
xi+1 = xi + 1
yi+1 = yi - m
xi+1 = xi - 1
yi+1 = yi +
m
xi+1 = xi - 1
yi+1 = yi – m
(or swap
points)
|m|>1
xi+1 = xi +
1/m
yi+1 = yi + 1
xi+1 = xi +
1/m
yi+1 = yi - 1
xi+1 = xi -
1/m
yi+1 = yi +
xi+1 = xi - 1/m
yi+1 = yi – 1
(or swap
26. April 23, 2023 Computer Graphics 26
Incremental DDA Algorithm
1. Input (x1,y1) and (x2,y2)
2. Compute
dx = x2-x1 and dy = y2-y1
m = dy/dx
3. If abs(dx)>abs(dy)
step = abs(dx)
else
step = abs(dy)
4. Compute xinc = dx/step
yinc = dy/step
5. Initialize x = x1 and y = y1
6. Draw pixel (x, y)
7. For k = 1 to steps do //we reach the other endpoint
{
x = x + xinc;
y = y + yinc;
Draw pixel (ROUND(x), ROUND(y));
}
27. April 23, 2023 Computer Graphics 27
8. Limitations
– Rounding integers takes time
– Variables y and m must be a real or fractional binary because
the slope is a fraction
– Real variables have limited precision, summing an inexact
slope m repetitively introduces a cumulative error buildup
DDA Algorithm
28. April 23, 2023 Computer Graphics 28
DDA Algorithm
Rounding Error
– Note that the actual pixel position is
actually stored as a REAL number
(in C/C++/java a float or a double)
– But we Round Off to the nearest
whole number just before we draw
the pixel.
– e.g. if m=.333 …
1.0
2.0
3.0
4.0
X Y Rounded { x, y }
0.33
0.66
0.99
1.32
{ 1, 0 }
{ 2, 1 }
{ 3, 1 }
{ 4, 1 }
29. April 23, 2023 Computer Graphics 29
Line Drawing Algorithms
1. DDA Line Algorithm
2. Bresenham Line Algorithm
30. April 23, 2023 Computer Graphics 30
1. Introduction
– One disadvantage of DDA is the ROUNDing part which can
be expensive
– Developed by Jack Bresenham
at IBM in the early 1960s
– One of the earliest algorithms in
computer graphics
– The Algorithm is based on essentially
the same principles but is completely
based on integer variables
Bresenham’s Line Algorithm
31. April 23, 2023 Computer Graphics 31
Bresenham’s Line Algorithm
2. Basic Concept
– Find the closest integer coordinates to the actual line path using
only integer arithmetic
– Candidate for the next pixel position
– No division, Efficient comparison, No floating point operations
Specified
Line Path
1
0
m
Specified
Line Path
32. April 23, 2023 Computer Graphics 32
Bresenham’s Line Algorithm
3. Derivation
– The algorithm is derived for a line having slope 0< m < 1 in
the first quadrant.
– Pixel position along the line are plotted by taking unit step
increments along the x-direction and determining y-
coordinate value of the nearest pixel to the line at each step.
– Can be generalized for all the cases
33. April 23, 2023 Computer Graphics 33
Bresenham’s Line Algorithm
– Let us assume that P(xk, yk ) is the currently plotted pixel.
Q(xk+1, yk+1 ) (xk+1, y ) is the next point along the actual
path of line. We need to decide next pixel to be plotted from
two candidate positions Q1(xk+1, yk ) or Q2(xk+1, yk+1)
l
k
x
x
m l
k
,
,
1
0
1
k
x
k
y
1
k
y
y
2
d
1
d
b
mx
y
k
x 1
k
x 2
k
x
2
k
y
1
k
y
k
y P
P
Q
1
Q
2
Q
1
Q
2
Q
Q
34. April 23, 2023 Computer Graphics 34
Bresenham’s Line Algorithm
Given the equation of line
y = mx + b
Thus actual value of y at x = xk+1 is given by
y = mxk+1 + b = m(xk + 1) + b
Let d1 = | QQ1|= distance of yk from actual value of y
= y – yk = m(xk + 1) + b – yk
d2 = | QQ2| = distance of actual value of y from yk +1
= yk+1 – y = (yk + 1)– [m(xk + 1) + b]
The difference between these 2 separations is
d1-d2 = 2m(xk + 1) + 2b –yk– (yk+ 1)
= 2m(xk + 1) – 2 yk + 2b – 1
35. April 23, 2023 Computer Graphics 35
Bresenham’s Line Algorithm
we can define a decision parameter pk for the kth step to by
simplifying above equation such that the sign of Pk is the same as
the sign of d1-d2, but involves only integer calculations.
Define Pk = Δx ( d1-d2)
)
1
2
2
)
1
(
2
(
D
D
D
b
y
x
x
y
x k
k
)
1
2
(
2
2
2
D
D
D
D
b
x
y
y
x
x
y k
k
constant
a
1
2
2
where
2
2
)
b
Δx(
Δy
c
k
k c
y
x
x
y
D
D
)
1
2
2
)
1
(
2
(
D
b
y
x
m
x k
k
36. April 23, 2023 Computer Graphics 36
Bresenham’s Line Algorithm
If pk< 0
( d1-d2) <0
distance d1 is less than d2
yk is closer to line-path
Hence Q1(xk+1, yk ) is the better choice
else
Q2(xk+1, yk+1) is the better choice
Thus if the parameter pk is negative lower pixel is plotted else
upper pixel is plotted
37. April 23, 2023 Computer Graphics 37
D
D
D
D
D
D
D
D
D
D
D
otherwise
x
y
p
p
if
y
p
p
otherwise
y
p
if
y
y
y
y
x
y
p
p
y
y
x
x
x
y
p
p
p
p
c
y
x
x
y
p
c
y
x
x
y
p
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
k
2
2
0
2
1
0
)
(
2
2
)
(
2
)
(
2
from
subtract
.
2
.
2
1
k
k
Replacing
.
2
.
2
1
1
1
1
1
1
1
1
1
1
1
Bresenham’s Line Algorithm
To put pk in the iterative form, we derived that
38. April 23, 2023 Computer Graphics 38
Bresenham’s Line Algorithm
The first parameter p0 is directly computed as:
p0 = 2 Δy.x0 - 2 Δx.y0 + c
= 2 Δy.x0 - 2 Δx.y0 + 2 Δy + Δx (2b-1)
Since (x0,y0) satisfies the line equation , we also have
y0 = Δy/ Δx * x0 + b
b = y0 - Δy/ Δx * x0
Combining the above 2 equations , we will have
p0 = 2Δy – Δx
The constants 2Δy, 2Δy – Δx and 2Δy-2Δx are calculated once.
39. April 23, 2023 Computer Graphics 39
Bresenham’s Line Algorithm
STEPS FOR BRESENHAM’S LINE DRAWING ALGORITHM (for |m| < 1.0)
1. Input the two line end-points (x0, y0) and (x1, y1)
2. Plot the point (x0, y0)
3. Compute Δx = x1 - x0 , Δy = y1 - y0
4. Initialize p0 = 2Δy – Δx
5. At each xk along the line, starting at k = 0, perform the
following test.
If pk < 0
the next point to plot is (xk+1, yk)
pk = pk +2Δy
else
the next point to plot is (xk+1, yk+1)
pk = pk +2Δy – 2Δx
6. Repeat step 5 (Δx – 1) times
7. Plot the point (x1, y1)
8. Exit
41. April 23, 2023 Computer Graphics 41
Bresenham’s Line Algorithm
4. General solution
– The algorithm can be generalized for all slopes
Cases/
slope
Case I
Dx>0 , Dy>0
Case II
Dx>0, Dy<0
Case III
Dx<0, Dy>0
Case IV
Dx<0, Dy<0
|m|<1
p = 2dy-dx
for x = x1 to x2
if p<0
p = p+ 2dy
else { y=y+1
p = p+2(dy-dx)}
p = 2dy-dx
for x = x1 to x2
if p<0
p = p+ 2dy
else { y=y-1
p = p+2(dy-dx)}
swap points
Case II
swap points
Case I
|m|>1
p = 2dx-dy
for y= y1 to y2
if p<0
p = p+ 2dx
else { x=x+1
p = p+2(dx-dy)}
p = 2dx-dy
for y= y2 to y1
if p<0
p = p+ 2dx
else { x=x+1
p = p+2(dx-dy)}
swap points
Case II
swap points
Case I
42. April 23, 2023 Computer Graphics 42
Bresenham’s Line Algorithm
5. Exercise
Calculate pixel positions that made up the line connecting endpoints:
(12, 10) and (17, 14).
2. Dx = ?, Dy =?, 2Dy = ?, 2Dy – 2Dx =?
1. (x0, y0) = ?
3. p0 = 2Dy – Dx =?
k pk (xk+1, yk+1)
43. April 23, 2023 Computer Graphics 43
Bresenham’s Line Algorithm
5. Exercise
Calculate pixel positions that made up the line connecting endpoints:
(12, 10) and (17, 14).
2. Dx = 5, Dy =4, 2Dy = 8, 2Dy – 2Dx =-2
1. (x0, y0) = (12,10)
3. p0 = 2Dy – Dx =3
k pk (xk+1, yk+1)
0 3
1
2
44. April 23, 2023 Computer Graphics 44
Bresenham’s Line Algorithm
5. Exercise
Calculate pixel positions that made up the line connecting endpoints:
(12, 10) and (17, 14).
2. Dx = 5, Dy =4, 2Dy = 8, 2Dy – 2Dx =-2
1. (x0, y0) = (12,10)
3. p0 = 2Dy – Dx =3
k pk (xk+1, yk+1)
0 3 (13, 11)
1 1 (14, 12)
2 -1 (15, 12)
3 7 (16, 13)
4 5 (17, 14)
45. April 23, 2023 Computer Graphics 45
Bresenham’s Line Algorithm
17
16
15
14
13
12
11
10
18
29
27
26
25
24
23
22
21
20 28 30
k pk (xk+1,yk+1)
5. Exercise: Trace for (20,10) to (30,18)