Lecture 5 Line Drawing Algos
Lecture 5 Line Drawing Algos
Output Primitives
• Points
• Lines
– DDA Algorithm
– Bresenham’s Algorithm
A picture can be described as:
• The set of intensities for the pixel position in the display.
OR
• As a set of complex objects, such as trees and terrain or
furniture and walls, positioned at specified coordinate locations
within the scene.
y
setPixel (x, y)
cgvr.korea.ac.kr
Line drawing is accomplished by calculating intermediate
positions along the line path between two specified endpoint
positions.
y2
y1
x1 x2
• The Cartesian slope intercept equation for straight line is
y=mx+b
where m is slope of the line and b as the y intercept
• Let (x1,y1) & (x2,y2) be the two endpoints of the line, then
m=(y2-y1)/(x2-x1)
b= y1- mx1
Jaggies
= Aliasing
DDA Algorithm
• Digital Differential Analyzer
0 < Slope <= 1
• Unit x interval = 1
y2
y1
x1 x2
yk 1 yk m
• Digital Differential Analyzer
Slope > 1
• Unit y interval = 1
y2
y1
x1 x2
1
xk 1 xk
m
• Digital Differential
Analyzer
-1 <= Slope < 0
• Unit x interval = -1
y1
y2
x1 x2
yk 1 yk m
• Digital Differential Analyzer
Slope < -1
• Unit y interval = -1
y2
y1
x1 x2
1
xk 1 xk
m
Digital Differential Algorithm(DDA)
• Scan conversion algorithm based on calculating either ∆x or
∆y.
• Sample the line at unit intervals in one coordinate and
determine the corresponding integer values nearest the line
path for the other coordinate.
Case 1
Line with positive slope less than or equal to 1, we sample at
unit x intervals (∆x =1) and determine the successive y value as
∆y=m ∆x
Yk+1= Yk + m Yk+1- Yk =m ∆x
Yk+1- Yk =m (∆x =1)
• k takes integer values starting from 1 and increases by 1 until the
final point is reached.
• m can be any number between 0 and 1.
• Calculated y values must be rounded off to the nearest integer.
• For lines with positive slope greater than 1, sample at unit y
intervals (∆y = 1) and calculate each successive x value as
xk+1= xk + 1/m
• The above eqns are based on the assumption that lines are
processed from left to right. If the processing is reversed then
5. Loop through the process steps times, till the last point xb, yb is
reached.
P1 P2
(xa,ya) (xb,yb)
Steps for DDA Algorithm
dx= xb – xa dy = yb - ya
Step 3: If abs(dx) >abs(dy)
steps =abs( dx)
else
steps =abs( dy)
Step 4: xincr = dx/steps
yincr =dy/steps
(ie dy/dx = m)
Draw a line using DDA Algorithm between (10, 100) and (50, 20)
Bresenham’s Line Algorithm
Bresenham Line Algorithm
(For +ve slope and 0<m<1)
BRESENHAM’S LINE DRAWING ALGORITHM
(for |m| < 1.0)
1. Input the two line end-points, storing the left end-point in
(x0, y0)
2. Plot the point (x0, y0)
3. Calculate the constants dx, dy, 2dy, and (2dy – 2dx) and
get the first value for the decision parameter as:
p0 2dy dx
4. At each xk along the line, starting at k = 0, perform the
following test. If pk < 0, the next point to plot is
(xk+1, yk) and:
pk 1 pk 2dy
Otherwise, the next point to plot is (xk+1, yk+1) and:
pk 1 pk 2dy 2dx
5. Repeat step 4 (dx – 1) times
Bresenham Line Algorithm
(For +ve slope and m>1)
BRESENHAM’S LINE DRAWING ALGORITHM
(for |m| > 1.0)
1. Input the two line end-points, storing the left end-point
in (x0, y0)
2. Plot the point (x0, y0)
3. Calculate the constants dx, dy, 2dx, and (2dx – 2dy)
and get the first value for the decision parameter as:
p0 2dx dy
4. At each yk along the line, starting at k = 0, perform the
following test. If pk < 0, the next point to plot is
(xk, yk+1 ) and:
pk 1 pk 2dx
Otherwise, the next point to plot is (xk+1, yk+1) and:
pk 1 pk 2dx 2dy
5. Repeat step 4 (dy – 1) times
Exercise:
Draw a line using Bresenham’s Algorithm between (10, 20) and (50, 100)
Draw a line using Bresenham’s Algorithm between (10, 20) and (50, 40)