Course: Computer Graphics (CSC209)
Semester: III
Author: Sanil Sthapit | github.com/Sanil-Sth
2D Line Drawing using the Digital Differential Analyzer (DDA) Algorithm
A computer screen is made up of thousands of tiny dots called pixels, arranged in a grid. Every pixel sits at an integer coordinate like (2, 5) or (100, 200). There is no such thing as a pixel at (1.5, 3.7).
A mathematical line, however, is continuous — it passes through infinitely many points, most of which are decimals. So when we want to draw a line on screen, we face a fundamental question:
Which pixels do we turn on so that it looks like a straight line?
The DDA (Digital Differential Analyzer) Algorithm answers this question. It is one of the simplest and most intuitive line drawing algorithms in computer graphics. The core idea is:
- Calculate the total difference in X (
dx) and Y (dy) between the two endpoints - Determine the number of steps needed — which is
max(|dx|, |dy|) - At each step, increment X and Y by a small equal floating point amount
- Round the result to the nearest integer and light up that pixel using
putpixel()
This produces a smooth, connected line of pixels between any two given points on screen.
- Simple to understand and implement
- Works correctly for all line slopes (steep, shallow, negative, horizontal, vertical)
- Uses floating point arithmetic which is slower than integer-based methods
- Rounding errors can accumulate over very long lines
Write a program to draw a 2D line using the DDA (Digital Differential Analyzer) Algorithm.
Step 1: Start
Step 2: Input the two endpoints of the line: (x1, y1) and (x2, y2)
Step 3: Calculate:
dx = x2 - x1
dy = y2 - y1
Step 4: Determine the number of steps:
if |dx| >= |dy|:
steps = |dx|
else:
steps = |dy|
Step 5: Calculate increment per step:
xInc = dx / steps
yInc = dy / steps
Step 6: Set starting point:
x = x1
y = y1
Step 7: Plot pixel at (round(x), round(y))
Step 8: Repeat 'steps' times:
x = x + xInc
y = y + yInc
Plot pixel at (round(x), round(y))
Step 9: Stop
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void drawDDA(int x1, int y1, int x2, int y2, int color) {
int dx = x2 - x1;
int dy = y2 - y1;
// Number of steps = the longer side (avoids gaps in the line)
int steps = (abs(dx) >= abs(dy)) ? abs(dx) : abs(dy);
// Increment per step (floating point for precision)
float xInc = (float)dx / steps;
float yInc = (float)dy / steps;
// Start from the first endpoint
float x = x1, y = y1;
// Plot each pixel along the line
for (int i = 0; i <= steps; i++) {
putpixel((int)round(x), (int)round(y), color);
x += xInc;
y += yInc;
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x1, y1, x2, y2;
printf("Enter starting point (x1 y1): ");
scanf("%d %d", &x1, &y1);
printf("Enter ending point (x2 y2): ");
scanf("%d %d", &x2, &y2);
drawDDA(x1, y1, x2, y2, WHITE);
getch();
closegraph();
return 0;
}Shows the user input for the line coordinates
The BGI graphics window showing the line drawn by the DDA algorithm
Zoomed in view clearly showing individual pixels plotted by the DDA algorithm
💡 The zoomed view demonstrates the core concept of DDA — each dot is an individual pixel placed by
putpixel()at rounded floating point coordinates.
In this lab, we successfully implemented the DDA Line Drawing Algorithm in C using graphics.h. The program accepts two endpoints from the user and draws a straight line between them by incrementally plotting pixels using floating point arithmetic.
The zoomed output clearly shows how DDA works at the pixel level — each pixel is individually calculated and plotted. The algorithm is simple and works correctly for all slopes. However, since it relies on floating point division and rounding at every step, it is slightly slower than integer-based methods like Bresenham's algorithm, which we will implement in the next lab.


