Digital Differential Analyser: Basic Line DDA
Digital Differential Analyser: Basic Line DDA
(7, 5)
5
2
(2, 2)
2 7 x
Basic of line
• Line drawing is accomplished by calculating
intermediate positions along the line path
between specified end points.
• Y=mX+B
• A slope of 2 means that every 1-unit change in X
yields a 2-unit change in Y.
B
Line Equations
• Let’s quickly review the equations involved in
drawing lines
y m x b
y
where:
y2 y1
y2 m
x2 x1
y1 b y1 m x1
x1 x2 x
Algorithm
• Start at the pixel for the left-hand endpoint x1
• Step along the pixels horizontally until reach the right-hand
end point x2
• For each value of x compute corresponding y value
• Round this value to nearest integer to select the nearest pixel
Basic Algorithm:
For x= x1 to x2
y=mx+b
PlotPixel(x,round(y))
End;
0
0 1 2 3 4 5 6 7
y2 y1
m b y1 m x1
A Very Simple Solution x2 x1
y m x b
y
(7, 5) • First work out m and b:
5
52 3
m 3
b 2 2
4
2 72 5 5 5
(2, 2)
2 3 4 5 6 7 x 3 4 3
Now for each x value work out the y value: y(3) 3 2
5 5 5
3 4 1 3 4 4 3 4 2
y(4) 4 3 y(5) 5 3 y(6) 6 4
5 5 5 5 5 5 5 5 5
How do we choose which pixels to turn on?
A Very Simple Solution
• Now just round off the results and turn on these
pixels to draw our line
3
7 y(3) 2 3
6 5
5 1
y(4) 3 3
4 5
3 4
2 y(5) 3 4
1 5
0 2
y(6) 4 4
0 1 2 3 4 5 6 7 8 5
Considerations
• Considerations to keep in mind:
• The line has to look good
• Avoid jaggies
Basic Algorithm:
For x= x1 to x2
PlotPixel(x,round(y))
y=y+m
End;
The DDA Algorithm (cont…)
• When the slope of the line is between -1 and 1 begin at the first
point in the line and, by incrementing the x coordinate by 1,
calculate the corresponding y coordinates as follows:
yk 1 yk m
• When the slope is outside these limits, increment the y coordinate
by 1 and calculate the corresponding x coordinates as follows:
1
xk 1 xk
m
If m: -1,1
{
if m:0,1
dX=1
if m:0,-1
dX=-1
increment X coordinate ,X i+1=Xi+dX
}
Otherwise
{
if m>1
dY=1
if m<-1
dY=-1
increment Y coordinate , Y i+1=Yi+dY
}
• Slope >= -1
• Unit y interval = 1
DDA Algorithm
• Digital Differential Analyzer
• Slope < -1
• Unit y interval = -1
Practice Example…
Practice
Line Drawing using DDA:
1. (200,700), (100,-200)
2. (200,700), (-500, 500)
3. (200, 700), (350,1000)
4. (-150, 300), (-50,-300)
5. (300,75), (100, 50)
The DDA Algorithm Summary
• The DDA algorithm is much faster than our
previous attempt
• In particular, there are no longer any multiplications
involved
• However, there are still two big issues:
• Accumulation of round-off errors can make the pixelated
line drift away from what was intended
• The rounding operations and floating point arithmetic
involved are time consuming
• The DDA algorithm is pretty good – but we can do
better
• Next we’ll look at the Bresenham line algorithm.
Thank You