The dotted-point bresenham algorithm raster draws a straight line (personal summary Lite version) with extremely short code! Extremely fast!

Source: Internet
Author: User

The midpoint bresenham algorithm rasterizes a straight line and only shifts one pixel at a time, which is highly accurate! This source code is converted using a straight line y = X, which simplifies the processing of the main displacement. Each movement only requires addition and subtraction without multiplication and division! Extremely fast !! At the end of this article, we will post the core code ~

Void bresenham_lineto (CDC * PDC, int X1, int Y1, int X2, int Y2) // raster the bresenham algorithm to draw a straight line {float K = 1.0 * (Y2-Y1) /(x2-X1); // slope int flag = 0; // whether to flip along y = x if (k> 1 | K <-1) {flag = 1; x1 ^ = Y1 ^ = x1 ^ = Y1; x2 ^ = Y2 ^ = x2 ^ = Y2; k = 1.0*(Y2-Y1)/(x2-X1 );} float d = 0.5-K; // initial value if (x1> x2) {x1 ^ = x2 ^ = x1 ^ = x2; Y1 ^ = Y2 ^ = Y1 ^ = Y2 ;} while (x1! = X2) // main displacement, each time the pixel is + 1 {If (k> = 0 & D <0) // forward Y1 ++, d ++; else if (k <0 & D> 0) // negative Y1 --, d --; D-= K; ++ x1; If (FLAG) PDC-> setpixel (Y1, X1, RGB (255, 0, 0); // flip the pixel else PDC-> setpixel (x1, Y1, RGB (255, 0, 0 ));}}

:

Principle:


Use a blue pixel to replace a red line

Each time you move one pixel in the main displacement (the main displacement in width and height), the other direction does not go depending on the midpoint deviation Discriminant Value.

Midpoint deviation Discriminant

F (x, y) = y-Kx-B = 0 where k = (y2-y1)/(x2-x1)

The relationship between the point and the line is determined based on the relationship between F (x, y) and 0 (<0, = 0,> 0 ).

For example. when the main displacement is X and 0 <k <1, The point is compared with the line by a (x + 1, Y), B (x + 1, Y + 1, then proceed accordingly

A (x + 1, Y), B (x + 1, Y + 1) midpoint bring deviation discriminant d = f (x + 1, Y + 0.5) = Y + 0.5-K * (x + 1)-B

If the dot of D <0 is below the straight line, and the next pixel is drawn in B, the value of D in the next step is d = f (x + 2, Y + 1.5) = Y + 0.5-k (x + 1)-B + 1-k = d + 1-K

If the dot of D> = 0 is above the straight line, and the next pixel is drawn in a, the next D value is d = f (x + 2, Y + 0.5) = Y + 0.5-k (x + 1)-B-k = D-K

Each d in the process can be obtained from the previous step through the addition and subtraction operation, the operation is fast!

The initial value of D is d = f (x + 1, Y + 0.5) = y + 0.5-k (x + 1)-B = Y-Kx-B-K + 0.5.

Where (X, Y) is the starting point, and D = 0.5-K is substituted into the linear y-Kx-B = 0.

The dotted-point bresenham algorithm raster draws a straight line (personal summary Lite version) with extremely short code! Extremely fast!

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.