The arithmetic of digital differential analysis drawing line in computer graphics
Modern computer graphics, our programmers often encounter drawing programming problems, and now there are a lot of drawing API, in other words, the graphical API is very rich, from TC graphic to Windows gdi/gdi+, as well as the cross-platform open standard OpenGL. These APIs provide basic drawing functions such as drawing lines, circles, and ellipses.
The algorithm for drawing straight lines is also called linear rasterization, we know that our computer display screen is actually a pixel composition, the drawing algorithm is to approximate the line of pixels on the color output.
is to select a direction where the ends of the line change a lot, starting from the starting point and recursively getting the result of the coloring.
The flowchart of the DDA algorithm is as follows:
With the flowchart, I believe that the algorithm is relatively simple to write, the following function is drawn with the GDI setpixel function coloring.
void Cglinedda (int x1,int y1,int x2,int y2,hdc hdc,colorref color) {int nlenght = 0;IF (ABS (X2-X1) >= abs (y2-y1)) {Nlengh T = ABS (X2-X1);} Else{nlenght = ABS (Y2-Y1);} Select the iteration unit size FLOAT dx = (x2-x1)/float (nlenght); float dy = (y2-y1)/float (nlenght); float x = x1 + 0.5;float y = y1 + 0.5;int I = 1;while (i <= nlenght) {SetPixel (HDC, (int) x, (int) y,color); x + = Dx;y + = Dy;i + = 1;}}
The arithmetic of digital differential analysis drawing line in computer graphics