Correct draw line algorithm: However, the second use of an integer variant is not understanding / * BRESENHAM algorithm
The Bresenham algorithm is the widest range of linear scanning conversion algorithms in the field of computer graphics. Still assume that the line slope is between 0 and 1, the method is similar to the mid-point method, and the next pixel point is determined by an error term symbol.
The algorithm principle is as follows: a set of virtual grid lines are constructed by each column center of each line. Press straight line from the start-up point to the end point to calculate the intersection of the vertical grid lines, and then determine the nearest pixels in this column pixel. The integrity of this algorithm is to adopt incremental calculations, so that for each column, as long as the symbol of an error term can be checked, the pixel of the column can be determined.
As shown in Figure 2.1.4, a straight line equation is yi 1 = yi k (xi 1-xi) k. Assuming that the column coordinate pixels have been determined as xi, its row coordinates are YI. Then the list of the next pixel is xi 1, and the coordinates are either yi, or add 1 Yi 1. Whether to increase 1 depends on the value of the error term D. The initial value D0 = 0, the X coordinate is increased by 1, and the value of D = D K is increased accordingly. Once D ≥ 1, subtract 1. This ensures that D is between 0, 1. When D ≥ 0.5, the straight line and the vertical line X = Xi 1 intersection is close to the top right of the current pixels (Xi, yi) (XI 1, yi 1); while when D <0.5, closer to the right pixel (Xi 1, yi) ). For convenience, the initial value of E = D-0.5, E is -0.5, and the increment is k. When E ≥ 0, the top right of pixels (xi, yi) is taken (xi 1, yi 1); and when E <0, take (xi, yi) right pixels (Xi 1, yi).
Figure 2.1.4 Geometric meaning of error item used by Bresenham algorithm
* /
// BRESENHAM portrait algorithm:
Void Bresenhamline (int X0, int y0, int x1, int y1, int color)
{INT X, Y, DX, DY;
Float K, E;
DX = X1-X0; DY = Y1-Y0; K = DY / DX; // DX, DY is the total number of straight lines. k is the number of slope E = -0.5; x = X0; Y = Y0; // x, y is the starting point. E is the number of adjustments to make the problem become> = 0, or the problem with <0. / * For convenient calculations, let E0 = -0.5, E I 1 = Di 1-0.5, the increment is k. When Ei 1 ≥ 0, the upper right upper right upper right upper right (XI 1, yi 1) of the pre-pixel (Xi, Yi) is taken; when E i 1 <0, it is closer to the right pixel (Xi 1, yi). * /// Here we use K = DY / DX slope, rather than using the above-by-one method. // Real number Yi value> = 0.5, then the Y (integer) 1 pixel, <0.5, the Y axis is still using the Y coordinate of the previous pixel. // Once, use Y 1, then the real increment of our yi Y-axis will be 1 this increment. So, once Y , you have to yi -. / / Here, in order to make us not judge whether it is> = 0.5, it is changed to judge whether it is> = 0, we need to all Yi-0.5. // This actually only needs yi-0.5. Because YI is a continuous increment - k. / / Here we use float e to assign E = -0.5, and E = E K later, so that it actually implements the purpose of E-0.5. For (i = 0; i IF (e> = 0) {y ; e = e-1;} } } / * Example: Use the Bresenham method to scan the conversion connection between two points P0 (0) and P1 (5, 2). I x y E0 - 0 0 -0.5 1 -0.1 1 0 -0.1 2 1 -0.7 3 1 -0.3 4 2 -0.9 Figure 2.1.5 Bresenham algorithm 5 2 -0.5 The above Bresenham algorithm uses the decimal and division when calculating the line slope and the error term. You can use an integer to avoid division. Since only the symbols of the error items are used in algorithms, they can be replaced: 2 * e * dx. Improved BRESENHAM line algorithm: * / void interbresenhamline (int X0, int y0, int x1, int y1, int color) {INT X, Y, DX, DY; INT K, E; DX = X1-X0; DY = Y1- Y0; E = -dx; x = x0; y = Y0; For (i = 0; i {DrawPixel (X, Y, Color); X ; E = E 2 * DY; // is probably calculated using a method of subtracting the absolute increment of X with a absolute increment of X of the Y. // If you don't know how to do it! ! ! ! ! IF (e> = 0) {y ; e = e-2 * dx;} } } Good 9CBS certified blog expert data analysis Pytorch Neural Networks Senior kicked people, proficient in Python, C, Java, JavaScript, Linux kernel, network protocol, virtualization, cloud computing, distributed storage, kubernetes, data warehousing and other technologies. Good at micro service architecture design, Devops. Currently, it is committed to assisting Pleura Finance with Fintech.