The arithmetic of digital differential analysis drawing line in computer graphics

Source: Internet
Author: User

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

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.