DDA Line Drawing Algorithm

Shubham Agrawal
2 min readDec 27, 2018

Digital Differential Analyzer (DDA) line drawing algorithm is the simplest
line drawing algorithm in computer graphics.

It works on incremental method. It plots the point from source to destination by incrementing the source coordinates.

Working:

  1. Suppose you have to draw a line from point P to point Q
  2. So your input will be: P(x1,y1) and Q(x2,y2)
  3. Now we will calculate the difference between x2 and x1, lets say dx = x2-x1
  4. Similarly dy = y2- y1
  5. Now, we will find biggest of both the difference, lets say noOfPointsToPlot= max(abs(dx), abs(dy)) where abs stands for absolute. One doubt, “Why are we choosing max of both?”, don’t worry will tell you after example.
  6. Now we know, how many number of points we need to plot. So why don’t we divide the total distance between points according to that. Therefore we need to increment x-coordinate by dx/noOfPointsToPlot and similarly y-coordinate by dy/noOfPointsToPlot, lets say xInc = dx/noOfPointsToPlot and yInc = dy/noOfPointsToPlot
  7. Now start plotting points from P(x1, y1) to Q(x2, y2) by repeatedly incrementing x1 and y2 and plot floor of incremented values.

Example:

Question: Draw a line from P(1,1) to Q(5,9) using DDA algorithm.
Solution:
P(1,1) => x1 = 1, y1= 1
Q(5,9) =>
x2 = 5, y2 = 9
Now lets calculate the difference:
dx = x2 — x1 = 5 –1 = 4
dy = y2 — y1 = 9 -1 = 8
Now lets calculate total number of points to plot:
noOfPointsToPlot = max(abs(dx), abs(dy)) = max(|4|,|8|) = 8
Now, lets discuss why we go for a max here, because we want to draw line as accurate as possible and accuracy can be given by more no of points.
Now lets calculate increment for both the coordinates.
xInc = dx/noOfPointsToPlot = 4/8 = 0.5
yInc = dy/noOfPointsToPlot = 8/8 = 1

Now, list all the points to be plotted:

Line drawn from above example is shown in this figure, now you will say “How stupid this line is?”. But actually that’s how computer draws the line, if you still don’t agree try drawing a line in paint and zoom it. Currently we are drawing a very small line only 10 pixels, but if you will create bigger line than this discrepancy will not be visible.

Algorithm:

Input P(x1,y1) and Q(x2,y2)
Calculate dx = x2-x1 and dy = y2-y1
Calculate noOfPointsToPlot = max(abs(dx), abs(dy))
Calculate xInc = dx/noOfPointsToPlot and yInc = dy/noOfPointsToPlot
Initialize, x = x1, y = y1
Plot( Floor(x), Floor(y))
do ( i=1 to noOfPointsToPlot)
x += xInc
y += yInc
Plot( Floor(x), Floor(y))

Thanks for you time. Please let me know the topics you want me to write about in comments.

--

--