0% found this document useful (0 votes)
4 views

Week 2 Line Drawing Algorithms

The document discusses various line drawing algorithms in computer graphics, focusing on the mathematical principles behind line segments defined by endpoints and slopes. It covers the Digital Differential Analyzer (DDA) algorithm and Bresenham's line drawing algorithm, detailing their steps, advantages, and limitations. The document also includes examples and exercises to illustrate the application of these algorithms.

Uploaded by

pawanjoshi9810
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week 2 Line Drawing Algorithms

The document discusses various line drawing algorithms in computer graphics, focusing on the mathematical principles behind line segments defined by endpoints and slopes. It covers the Digital Differential Analyzer (DDA) algorithm and Bresenham's line drawing algorithm, detailing their steps, advantages, and limitations. The document also includes examples and exercises to illustrate the application of these algorithms.

Uploaded by

pawanjoshi9810
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Line Drawing Algorithm

Line Drawing Algorithms

A line in Computer graphics typically refers to line


segment, which is a portion of straight line that
extends indefinitely in opposite direction. It is
defined by its two end points & the slope intercept
equation for a line:
y = mx + b (1)
where, m = Slope of the line
b = the y intercept of a line
The two endpoints of a line segment are specified at
positions (x1,y1) and (x2,y2).
y
P2(x2,y2)

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

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.
DDA Algorithm
• The digital differential analyzer (DDA) algorithm is an
incremental scan-conversion method. Such an approach
is characterized by performing calculations at each step
using results from the preceding step.
• Suppose, at step i we have calculated (xi, yi) to be a point
on the line. Since the next point (xi+1,yi+1) should satisfy
Δy/Δx= m where Δy= yi+1 – yi & Δx = xi+1 – xi.
• We have, yi+1 = yi + mΔx
yi+1 = yi + Δy (1)
Or xi+1 = xi + Δy/m (2)
Algorithm:
(x1,y1) (x2,y2) are the end points and dx, dy are the float variables.
(i) If abs(x2-x1) > abs(y2-y1) then
length = abs(x2-x1)
else
length = abs(y2-y1)
endif
(ii) dx = (x2-x1)/length
dy = (y2-y1)/length
(iii) x = x1 + 0.5, y = y1 + 0.5
(iv) i=0
(v) Plot (trunc(x), trunc(y))
(vi) x = x + dx, y = y + dy
(vii) i = i + 1
(viii) If i < length then go to step (v)
(ix) Stop
Example 2 Scan convert a line having end points (3,2) &
(4,7) using DDA.
Solution: x2 - x1 = 4-3 = 1
y2 - y1 = 7-2 = 5
As, abs(x2-x1) < abs(y2-y1) then
length = y2-y1 = 5
dx = (x2-x1)/ length = 1/5 = .2
dy = (y2-y1)/ length = 5/5 = 1
x1 y1 x2 y2 L dx dy i x y Result Plot
3 2 4 7 5 .2 1 0 3.5 2.5 3.5, 2.5 3,2
1 3.7 3.5 3.7,3.5 3,3
2 3.9 4.5 3.9,4.5 3,4
3 4.1 5.5 4.1,5.5 4,5
4 4.3 6.5 4.3,6.5 4,6
5 4.5 7.5 4.5,7.5 4,7
Limitations of DDA:
(1) The rounding operation & floating point arithmetic
are time consuming procedures.
(2) The accumulation of round-off error in successive
addition of floating point increment can cause the
calculated pixel position to drift away from the
true line path for long line segment.
Exercise
• Implement the DDA algorithm to draw a line
from (0,0) to (6,6).
Bresenham’s Line
Drawing Algorithm
Types of Bresenham’s Line Drawing
Algorithm
• First Octant
• Integer
• Generalized
(if you can’t do all three do generalized
algorithm only)
Bresenham's Line Drawing Algorithm
• Originally developed for digital plotters, equally
suited for CRT raster devices.
• Meant for first octant.
• Algorithm always increment by one unit in either x or
y direction, depending on the slope of the line.
• The increment in the other variable, either zero or
one, is determined by examining the distance
between the actual line and the nearest grid
location. This distance is called as error.
Bresenham's Line Drawing Algorithm
(contd.)
• Initialize error term e = -1/2
• Error term calculated as e = e + m, therefore
initially e = -1/2 + m
• If error term exceeds 0, reinitialize the error
term as e = e – 1.
Bresenham's Line Drawing Algorithm
(contd.)
• Only the sign of error term is examined.
• For lines falling in first octant, x is incremented in all
cases.
• Decision is to be taken for y.
• If e > 0(+ve), y is incremented, as line passes above
the mid point and is closer to the next higher vertical
pixel i.e. (x+1,y+1).
• If e < 0(-ve), y remains the same, as line passes below
the mid point and is closer to the next horizontal
level pixel i.e. (x+1,y).
• If e = 0, y remains the same, as line passes through
the mid point and next horizontal level pixel i.e.
(x+1,y) is chosen.
error>0

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-½

(begin the main loop)


(iv) for i=1 to ∆x
setpixel (x,y)
while (e>0)
y=y+1
e=e-1
end while
x=x+1
e=e+m
(v) next i
Dry Run - To draw a line from (0,0) to (3,3)
Initial calculation
x1 = 0, y1 = 0, ∆x = 3, ∆y2 = 3, m = 1, e = 1-1/2 = 1/2

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

Now translation of equations involving e:


1. e=-1/2+ ∆y/∆x into ebar=2∆y-∆x
2. e=e+∆y/∆x into ebar=ebar+2∆y
3. e=e-1 into ebar=ebar-
2∆x
Bresenham’s Integer Line Drawing Algorithm for the first octant

(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

(begin the main loop)


(iv) for i=1 to ∆x
setpixel (x,y)
while (ebar>0)
y=y+1
ebar=ebar-2∆x
end while
x=x+1
ebar=ebar+2∆y
(v) next i
Example: Find the intermediate pixels
between (5,4) and (12,7)
i Setpixel(x,y) ebar X,Y

5,4 -1
Initial Calculations
x1=5, y1=4 1 5,4 5 6,4

∆x=7, ∆y=3, 2 6,4 -9 6,5


ebar=2∆y- ∆x = -1 -3 7,5
3 7,5 3 8,5
4 8,5 -11 8,6
-5 9,6
5 9,6 1 10,6
6 10,6 -13 10,7
-7 11,7
7 11,7 -1 12,7
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
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.

x=0, y=0, ∆x=8, ∆y=4


s1=-1,s2=-1
interchange=0, ebar=0
i setpixel ebar x y
0 0 0
1 (0,0)
8 -1 0
2 (-1,0)
-8 -1 -1
0 -2 -1
3 (-2,-1)
8 -3 -1
4 (-3,-1)
-8 -3 -2
0 -4 -2
5 (-4,-2)
8 -5 -2
6 (-5,-2)
-8 -5 -3
0 -6 -3
7 (-6,-3)
8 -7 -3
8 (-7,-3)
-8 -7 -4
0 -8 -4

You might also like