DDA Line Drawing Algorithm (Digital Differential Analyzer)
The DDA (Digital Differential Analyzer) algorithm is a line drawing algorithm used in computer graphics to generate a straight line between two given points. It works on the principle of incremental calculations, meaning it calculates intermediate points step by step rather than solving the line equation repeatedly.
In DDA, a line is generated by incrementing one coordinate (x or y) in small steps and calculating the corresponding value of the other coordinate using the slope of the line.
Principle of DDA Algorithm
A straight line between two points is mathematically defined as:
y = mx + c
But instead of directly using this equation, DDA uses incremental changes:
- If slope (m) ≤ 1 → increment x
- If slope (m) > 1 → increment y
This ensures smooth line generation.
Steps of DDA Algorithm
Input two points:
- (x1, y1),
- (x2, y2)
Step 1: Calculate Differences
- ΔX = X2 − X1, ΔY = Y2 − Y1
- m = ΔY / ΔX
- Slope (m) determines which case to apply.
Step 2: Determine Number of Steps
steps = {|ΔX| if |ΔX| > |ΔY|, |ΔY| otherwise
This ensures:
- More changes → more steps
- Covers all types of lines (horizontal, vertical, steep, gentle)
Step 3: Find Next Points (ALL CASES)
Let current point = (Xp, Yp)
Case 1: |m| < 1 (Gentle Slope)
- X increases by 1
- Y increases by slope
Xp+1 = Xp+1 Yp+1 = Yp+m
Used when ∣ΔX∣>∣ΔY∣
Case 2: m = 1
- Equal increments
Xp+1 = Xp+1 and Yp+1 = Yp+1
Straight diagonal line
Case 3: |m| > 1 (Steep Slope)
- Y increases by 1
- X increases slowly
Xp+1 = Xp + 1/m and Yp+1 = Yp+1
Used when |ΔY| > |ΔX|
Case 4: Negative Slope
- Same formulas apply
- Values decrease automatically
Xinc, Yinc become negative
Case 5: Vertical Line
- ΔX = 0
X = constant, and Y = Y + 1
Case 6: Horizontal Line
- ΔY = 0
X = X + 1, and Y=constant
Step 4: Repeat
- Continue until:
- End point is reached OR
- Generated points = steps
Key Characteristics
- Uses floating point calculations
- Simple and easy to implement
- Less efficient than Bresenham
- Produces smooth lines but slightly slower
Advantages and Disadvantages
Advantages:
- Easy to understand
- Works for all slopes
- Simple incremental method
Disadvantages:
- Uses floating point arithmetic
- Rounding errors occur
- Slower compared to integer-based algorithms
Solved Numerical Example
Case 1: Gentle Slope (|m| < 1)
From (2, 3) → (10, 6)
- dx = 8, dy = 3
- steps = 8
- Xinc = 1, Yinc = 0.375
Table:
| Step | x | y | Round(x,y) |
|---|---|---|---|
| 0 | 2.000 | 3.000 | (2, 3) |
| 1 | 3.000 | 3.375 | (3, 3) |
| 2 | 4.000 | 3.750 | (4, 4) |
| 3 | 5.000 | 4.125 | (5, 4) |
| 4 | 6.000 | 4.500 | (6, 5) |
| 5 | 7.000 | 4.875 | (7, 5) |
| 6 | 8.000 | 5.250 | (8, 5) |
| 7 | 9.000 | 5.625 | (9, 6) |
| 8 | 10.000 | 6.000 | (10, 6) |
Case 2: Steep Slope (|m| > 1)
From (2, 2) → (5, 10)
- dx = 3, dy = 8
- steps = 8
- Xinc = 0.375, Yinc = 1
Table:
| Step | x | y | Round(x,y) |
|---|---|---|---|
| 0 | 2.000 | 2.000 | (2, 2) |
| 1 | 2.375 | 3.000 | (2, 3) |
| 2 | 2.750 | 4.000 | (3, 4) |
| 3 | 3.125 | 5.000 | (3, 5) |
| 4 | 3.500 | 6.000 | (4, 6) |
| 5 | 3.875 | 7.000 | (4, 7) |
| 6 | 4.250 | 8.000 | (4, 8) |
| 7 | 4.625 | 9.000 | (5, 9) |
| 8 | 5.000 | 10.000 | (5, 10) |
Case 3: m = 1 (Perfect Diagonal)
From (1, 1) → (6, 6)
- dx = dy = 5
- steps = 5
- Xinc = 1, Yinc = 1
Table:
| Step | x | y | Plot |
|---|---|---|---|
| 0 | 1 | 1 | (1, 1) |
| 1 | 2 | 2 | (2, 2) |
| 2 | 3 | 3 | (3, 3) |
| 3 | 4 | 4 | (4, 4) |
| 4 | 5 | 5 | (5, 5) |
| 5 | 6 | 6 | (6, 6) |
Case 4: Negative Slope
From (10, 5) → (5, 2)
- dx = -5, dy = -3
- steps = 5
- Xinc = −1, Yinc= −0.6
Table:
| Step | x | y | Round(x,y) |
|---|---|---|---|
| 0 | 10.0 | 5.0 | (10, 5) |
| 1 | 9.0 | 4.4 | (9, 4) |
| 2 | 8.0 | 3.8 | (8, 4) |
| 3 | 7.0 | 3.2 | (7, 3) |
| 4 | 6.0 | 2.6 | (6, 3) |
| 5 | 5.0 | 2.0 | (5, 2) |
Case 5: Vertical Line
From (4, 2) → (4, 8)
- dx = 0, dy = 6
- steps = 6
- Xinc = 0, Yinc = 1
Table:
| Step | x | y | Plot |
|---|---|---|---|
| 0 | 4 | 2 | (4, 2) |
| 1 | 4 | 3 | (4, 3) |
| 2 | 4 | 4 | (4, 4) |
| 3 | 4 | 5 | (4, 5) |
| 4 | 4 | 6 | (4, 6) |
| 5 | 4 | 7 | (4, 7) |
| 6 | 4 | 8 | (4, 8) |
Case 6: Horizontal Line
From (3, 5) → (8, 5)
- dx = 5, dy = 0
- steps = 5
- Xinc=1, Yinc=0
Table:
| Step | x | y | Plot |
|---|---|---|---|
| 0 | 3 | 5 | (3, 5) |
| 1 | 4 | 5 | (4, 5) |
| 2 | 5 | 5 | (5, 5) |
| 3 | 6 | 5 | (6, 5) |
| 4 | 7 | 5 | (7, 5) |
| 5 | 8 | 5 | (8, 5) |
Practice Problems
Try solving these:
- Draw line from (0,0) to (5,5)
- Draw line from (2,3) to (5,6)
- (1,2) to (7,5)
- (3,3) to (8,10)
- Calculate the points between the starting point (5, 6) and ending point (8, 12).
- Calculate the points between the starting point (5, 6) and ending point (13, 10).
- Calculate the points between the starting point (1, 7) and ending point (11, 17).
Summary
DDA is a simple line drawing algorithm that works by incrementally calculating intermediate points using slope-based increments. Although easy to implement, it is less efficient than Bresenham’s algorithm due to floating-point calculations.