What is the DDA algorithm?
DDA stands for Digital Differential Analyzer. In computer graphics, the DDA line drawing algorithm is used to generate the intermediate points between two given endpoints of a line. Since a screen is made of pixels, the mathematical line must be converted into integer pixel positions.
The algorithm starts from the first endpoint, repeatedly increases x and y by a fixed amount, rounds the result to the nearest pixel, and plots that pixel using putpixel() in graphics.h.
DDA formula
For a line from (x1, y1) to (x2, y2), first calculate:
dy = y2 - y1
steps = max(abs(dx), abs(dy))
xIncrement = dx / steps
yIncrement = dy / steps
The value of steps is chosen as the larger of abs(dx) and abs(dy) so that the algorithm moves one pixel at a time along the dominant direction of the line.
Steps of the DDA line drawing algorithm
- Read the starting point
(x1, y1)and ending point(x2, y2). - Calculate
dx = x2 - x1anddy = y2 - y1. - Set
stepsto the larger value betweenabs(dx)andabs(dy). - Calculate
xIncrement = dx / stepsandyIncrement = dy / steps. - Start with
x = x1andy = y1. - Plot
round(x), round(y). - Add the increments to x and y, then repeat until all steps are complete.
Example calculation
Suppose we want to draw a line from (2, 3) to (8, 6).
dy = 6 - 3 = 3
steps = max(6, 3) = 6
xIncrement = 6 / 6 = 1
yIncrement = 3 / 6 = 0.5
The algorithm starts at (2, 3). On every step, x increases by 1 and y increases by 0.5. After rounding, the plotted points look like this:
| Step | x | y | Pixel plotted |
|---|---|---|---|
| 0 | 2 | 3 | (2, 3) |
| 1 | 3 | 3.5 | (3, 4) |
| 2 | 4 | 4 | (4, 4) |
| 3 | 5 | 4.5 | (5, 5) |
| 4 | 6 | 5 | (6, 5) |
| 5 | 7 | 5.5 | (7, 6) |
| 6 | 8 | 6 | (8, 6) |
DDA algorithm program in C using graphics.h
This program draws a line using the DDA algorithm. It does not use the built-in line() function for drawing the final line; instead, it plots each point with putpixel().
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int roundValue(float value)
{
return (int)(value + 0.5);
}
void drawDDALine(int x1, int y1, int x2, int y2)
{
int dx = x2 - x1;
int dy = y2 - y1;
int steps;
int i;
float xIncrement;
float yIncrement;
float x = x1;
float y = y1;
if (abs(dx) > abs(dy))
steps = abs(dx);
else
steps = abs(dy);
if (steps == 0)
{
putpixel(x1, y1, WHITE);
return;
}
xIncrement = dx / (float)steps;
yIncrement = dy / (float)steps;
for (i = 0; i <= steps; i++)
{
putpixel(roundValue(x), roundValue(y), WHITE);
x = x + xIncrement;
y = y + yIncrement;
delay(20);
}
}
int main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2;
printf("Enter x1 y1: ");
scanf("%d %d", &x1, &y1);
printf("Enter x2 y2: ");
scanf("%d %d", &x2, &y2);
initgraph(&gd, &gm, "");
drawDDALine(x1, y1, x2, y2);
getch();
closegraph();
return 0;
}
Try it online: open the graphics.h online compiler, paste the program, then use sample inputs 100 100 and 400 250.
Advantages of DDA algorithm
- It is simple and easy to understand.
- It works for all line slopes, including horizontal, vertical, and diagonal lines.
- It directly follows the mathematical idea of a straight line.
Limitations of DDA algorithm
- It uses floating-point arithmetic, which can be slower than integer-only methods.
- It requires rounding at every step.
- For performance-critical graphics, Bresenham's line algorithm is usually preferred.
Common mistakes
Using integer division
If you write xIncrement = dx / steps using only integers, fractional values may be lost. Use dx / (float)steps instead.
Forgetting to round pixel positions
Pixel coordinates are integers. Since DDA produces floating-point x and y values, round them before passing them to putpixel().
Using line() instead of putpixel()
The purpose of this algorithm is to generate the line manually. If you call line(x1, y1, x2, y2), graphics.h draws the line for you and the DDA logic is skipped.
Summary
The DDA line drawing algorithm calculates the number of steps needed between two endpoints, computes fixed increments for x and y, and plots rounded pixel positions one by one. It is a useful first algorithm for learning how mathematical shapes are converted into pixels on a display.