Scan Conversion: DDA Algorithm
Scan Conversion: DDA Algorithm
Display devices need special procedures for displaying any graphic object: line, circle, curves, and
even characters. Irrespective of the procedures used, the system can generate the images on these
raster devices by turning the pixels on or off. The process in which the object is represented as the
collection of discrete pixels is called scan conversion. The video output circuitry of a computer is
capable of converting binary values stored in its display memory into pixel-on, pixel-off information
that can be used by a raster output device to display a point. This ability allows graphics computers to
display models composed of discrete dots.
A simple approach to scan-converting a line is to first scan-convert P and P to pixel coordinates
1 2
(x’ , y’ ) and (x’ , y’ ), respectively; then set m = (y’ , y’ ) (x’ , x’ ) and b = y’ –mx’ . If [m] <=1
1 1 2 2 2 1 2 1 1 1
then for every integer value of x between and excluding x’, and x’ , calculate the corresponding value
2
of y using the equation and scan-convert (x, y). If [m] > 1, then for every integer value of y between
and excluding y’ and y’ calculate the corresponding value of x using the equation and scan-convert
1 2
(x, y).
DDA Algorithm
This algorithm works on the principle of obtaining the successive pixel values based on the
differential equation governing the line. Since screen pixels are referred with integer values, or
plotted positions, which may only approximate the calculated coordinates – i.e., pixels which are
intensified are those which lie very close to the line path if not exactly on the line path which in this
case are perfectly horizontal, vertical or 45° lines only. Standard algorithms are available to
determine which pixels provide the best approximation to the desired line, one such algorithm is the
DDA (Digital Differential Analyser) algorithm. Before going to the details of the algorithm, let us
discuss some general appearances of the line segment, because the respective appearance decides
which pixels are to be intensified. It is also obvious that only those pixels that lie very close to the
line path are to be intensified because they are the ones which best approximate the line. Apart from
the exact situation of the line path, which in this case are perfectly horizontal, vertical or 45° lines
(i.e., slope zero, infinite, one) only. We may also face a situation where the slope of the line is > 1 or <
1.Which is the case shown in Figure 1.
In Figure 1, there are two lines. Line 1 (slope<1) and line 2 (slope>1). Now let us discuss the general
mechanism of construction of these two lines with the DDA algorithm. As the slope of the line is a
crucial factor in its construction, let us consider the algorithm in two cases depending on the slope of
the line whether it is > 1 or < 1.
Case 1: slope (m) of line is < 1 (i.e., line 1): In this case to plot the line we have to move the
direction of pixel in x by 1 unit every time and then hunt for the pixel value of the y direction which
best suits the line and lighten that pixel in order to plot the line.
So, in Case 1 i.e., 0 < m < 1 where x is to be increased then by 1 unit every time and proper y is
approximated.
Figure 1
Case 2: slope (m) of line is > 1 (i.e., line 2) if m > 1 i.e., case of line 2, then the most appropriate
strategy would be to move towards the y direction by 1 unit every time and determine the pixel in x
direction which best suits the line and get that pixel lightened to plot the line.
So, in Case 2, i.e., (infinity) > m > 1 where y is to be increased by 1 unit every time and proper x is
approximated.
Algorithm:
Step 1: Get the input of two end points (X0, Y0) and (X1, Y1).
Step 2: Calculate the difference between two end points.
dx = X1 - X0
dy = Y1 - Y0
Step 3: Based on the calculated difference in step-2, you need to identify the number of steps to put pixel. If dx >
dy, then you need more steps in x coordinate; otherwise in y coordinate.
if (dx > dy)
Steps = absolute(dx);
else
Steps = absolute(dy);
Step 4: Calculate the increment in x coordinate and y coordinate.
Xincrement = dx / (float) steps;
Yincrement = dy / (float) steps;
Step 5: Put the pixel by successfully incrementing x and y coordinates accordingly and complete the drawing of the
line.
for(int v=0; v < Steps; v++)
{
x = x + Xincrement;
y = y + Yincrement;
putpixel(x,y);
}
Bresenham’s Line Algorithm
A pixel is plotted at the starting coordinate of the line, and each iteration of the algorithm increments the
pixel one unit along the major, or x-axis. The pixel is incremented along the minor, or y-axis, only when a
decision variable (based on the slope of the line) changes sign. A key feature of the algorithm is that it
requires only integer data and simple arithmetic. This makes the algorithm very efficient and fast. The
Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of this
algorithm is that, it uses only integer calculations. Moving across the x axis in unit intervals and at each
step choose between two different y coordinates.
For example, as shown in the following illustration, from position (2, 3) you need to choose between (3,
3) and (3, 4). You would like the point that is closer to the original line.
At sample position xk+1, the vertical separations from the mathematical line are
labelled as dupper and dlower.
From the above illustration, the y coordinate on the mathematical line at xk+1 is:
𝑌 = m(𝑋𝑘 + 1) + 𝑏
So, dupper and dlower are given as follows:
𝑑𝑙𝑜𝑤𝑒𝑟 = 𝑦 − 𝑦𝑘
= m (𝑋𝑘 + 1) + 𝑏 − 𝑌𝑘
and
𝑑𝑢𝑝𝑝𝑒𝑟 = (𝑦𝑘 + 1) − 𝑦
= 𝑌𝑘 + 1 − m (𝑋𝑘 + 1) − 𝑏
You can use these to make a simple decision about which pixel is closer to the mathematical line. This
simple decision is based on the difference between the two pixel positions.
𝑑𝑙𝑜𝑤𝑒𝑟 − 𝑑𝑢𝑝𝑝𝑒𝑟 = 2m(𝑥𝑘 + 1) − 2𝑦𝑘 + 2𝑏 − 1
Let us substitute m with dy/dx where dx and dy are the differences between the endpoints.
𝑑x(𝑑𝑙𝑜𝑤𝑒𝑟 − 𝑑𝑢𝑝𝑝𝑒𝑟 ) = 𝑑𝑥(2𝑑𝑦/ 𝑑𝑥 (𝑥𝑘 + 1) − 2𝑦𝑘 + 2𝑏 − 1)
= 2𝑑𝑦 ∙ 𝑥𝑘 − 2𝑑𝑥 ∙ 𝑦𝑘 + 2𝑑𝑦 + (2𝑏 − 1)
= 2𝑑𝑦 ∙ 𝑥𝑘 − 2𝑑𝑥 ∙ 𝑦𝑘 + 𝐶
So, a decision parameter pk for the kth step along a line is given by:
𝑝𝑘 = 𝑑x(𝑑𝑙𝑜𝑤𝑒𝑟 − 𝑑𝑢𝑝𝑝𝑒𝑟 )= 2𝑑𝑦 ∙ 𝑥𝑘 − 2𝑑𝑥 ∙ 𝑦𝑘 + 𝐶
The sign of the decision parameter pk is the same as that of dlower – dupper.
If pk is negative, then choose the lower pixel, otherwise choose the upper pixel. Remember, the
coordinate changes occur along the x axis in unit steps, so you can do everything with integer
calculations. At step k+1, the decision parameter is given as:
𝑝𝑘+1 = 2𝑑𝑦 ∙ 𝑥𝑘+1 − 2𝑑𝑥 ∙ 𝑦𝑘+1 + 𝐶
Subtracting pk from this we get:
𝑝𝑘+1 − 𝑝𝑘 = 2(𝑥𝑘+1 − 𝑥𝑘 ) − 2𝑑𝑥(𝑦𝑘+1 − 𝑦𝑘 )
But, xk+1 is the same as xk+1. So:
𝑝𝑘+1 = 𝑝𝑘 + 2𝑑𝑦 − 2(𝑦𝑘+1 − 𝑦𝑘 )
Where, yk+1 – yk is either 0 or 1 depending on the sign of pk.
The first decision parameter p0 is evaluated at (x0, y0) is given as:
𝑝0 = 2𝑑𝑦 − 𝑑𝑥
Now, keeping in mind all the above points and calculations, here is the Bresenham algorithm for slope m
< 1:
Algorithm:
Step 1: Input the two end-points of line, storing the left end-point in (x0, y0).
Step 2: Plot the point (x0, y0).
Step 3: Calculate the constants dx, dy, 2dy, and (2dy – 2dx) and get the first value
for the decision parameter as:
𝑝0 = 2𝑑𝑦 − 𝑑𝑥
Step 4: At each xk along the line, starting at k = 0, perform the following test:
If pk < 0, the next point to plot is (xk+1, yk) and
𝑝𝑘+1 = 𝑝𝑘 + 2𝑑𝑦Otherwise,
𝑝𝑘+1 = 𝑝𝑘 + 2𝑑𝑦 − 2𝑑𝑥
Step 5: Repeat step 4 (dx – 1) times.
For m > 1, find out whether you need to increment x while incrementing y each time. After solving, the
equation for decision parameter pk will be very similar, just the x and y in the equation gets interchanged.