Professional Documents
Culture Documents
m = -1 m=1
m = -1/2 m = 1/ 2
m = -1/3 m = 1/3
m=0 m=0
DDA(Digital Differential Analyzer) Algorithm
• DDA(Digital Differential Analyzer) Algorithm
– Scan conversion line algorithm based on ∆x or ∆y
– Sample the line at unit interval in one coord. and determine
corresponding integer value
• When line slope (m) is positive.
Case 1) 0 < m ≤1
− Sample at unit x interval (∆x = 1)
− From eq.(1.a)
xk +1 = xk + 1 yk +1 = yk + m
Case 2) m>1
− Sample at unit y interval (∆ y = 1)
− From eq.(1.b)
1
xk +1 = xk + yk +1 = yk + 1
m
DDA(Digital Differential Analyzer) Algorithm
• When line slope(m) is negative.
Case 3) -1 ≤ m < 0
− Sample at unit x interval (∆ x = -1)
xk +1 = xk − 1 yk +1 = yk − m
Case 4) m < -1
− Sample at unit y interval (∆ y = -1)
− From eq.(3,b)
1
xk +1 = xk − yk +1 = yk − 1
m
• DDA Algorithm
- Faster method than y = mx + b using but may specifies inaccurate pixel section
- Rounding off operations and floating point arithmetic operations are time
consuming.
Pseudo Code of DDA Algorithm
#include <stdlib.h>
#include <math.h>
inline int round( const float a ) { return int(a+0.5); }
void lineDDA( int x0, int y0, int xEnd, int yEnd )
{
int dx = xEnd – x0, dy = yEnd – y0, steps, k;
float xIncrement, yIncrement, x = x0, y = y0;
if( fabs(dx) > fabs(dy) )
steps = fabs(dx);
else
steps = fabs(dy);
xIncrement = float(dx)/float(steps);
yIncrement = float(dy)/float(steps);
setPixel( round(x), round(y) );
for( k=0; k<steps; k++ ) {
x += xIncrement;
y += yIncrement;
setPixel( round(x), round(y) );
}
}
Bresenham’s Algorithm
• Bresenham’s Algorithm
– Originally developed for digital plotter.
– Seek optimum raster loc. to represent a straight line
– Increment by one unit (x or y) depending on the slope of
line
– corresponding value is determined by examining the
distance between the actual line and nearest grid loc.
The Big Idea
• Move across the x axis in unit intervals and at each
step choose between two different y coordinates
(xk+1, yk+1)
position (2, 3) we have
4
to choose between (3, 3)
(xk, yk) and (3, 4)
3
We would like the point
(xk+1, yk)
2
that is closer to the
original line
2 3 4 5
Deriving The Bresenham Line Algorithm
• At sample position xk+1 y
the vertical separations
k+1
dupper
y
from the mathematical dlower
yk
line are labelled dupper
and dlower xk+1
The y coordinate on the mathematical line at xk+1
is:
y = m( xk + 1) + b
Deriving The Bresenham Line Algorithm (cont…)
= 2∆y ⋅ xk − 2∆x ⋅ yk + c
Deriving The Bresenham Line Algorithm (cont…)
pk +1 − pk = 2∆y ( xk +1 − xk ) − 2∆x( yk +1 − yk )
Deriving The Bresenham Line Algorithm (cont…)
17 0
16 1
15 2
14 3
13 4
12
5
6
11
7
10
8
20 21 22 23 24 25 26 27 28 29 30
9
Bresenham Line Algorithm Summary