The Problem Of Scan Conversion
A line segment in a scene is defined by the
coordinate positions of the line end-points
(7, 5)
(2, 2)
B.Tulasi, Dept Of CS,Christ University 1
Bangalore
The Problem
When drawn on a pixel based display . Which
pixel has to be chosen?
B.Tulasi, Dept Of CS,Christ University 2
Bangalore
Points to be considered
Line should not be jarred
It should be fast
Line equation
The slope intercept form of a line is
y=m.x+b
Where m=y2-y1/x2-x1
b=y-m.x
B.Tulasi, Dept Of CS,Christ University 3
Bangalore
Lines & Slopes
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=∞
m = -4 m=4
m = -2 m=2
m = -1 m=1
m = -1/2 m = 1/2
m = -1/ 3 m = 1/3
m=0 m=0
B.Tulasi, Dept Of CS,Christ University 4
Bangalore
Let’s consider the following example:
y
(7, 5)
5
2
(2, 2)
x
2 7
B.Tulasi, Dept Of CS,Christ University 5
Bangalore
y
(7, 5) First work out m and b:
5
52 3
m
2
72 5
(2, 2)
3 4
b 2 2
2 3 4 5 6 7
x 5 5
Now for each x value work out the y value:
3 4 3 3 4 1
y (3) 3 2 y (4) 4 3
5 5 5 5 5 5
3 4 4 3 4 2
y (5) 5 3 y (6) 6 4
5 5 5 5 5 5
B.Tulasi, Dept Of CS,Christ University
Bangalore
6
Round off the results.
3
7 y (3) 2 3
6 5
5 1
4
y (4) 3 3
5
3
4
2 y (5) 3 4
1 5
0 2
y ( 6) 4 4
0 1 2 3 4 5 6 7 8 5
B.Tulasi, Dept Of CS,Christ University 7
Bangalore
The approach is slow
Multiplication of m,x
Rounding of y value
Alternate solution
x=y-b/m
B.Tulasi, Dept Of CS,Christ University 8
Bangalore
X(3)=2,x(4)=5
7
6
5
4
3
2
1
0
0 1 2
B.Tulasi, Dept Of CS,Christ University
3 4 5 6 7 8 9
Bangalore
Ifthe slope of a line is between -1 and 1 then we
work out the y coordinates for a line based on it’s
unit x coordinates
Otherwise we do the opposite – x coordinates are
computed based on unit y coordinates
m = -4 m=∞
m=4
m = -2 m=2
m = -1 m=1
m = -1 / 2
m = 1/ 2
m = -1/3 m = 1/ 3
B.Tulasi, Dept Of CS,Christ University 10
m=0 Bangalore m=0
The DDA Algorithm
The digital differential
analyzer (DDA) algorithm
takes an incremental
approach in order to speed
up scan conversion
Simply calculate yk+1 based The original differential
on yk analyzer was a physical
machine developed by
Vannevar Bush at MIT in
the 1930’s in order to
solve ordinary differential
equations.
B.Tulasi, Dept Of CS,Christ University 11
Bangalore
Consider the list of points that we determined
for the line in our previous example:
(2, 2), (3, 23/ ), (4, 31/ ), (5, 34/ ), (6, 42/ ),
5 5 5 5
(7, 5)
Notice that as the x coordinates go up by one,
the y coordinates simply go up by the slope of
the line
This is the key insight in the DDA algorithm
B.Tulasi, Dept Of CS,Christ University 12
Bangalore
When the slope of the line is between -1 and 1
begin at the first point in the line and, by
incrementing the x coordinate by 1, calculate the
corresponding y coordinates as follows:
yk 1 yk m
When the slope is outside these limits, increment
the y coordinate by 1 and calculate the
corresponding x coordinates as follows:
1
xk 1 xk
m
B.Tulasi, Dept Of CS,Christ University 13
Bangalore
The DDA Algorithm (cont…)
Again the values calculated by the equations
used by the DDA algorithm must be rounded to
match pixel values
(xk+1, round(yk+m)) (round(xk+ 1/m), yk+1)
(xk, yk) (xk+ 1/m, yk+1)
(xk+1, yk+m) (xk, yk)
(xk, round(yk)) (round(xk), yk)
B.Tulasi, Dept Of CS,Christ University 14
Bangalore
Try out the following
◦ A(2,2) B(7,5)
◦ C(3,2),D(2,7)
B.Tulasi, Dept Of CS,Christ University 15
Bangalore
The DDA Algorithm Summary
The DDA algorithm is much faster than
previous attempt
◦ In particular, there are no longer any multiplications
involved
However, there are still two big issues:
◦ Accumulation of round-off errors can make the
pixelated line drift away from what was intended.
◦ The rounding operations and floating point
arithmetic involved are time consuming.
B.Tulasi, Dept Of CS,Christ University 16
Bangalore
The Bresenham Line Algorithm
The Bresenham algorithm is
another incremental scan
conversion algorithm
The big advantage of this
algorithm is that it uses only
integer calculations
Jack Bresenham
worked for 27 years at
IBM before entering
academia. Bresenham
developed his famous
algorithms at IBM in
the early 1960s
B.Tulasi, Dept Of CS,Christ University 17
Bangalore
The Big Idea
Move across the x axis in unit intervals and at
each step choose between two different y
coordinates
For example, from
5
(xk+1, yk+1)
position (2, 3) we
4
have to choose
(xk, yk) between (3, 3) and
3 (3, 4)
(xk+1, yk)
2
We would like the
point that is closer
2 3 4 5
to the original line
B.Tulasi, Dept Of CS,Christ University
Bangalore
18
Deriving The Bresenham Line
Algorithm
At sample position x +1
k y k+1
the vertical separations dupper
from the mathematical y
dlower
line are labelled dupper yk
and dlower xk+1
The y coordinate on the mathematical line
at xk+1 is:
y m( xk 1) b
B.Tulasi, Dept Of CS,Christ University 19
Bangalore
So, dupper and dlower are given as follows:
d
yy
lowerk
and:
m
(x
k1
)b
y
k
d(
y1
upper
k
)y
y
k
1
m
(
x
k1
)b
We can use these to make a simple decision
about which pixel is closer to the mathematical
line
B.Tulasi, Dept Of CS,Christ University 20
Bangalore
Thissimple decision is based on the difference
between the two pixel positions:
d lower d upper 2m( xk 1) 2 yk 2b 1
Let’s substitute m with ∆y/∆x where ∆x and
∆y are the differences between the end-points:
y
x(d lower d upper ) x(2 ( xk 1) 2 yk 2b 1)
x
2y xk 2x yk 2y x(2b 1)
2y xk 2x yk c
B.Tulasi, Dept Of CS,Christ University 21
Bangalore
So,a decision parameter pk for the kth step
along a line is given by:
pk x(d lower d upper )
2y xk 2x yk c
The sign of the decision parameter pk is the
same as that of dlower – dupper
If pk is negative, then we choose the lower
pixel, otherwise we choose the upper pixel
B.Tulasi, Dept Of CS,Christ University 22
Bangalore
Remember coordinate changes occur along the
x axis in unit steps so we can do everything
with integer calculations
At step k+1 the decision parameter is given as:
pk 1 2y xk 1 2x yk 1 c
Subtracting pk from this we get:
pk 1 pk 2y ( xk 1 xk ) 2x( yk 1 yk )
B.Tulasi, Dept Of CS,Christ University 23
Bangalore
But, xk+1 is the same as xk+1 so:
pk 1 pk 2y 2x( yk 1 yk )
where yk+1 - yk is either 0 or 1 depending on
the sign of pk
The first decision parameter p0 is evaluated at
(x0, y0) is given as:
p0 2y x
B.Tulasi, Dept Of CS,Christ University 24
Bangalore
The Bresenham Line Algorithm
BRESENHAM’S LINE DRAWING ALGORITHM
(for |m| < 1.0)
1. Input the two line end-points, storing the left end-
point in (x0, y0)
2. Plot the point (x0, y0)
3. Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx)
and get the first value for the decision parameter
as: p0 2y x
4. At each xk along the line, starting at k = 0, perform
the following test. If pk < 0, the next point to plot is
pk 1 pk 2y
B.Tulasi, Dept Of CS,Christ University 25
Bangalore
Otherwise, the next point to plot is (xk+1, yk+1) and:
pk 1 pk 2y 2x
5. Repeat step 4 (Δx – 1) times
ACHTUNG! The algorithm and derivation above
assumes slopes are less than 1. for other slopes
we need to adjust the algorithm slightly
B.Tulasi, Dept Of CS,Christ University 26
Bangalore
Bresenham Example
Plot the line from (20, 10) to (30, 18)
First off calculate all of the constants:
◦ Δx: 10
◦ Δy: 8
◦ 2Δy: 16
◦ 2Δy - 2Δx: -4
Calculate the initial decision parameter p0:
◦ p0 = 2Δy – Δx = 6
B.Tulasi, Dept Of CS,Christ University 27
Bangalore
Bresenham Example (cont…)
18 k pk (xk+1,yk+1)
17 0
16 1
2
15
3
14
4
13 5
12 6
11 7
8
10
9
20 21 22 23 24 25 26 B.Tulasi,
27 Dept 28 Of CS,Christ
29 30 University 28
Bangalore
Bresenham Exercise
Go through the steps of the Bresenham line
drawing algorithm for a line going from (21,12)
to (29,16)
B.Tulasi, Dept Of CS,Christ University 29
Bangalore
Bresenham Line Algorithm Summary
The Bresenham line algorithm has the
following advantages:
◦ An fast incremental algorithm
◦ Uses only integer calculations
Comparing this to the DDA algorithm, DDA has
the following problems:
◦ Accumulation of round-off errors can make the
pixelated line drift away from what was intended
◦ The rounding operations and floating point
arithmetic involved are time consuming
B.Tulasi, Dept Of CS,Christ University 30
Bangalore