0% found this document useful (0 votes)
127 views23 pages

Digital Differential Analyser: Basic Line DDA

This document discusses algorithms for drawing lines on a digital display. It begins by explaining the basic line drawing process of calculating pixel positions between start and end points. It then covers the digital differential analyzer (DDA) algorithm, which takes an incremental approach to speed up the process. The DDA works by calculating the next y-value based on the current y-value and slope, rather than recalculating from the line equation at each step. This eliminates multiplications and speeds up the algorithm. However, issues like rounding errors and floating point operations still introduce inefficiencies. An improved algorithm called Bresenham's line algorithm will be discussed next.

Uploaded by

Wahida Ferdose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views23 pages

Digital Differential Analyser: Basic Line DDA

This document discusses algorithms for drawing lines on a digital display. It begins by explaining the basic line drawing process of calculating pixel positions between start and end points. It then covers the digital differential analyzer (DDA) algorithm, which takes an incremental approach to speed up the process. The DDA works by calculating the next y-value based on the current y-value and slope, rather than recalculating from the line equation at each step. This eliminates multiplications and speeds up the algorithm. However, issues like rounding errors and floating point operations still introduce inefficiencies. An improved algorithm called Bresenham's line algorithm will be discussed next.

Uploaded by

Wahida Ferdose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CSE- 4105

Basic Line DDA


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

(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;

Problems: Lot of computation and inefficient.


For each iteration: 1 float multiplication, 1 addition, 1 round
A Very Simple Solution

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

52 3
m  3
b  2 2 
4
2 72 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

• It has to be lightening fast!


• How many lines need to be drawn in a
typical scene?
• This is going to come back to bite us again
and again
A Very Simple Solution
• However, this approach is just way too slow
• In particular look out for:
• The equation y = mx + b requires the multiplication of
m by x
• Rounding off the resulting y coordinates
• We need a faster solution

Problems: Lot of computation and inefficient.


For each iteration: 1 float multiplication, 1 addition, 1 round
Line Equations
• The equation of a simple line is: yi  m  xi  b
• Modify the equation:
yi 1  m  xi 1  b
 m( xi  1)  b
 mxi  m  b
Replace by yi  (mxi  b)  m
 yi  m
The DDA Algorithm
• The digital differential analyzer (DDA)
algorithm takes an incremental approach in order
to speed up scan conversion

• Simply calculate yk+1 based on yk


The DDA Algorithm (cont…)

• Consider the list of points that we determined for the


line in our previous example:
• (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

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
}

dy=-1 dx=-1 dx=1 dy=1


increment Y coordinate increment X coordinate increment Y coordinate
Slope value: … -2 -1 0 0 1 2 ….
DDA Algorithm
• Digital Differential Analyzer
• 0 < Slope <= 1
• Unit x interval = 1

• Slope >= -1
• Unit y interval = 1
DDA Algorithm
• Digital Differential Analyzer

• -1 <= Slope < 0


• Unit x interval = -1

• Slope < -1
• Unit y interval = -1
Practice Example…
Practice
Line Drawing using DDA:

Compute the slope and the first 10 pixels of the


following line segments

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

You might also like