Computer Graphics-Output Primitives
Computer Graphics-Output Primitives
(7, 5)
(2, 2)
Line equation
The slope intercept form of a line is
y=m.x+b
Where m=y2-y1/x2-x1
b=y-m.x
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
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
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
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
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:
m
(x
k1
)b
y
k
d(
y1
upper
k
)y
y
k
1
m
(
x
k1
)b
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
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
◦ Δx: 10
◦ Δy: 8
◦ 2Δy: 16
◦ 2Δy - 2Δx: -4
Calculate the initial decision parameter p0:
◦ p0 = 2Δy – Δx = 6
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)