Computer Graphics (ii) output graphic element _3_ algorithm _2_dda algorithm for line drawing

Source: Internet
Author: User

DDA algorithm
Digital Differential Analyzer (DDA) method is a line-scan conversion algorithm based on &x or & calculated using equation (3.4) or equation (3.5) Y. Samples the segment at unit intervals on one axis to determine the corresponding integer value closest to the line path on the other axis. First consider the line with positive slope shown in 3.6. For example, if the slope is less than or equal to 1, the unit x interval (&x = 1) is sampled and each Y-value is computed one at a intervals:

the subscript k takes an integer value, starting from the first point 1 increments up to the last endpoint. Since m can be any real number between 0 and 1, the calculated Y value must be rounded. For segments with a positive slope greater than L, the positions of X and Y are exchanged. That is, the unit y interval (&y = 1) is sampled and each successive X-value is computed:



at this point, each computed x value is rounded to the nearest pixel position along the Y-scan line. the equation (3.6) and the equation (3.7) are based on the assumption that the line segment is processed from the left endpoint to the right endpoint (see Figure 3.6). If this everprocess in the opposite direction, that is, the starting endpoint is on the right, then &x =-1, and
                                      
or (when the slope is greater than 1 o'clock) is &y =-1, and



equation (3.6) and equation (3.9) can also be used to calculate the pixel position of a segment with a negative slope. If the absolute value of the slope is less than 1 and the starting endpoint is on the left, you can set &x = 1 and use the equation (3.6) to calculate the Y value. When the starting endpoint is on the right side (with the same slope), we can set &x =-1 and get the position of Y by the equation (3.8). Similarly, the absolute value of the negative slope is greater than 1 o'clock and can be calculated using &y =-1 and equation (3.9) or &y = 1 and equation (3.7).
the algorithm can be summarized as follows: The pixel position of the two endpoints of the transmission line, and the horizontal and vertical difference between the endpoint positions are assigned to the parameters dx and dy. A parameter with a large absolute value determines the values of the parameter steps. Starting at the pixel location (x0, y0), determine the offset that is required for each step in the line segment to generate the next pixel position, and loop the above procedure steps times. If the absolute value of the DX is greater than that of the dy and the x0 is less than xend, then the increment values in the x and Y directions are 1 and M respectively. If the x direction changes large, but the x0 is greater than xend, then the decrement-1 and-M are used to generate each point on the line segment. In other cases, the Y direction uses a unit increment (or decrement), and the x direction uses the increment (or decrement) of the 1/m.
#include <stdlib.h> #include <math.h>inline int round (const float a) {return int (a + 0.5);} void LineDDA (int x0,int y0,int xend,int yend) {     int dx = xend-x0, dy = yend-y0,steps, k; float xincrement, Yincreme nt,x = X0,y = y0;if (fabs (DX) >fabs (dr) Steps   = fabs (dx); elsesteps = Fabs (dy); xincrement = float (dx)/float (steps ): Yincrement = float (dy)/float (steps), SetPixel (round (x), round (y)); for (k = 0;k<steps;k++) {    x + = Xincrement;
   y + = Yincrement;setpixel (round (x), round (y));}}

The D-Da method calculates the pixel position more quickly than using the straight line equation (3.1) . It uses grating characteristics to eliminate multiplication in the linear equation (3.1), and to use the appropriate increment in the X or Y direction, thus gradually getting the positions of each pixel along the path . But in the continuous superposition of floating-point increment, the accumulation of rounding error causes the pixel position calculated for the longer segment to deviate from the actual segment. And the rounding and floating-point operations in the process are still time consuming. We can improve the performance of the DDA algorithm by separating the increment m and 1/m into integers and fractional parts, so that all calculations are simplified to integer operations. The method of calculating 1/m increments with integer steps is discussed later. In the next section, we consider a more general Scan line program that can be used for both straight lines and curves.

Computer Graphics (ii) output graphic element _3_ algorithm _2_dda algorithm for line drawing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.