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

Source: Internet
Author: User

DDA algorithm
? ? ? ? Digital differential Analyzers(Digital differential Analyzer, DDA) method is a line-scan transformation Algorithm。 Based on the &x or &y calculated using the equation (3.4) or equation (3.5).

The

Samples the segments at unit intervals on an axis to determine the corresponding integer values that are closest to the line path on an axis. First, consider the line segment with positive slope as seen in 3.6. Like what. If the slope is less than or equal to 1. Samples are taken at the unit x interval (&x = 1) and each Y value is computed one by one:

??? subscript K rounding value, starting from the first point 1 increments up to the last endpoint. Because M can be a random real number between 0 and 1. So 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:








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).
This 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 end positions are assigned to the parameters dx and dy. An absolute large number of parameters determines the value of the parameter steps. Start with the pixel location (x0, y0) to determine the offset required for each step in the line segment to generate the next pixel position. and cycle the above process steps times.

If the absolute value of the DX is greater than the value of dy, and 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 larger, but the x0 is greater than xend. Then we use decrement-1 and-m 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));}}

da Method calculates the pixel position more quickly than the straight line equation (3.1) .

It uses the raster attribute to eliminate multiplication in the line equation (3.1) and to use the appropriate increment in the X or Y direction. Thus, the path along the route gets the position of each pixel . 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, thus simplifying all computations to integer operations. The will later discuss the method of calculating 1/m increments with integer steps. In the next section. We consider a more general Scanning line program that can be used for both straight lines and curves.

Computer graphics (ii) OUTPUT element _3_ algorithm _2_dda algorithm

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.