Digital Differential Analyzer (graphics algorithm)

From Wikipedia, the free encyclopedia

In Computer graphics, Digital Differential Analyzer (DDA) is an algorithm used to determine which points need to be plotted in order to draw a straight line between two given points. It employs the equation for line representation (example: y=mx+c), then scan through an axis. Each scan it would determine the value on the other axis using the equation, this way the proper pixel can be located.

Contents

[edit] Overview

The DDA method is not very efficient due to the need for division and rounding. Bresenham's line algorithm is a more efficient method to draw lines because it uses only addition, subtraction and bit shifting.

[edit] The algorithm in code

  This is a sample implementation of the DDA algorithm in the C programming language:

/* Draw a straight line between the points (xa, ya) and (xb, yb) */
void line DDA(int xa, int ya, int xb, int yb) {
           
            int dx=xb-xa; // horizontal difference
            int dy=yb-ya; // vertical difference
            int steps, k;
            float xIncrement, yIncrement;
            float x=xa, y=ya; // the drawing points x and y are initiated to one endpoint of the line

            // calculate the number of steps used to draw the line (number of pixels)
            if(abs(dx)>abs(dy)) 
                steps=abs(dx);
            else 
                steps=abs(dy);        

            // calculate the increment for x and y in each step
            xIncrement=dx/(float)steps;
            yIncrement=dy/(float)steps;

            // point out the starting pixel
            setPixel(ROUND(x), ROUND(y));

            // loop through the line from one end to the other and point out the pixels
            for(k=0; k<steps; k++) {
                    
                    // the points are incremented an equal amount for every step
                    x += xIncrement;
                    y += yIncrement;
                        
                    // since the values for x and y are float they are rounded before they are plotted
                    setPixel(ROUND(x), ROUND(y));
            }
}

[edit] See also

[edit] External links