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

Source: Internet
Author: User

Bresenham drawing Line Algorithm

Originally in Word there is a lower-right number, an editor in the CSDN editor will not be.

            

Bresenham line algorithm is a precise and efficient raster line generation algorithm proposed by Bresenham, which uses only incremental integer computation. In addition, the Bresenham algorithm can be used to display circles and other curves. Figure 3.8 and Figure 3.9 Show the part of the screen that draws the segment. The vertical axis represents the scan line position, and the horizontal axis identifies the pixel column. In this example, we sample at the unit x interval and need to determine which of the two possible pixel positions is closer to the line path each time the sample is sampled. Starting with the left endpoint in Figure 3.8, you need to determine whether the next sampled pixel position is (one, 1l) or (1 1, 12). Similarly, figure 3.9 shows a line with a negative slope for the pixel position (50,50) as the left endpoint. At this point, you need to determine whether the next pixel position is ((51, 50) or ((51, 49). To solve these problems, the Bresenham algorithm detects the symbol of the integer parameter, which is proportional to the offset between the two pixels and the actual segment.

to illustrate the Bresenham method, the scanning conversion process of a straight line with a slope of less than 1 is first considered, and the pixel position of the path along the route is determined by sampling at the unit x interval. Starting with the left end point (xo,yo) for a given segment, step through each successive column (x position) and draw a point on the pixel whose scan line y value is closest to the segment: Figure 3.10 shows step K of the process. If you have decided to display pixels in (XK, YK), then the next step is to determine which pixel to draw on the column xk+1 = xk+1, whether it is in position (xk+1, YK), or position (xk+1,yk+1)



at the sampling location xk+1, we use Dlower and dupper to identify the vertical offset of two pixels from the Math line path (see Figure 3.11), where the y-coordinate on the line at the pixel column position XK + 1 can be calculated as

so

And


to determine which of the two pixels is closer to the line path, you need to test the difference between the two pixel offsets:

by rearranging the equation (3.13), the decision parameter PK of step K of the draw line algorithm is obtained, thus only the integer is used for the calculation.
set Y and X as the vertical and horizontal offsets of the two ends, making m = y/x, defining the decision parameters as

The symbol of PK is the same as the symbol of Dlower-dupper (because of x >0 in the example), the parameter C is a constant with a value of 2 y + x (2b-1), which is independent of the pixel position and is eliminated when the PK is computed in the loop. If the pixels at YK are closer to the segment (i.e. Dlower < dupper) than the pixels in the yk+ 1, then the parameter PK is negative. At this point, draw the following pixels, and conversely, draw the pixels above.

the coordinates on the line vary by the unit step in the X or Y direction. Therefore, the successive decision parameter values can be obtained by the increment integer operation. In k+1 step, the decision parameters can be calculated from the equation (3.14):

subtracting equations (3.14) from the above equation, you can get

but xk+1 = xk+1, thus getting

wherein, Yk+1-yk takes 0 or 1, depending on the parameter PK symbol.
The recursive operation of the decision parameters is performed at the x position of each integer at the beginning of the coordinate end of the line segment, and the first parameter of the starting pixel position (xo,yo) PO is calculated by equation (3.14) and M = y/x

we can summarize the Bresenham algorithm for line segments with a positive slope of less than 1 as the following steps. The constant 2 y and 2 y-2 x are computed only once for each line scanned, so the system only computes integer addition and subtraction between the two constants.
:
|m| < 1 o'clock Bresenham algorithm for line drawing
1. The two endpoints of the transmission line, and the left endpoint is stored in (xo,yo);
2. Load (xo,yo) into the frame cache and draw the first point;

3. Calculate Constants x, Y, 2 y, and 2 y-2 x, and get the first value of the decision parameter:


4. Starting with k=0, the following tests are performed at each XK along the route path:

If pk<0, the next point to draw is (xk+1,yk+1), and


5. Repeat step 4, xk-1

Example 3.1 Bresenham drawing line algorithm
to demonstrate the above algorithm, we draw a line segment with a slope of (20, 10) and (30,18) that has an end point of 0.8 and

Then the value of the initial decision parameter number is


Calculates the increment of subsequent decision parameters to



The pixel points generated along this line path are shown in Figure 3.12.


The implementation of the Bresenham line algorithm with slope 0 < M < 1.0 is given in the following procedure. First, the endpoint pixel position of the segment is entered into the program, and then the pixels are drawn from the left endpoint to the right endpoint.

#include <stdlib.h> #include <math.h>/*bresenham line-drawing procedure for |m|<1.0. */void linebres (int x0,int y0,int xend,int yend) {    int dx = fabs (xend-x0), dy = Fabs (yend-y0);    int p = 2*DY-DX;    int twody = 2*dy, Twodyminusdx = (DY-DX);    int X,y;/*determine which endpoint to use as Start position.*/if (x0 > XEnd) {    x = xEnd;    y = yend;    XEnd = x0;} else{    x = x0;    y = y0;} SetPixel (x, y), while (x<xend) {  x++;if (P < 0)  p + = twody;else{  y++;  p + = TWODYMINUSDX;}  SetPixel (x, y);}}

by considering the symmetry between the eight and four regions of the XY plane, the Bresenham algorithm is universal for any slope segment. For a line segment with a positive slope and greater than 1.0, as long as the rules for the X and Y directions are swapped, that is, moving in unit stride along the y direction and calculating the contiguous X-value closest to the line path. Of course, you can also change the program so that it can start drawing pixels from any endpoint, and if the initial position of the positive slope line segment is the right endpoint, then x and Y will be decremented in the right-to-left step. To determine whether the same pixel can be drawn from any endpoint, Dlower = dupper When the candidate pixels are equal to the two vertical offsets of the segment, we always select the higher (or lower) pixels. For a line segment that draws a negative slope, the program is similar unless one of the coordinates is decremented and the other is incremented. Finally, the following special cases can be handled separately: horizontal lines (y = 0), vertical lines (x = 0), and diagonals | x| = | y|, they can all be loaded directly into the frame cache without the need for line-drawing algorithm processing.

Computer Graphics (ii) output graphic element _3_ line algorithm _3_bresenham line drawing 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.