Week 2 Line Drawing Algorithms
Week 2 Line Drawing Algorithms
P1(x1,y1)
b
0
x
We can determine the value for slope m & b intercept as
m = y2-y1/x2-x1 (2)
And, b = y1 – mx1 (3)
For a given x interval Δx along a line, we can
compute the corresponding y interval Δy from
Δy = m Δx (4)
Similarly, we can obtain x interval Δx by Δy:
Δx = Δy/m (5)
• If |m|=1, Δx = Δy. In each case, a smooth line with
slope m is generated between the specific endpoints.
• If |m|<1, then for every integer value of x between
and excluding x1 and x2, calculate the corresponding
value of y using equation Δy = m Δx & scan convert
(x,y).
• If |m|>1, then for every integer value of y between
and excluding y1 and y2, calculate the corresponding
value of x using equation Δx = Δy/m & scan convert
(x,y).
Example 1 The endpoints of line are(0,0) & (6,18).
Compute each value of y as x steps from 0 to 6 and
plot the result.
Solution : Equation of line is y= mx +b
m = y2-y1/x2-x1= 18-0/6-0 = 3
Next the y intercept b is found by plugging y1 & x1 into
the equation y = 3x + b,
0 = 3(0) + b. Therefore, b=0, so the equation for the
line is y= 3x. y X
0 0
3 1
6 2
9 3
12 4
15 5
18 6
While this approach is mathematically sound, it involves
floating-point computation (multiplication & addition)
in every step that uses the line equation since m & b
are generally real numbers. The challenge is to find a
way to achieve the same goal as quickly as possible.
The DDA Algorithm
• The digital differential analyzer
(DDA) algorithm takes an
incremental approach in order
to speed up scan conversion
error<=0
Bresenham's Line Rasterization algorithm for the first octant
(i) The line end points are (x1,y1) and (x2,y2), assumed not equal.
x, y, ∆x, ∆y are assumed integer; e is real
(ii) initialize variables
x=x1, y=y1, ∆x=x2-x1, ∆y=y2-y1, m=∆y/∆x
(iii) initialize e
e=m-½
i setpixel e x y
___________________________________________
1/2 0 0
1 (0,0)
-1/2 0 1
1/2 1 1
2 (1,1)
-1/2 1 2
1/2 2 2
3 (2,2)
-1/2 2 3
1/2 3 3
Exercise
• Scan convert a line from (1,1) &(8,5) with
0<m<1 using Bresenham’s Line Algorithm.
Bresenham’s Integer Algorithm
BRESENHAM’S INTEGER LINE DRAWING
ALGORITHM
• Bresenham’s Previous line drawing algorithm
uses floating point arithmetic to calculate
slope of the line and error term.
• Bresenham’s Integer line drawing algorithm
overcomes this limitation by using integer
arithmetic. (to speed up the Scan conversion
of points in C.G, and to efficiently implement
in hardware and firmware)
• Meant for Ist Octant.
BRESENHAM’S INTEGER LINE DRAWING
ALGORITHM contd.
Sign of error term considered again, and simple
transformation ebar = 2e∆x is used, thus
e = ebar/2∆x
(i) The line end points are (5,4) and (12,7), assumed not equal.
all variables are assumed integer
(ii) initialize variables
x=5, y=4, x2=12,y2=7
∆x=x2-x1=7, ∆y=y2-y1=3
(iii) initialize e
ebar = 2∆y-∆x=-1
5,4 -1
Initial Calculations
x1=5, y1=4 1 5,4 5 6,4
(i) The line end points are (x1,y1) and (x2,y2), assumed not equal.
all variables are assumed integer ;
(the Sign function returns -1,0,1 as its argument is <0,=0 or >0)
(ii) initialize variables
x=x1, y=y1, ∆x=abs(x2-x1), ∆y=abs(y2-y1), s1=sign(x2-x1),
s2=sign(y2-y1)
(iii) Interchange ∆x and ∆y depending on the slope of the line
if ∆y > ∆x then
Temp = ∆x , ∆x = ∆y, ∆y = Temp
Interchange = 1
else
Interchange = 0
end if
ebar = 2 * ∆y - ∆x
(main loop)
(iv) for i = 1 to ∆x
i Setpixel ebar x y
setpixel(x,y) (x,y)
while(ebar > 0 )
0 0 0
if Interchnage = 1 then
x = x + s1 1 0,0 8 -1 0
else 2 -1,0 -8 -1 -1
y = y+ s2 0 -2 -1
end if 3 -2,-1 8 -3 -1
ebar = ebar – 2* ∆x 4 -3,-1 -8 -3 -2
end while
0 -4 -2
5 -4,-2 8 -5 -2
if Interchange = 1 then
6 -5,-2 -8 -5 -3
y = y+s2
else 0 -6 -3
x= x+s1 7 -6,-3 8 -7 -3
endif 8 -7,-3 -8 -7 -4
ebar = ebar +2* ∆y 0 -8 -4
(v) next i
Consider a line from (0,0) to (-8,-4), find out
the pixels to be chosen to draw the line
using General Bresenham’s line drawing
algorithm.