Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Complete-Week 2 Line Drawing Algorithms

The document discusses line drawing algorithms in computer graphics, focusing on the challenges of scan conversion on pixel-based displays. It introduces the Digital Differential Analyzer (DDA) and Bresenham's algorithms, detailing their approaches to efficiently plot line segments based on their endpoints and slopes. The document also highlights the limitations of DDA and presents Bresenham's Integer Line Drawing Algorithm as a more efficient alternative using integer arithmetic.

Uploaded by

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

Complete-Week 2 Line Drawing Algorithms

The document discusses line drawing algorithms in computer graphics, focusing on the challenges of scan conversion on pixel-based displays. It introduces the Digital Differential Analyzer (DDA) and Bresenham's algorithms, detailing their approaches to efficiently plot line segments based on their endpoints and slopes. The document also highlights the limitations of DDA and presents Bresenham's Integer Line Drawing Algorithm as a more efficient alternative using integer arithmetic.

Uploaded by

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

Line Drawing Algorithm

The Problem Of Scan Conversion


• A line segment in a scene is defined by the
coordinate positions of the line end-points
y

(7, 5)

(2, 2)

x
The Problem (cont…)
• But what happens when we try to draw this
on a pixel based display?

How do we choose which pixels to turn on?


Considerations
• Considerations to keep in mind:
– The line has to look good
• Avoid jaggies
– It has to be lightning fast!
• How many lines need to be drawn in a typical scene?
• This is going to come back to bite us again and again
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
A Very Simple Solution
• We could simply work out the corresponding y
coordinate for each unit x coordinate
• Let’s consider
y
the following example:

(7, 5)
5

2
(2, 2)

x
2 7
Problem
• DDA = for each x plot pixel at closest y
– Problems for steep lines
Using Symmetry
• Use for 1  m  0
• For m > 1, swap role of x and y
– For each y, plot closest x
A Note About Slopes (cont…)

7
• We can see easily that
6
this line doesn’t look 5
very good! 4
3
• We choose which way
2
to work out the line 1
pixels based on the 0

slope of the line 0 1 2 3 4 5 6 7 8

Discontinuity
Line Drawing Algorithms

A line in Computer graphics typically refers to a line


segment, which is a portion of a straight line that
extends indefinitely in opposite directions. 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 the 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 & the intercept b
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 by using the
equation
Δy = m Δx (4)
Similarly, we can obtain x interval Δx corresponding to Δy
by using the equation:
Δx = Δy/m (5)
• If |m|=1, Δx = Δy. In this case, a smooth line with slope
m=1 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 the 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 the equation Δx = Δy/m & scan
convert (x,y).
Example 1 The endpoints of a line are (0,0) & (6,18).
Compute each value of y as x steps from 0 to 6 and
plot the result.
Given: (x1,y1)=(0,0), (x2,y2)=(6,18)
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.
• Simply calculate yi+1 based on
yi 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, therefore
• we have, yi+1 = yi + Δy
yi+1 = yi + mΔx (1)
and xi+1 = xi + Δy/m (2)
The DDA Algorithm:Basics (cont…)
• Consider the list of points that we determined
for the line in the following example, where
slope is 3/5 :
(2, 2), (3, 23/5), (4, 31/5), (5, 34/5), (6, 42/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
DDA 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
Bresenham's Line Drawing Algorithm
• Originally developed for digital plotters, equally
suited for CRT raster devices.
• Meant for first octant.
• Algorithm always increments 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 algorithm: core idea
• At each step, choice between 2 pixels
(0£ m£ 1)

…or that one

Line drawn so far Either I lit this pixel…


Bresenham algorithm

• I need a criterion to pick between them


• Distance between line and center of pixel:
– the error associated with this pixel

e=+1/2
Error pixel 2
e=0
Error pixel 1
e=-1/2
Ranges of e and m
• Error term e must always lie between -1/2 and +1/2,
i.e.,
-1/2 <= e <= +1/2.
• Slope m of the actual line is between 0 and 1 since
only the first octant is being considered, i.e.,
0 <= m <= 1
• There is a correspondence between the lower and
upper bounds of e and m, i.e., e=-1/2 corresponds to
m=0 and so on.
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, ∆y = 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) to (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, and to efficiently implement in
hardware and firmware)
• Meant for Ist Octant only.
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)

Initial Calculations
x1=5, y1=4
∆x=7, ∆y=3,
ebar=2∆y- ∆x = -1
Bresenham’s General 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 Interchange = 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.

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