Digital differential analyzer (graphics algorithm)
In computer graphics, a digital differential analyzer (DDA) is hardware or software used for interpolation of variables over an interval between start and end point. DDAs are used for rasterization of lines, triangles and polygons. They can be extended to non linear functions, such as perspective correct texture mapping, quadratic curves, and traversing voxels.
In its simplest implementation for linear cases such as lines, the DDA algorithm interpolates values in interval by computing for each xi the equations xi = xi−1 + 1, yi = yi−1 + m, where m is the slope of the line. This slope can be expressed in DDA as follows:
In fact any two consecutive points (x,y) lying on this line segment should satisfy the equation.
Performance
The DDA method can be implemented using floating-point or integer arithmetic. The native floating-point implementation requires one addition and one rounding operation per interpolated value (e.g. coordinate x, y, depth, color component etc.) and output result. This process is only efficient when an FPU with fast add and rounding operation will be available.
The fixed-point integer operation requires two additions per output cycle, and in case of fractional part overflow, one additional increment and subtraction. The probability of fractional part overflows is proportional to the ratio m of the interpolated start/end values.
DDAs are well suited for hardware implementation and can be pipelined for maximized throughput.
Algorithm
A linear DDA starts by calculating the smaller of dy or dx for a unit increment of the other. A line is then sampled at unit intervals in one coordinate and corresponding integer values nearest the line path are determined for the other coordinate.
Considering a line with positive slope, if the slope is less than or equal to 1, we sample at unit x intervals (dx=1) and compute successive y values as
Subscript k takes integer values starting from 0, for the 1st point and increases by 1 until endpoint is reached. y value is rounded off to nearest integer to correspond to a screen pixel.
For lines with slope greater than 1, we reverse the role of x and y i.e. we sample at dy=1 and calculate consecutive x values as
Similar calculations are carried out to determine pixel positions along a line with negative slope. Thus, if the absolute value of the slope is less than 1, we set dx=1 if i.e. the starting extreme point is at the left.
Program
DDA algorithm Program in Turbo C++:
1 #include <graphics.h>
2
3 #include <iostream.h>
4 #include <math.h>
5 #include <dos.h>
6 #include <conio.h>
7
8 void main( )
9 {
10 float x, y, x1, y1, x2, y2, dx, dy, step;
11 int i, gd = DETECT, gm;
12 initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
13
14 cout << "Enter the value of x1 and y1 : ";
15 cin >> x1 >> y1;
16 cout << "Enter the value of x2 and y2: ";
17 cin >> x2 >> y2;
18
19 dx = (x2 - x1);
20 dy = (y2 - y1);
21 if (abs(dx) >= abs(dy))
22 step = abs(dx);
23 else
24 step = abs(dy);
25 dx = dx / step;
26 dy = dy / step;
27 x = x1;
28 y = y1;
29 i = 1;
30 while (i <= step) {
31 putpixel(x, y, 5);
32 x = x + dx;
33 y = y + dy;
34 i = i + 1;
35 delay(100);
36 }
37 getch();
38 closegraph();
39 }
See also
- Bresenham's line algorithm is an algorithm for line rendering.
- incremental error algorithm
- Xiaolin Wu's line algorithm is an algorithm for line anti-aliasing
References
http://www.museth.org/Ken/Publications_files/Museth_SIG14.pdf
- Alan Watt: 3D Computer Graphics, 3rd edition 2000, p. 184 (Rasterizing edges). ISBN 0-201-39855-9