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).
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 (x1,y1) and (x2,y2), assumed not equal.
all variables are assumed integer
(ii) initialize variables
x=x1, y=y1, ∆x=x2-x1, ∆y=y2-y1
(iii) initialize e
ebar = 2∆y-∆x
i setpixel ebar x y
__________________________________________________
3 0 0
1 (0,0)
-3 1 0
3 1 1
2 (1,1)
-3 2 1
3 2 2
3 (2,2)
-3 3 2
3 3 3
General Bresenham’s Line
Drawing Algorithm
General Bresenham’s Line Drawing
Algorithm
• Applicable for lines lying in all octants.
• If |slope|>1, y is incremented by 1 and error
criteria is used to determine when to
increment x.
• Whether x or y is incremented depends on the
quadrant.
General Bresenham’s Line Drawing Algorithm for all quadrants
(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
setpixel(x,y)
while(ebar > 0 )
if Interchnage = 1 then
x = x + s1
else
y = y+ s2
end if
ebar = ebar – 2* ∆x
end while
if Interchange = 1 then
y = y+s2
else
x= x+s1
endif
ebar = ebar +2* ∆y
(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.