Author: Zhu Jincan
Source: http://blog.csdn.net/clever101/
The DDA algorithm and bresenham algorithm are two common algorithms used to draw straight lines in computer graphics. This article describes how to implement the DDA algorithm and bresenham algorithm. The DDA algorithm is derived based on the linear formula Y = kx + B. The key is how to set the unit step, that is, the unit step in one direction, the step in the other direction must be smaller than 1. The specific idea of the algorithm is as follows:
1. Enter the start and end points of the straight line;
2. Calculate the spacing in the X direction: △x and y: △y.
3. determine the unit step, take maxsteps = max (△x, △y); If △x> = △y, the X direction is the unit step, and the X direction is one unit, step △y/maxsteps in the Y direction; otherwise, the opposite is true.
4. Set the pixel value of the first Vertex
5. Set the initial value of the loop to 1, set the number of cycles to maxsteps, define the variables X and Y, and perform the following calculation:
A. X adds one unit step, and y adds one unit step.
B. Set the pixel value at (x, y ).
The specific implementation code is as follows:
// @ Brief floating point to integer macro <br/> # define floattointeger (fnum) (fnum> 0 )? Static_cast <int> (fnum + 0.5): static_cast <int> (fNum-0.5) <br/> /*! <Br/> * @ brief DDA draw line function <br/> * @ Param PDC [in] window DC <br/> * @ Param beginpt [in] Straight Line start point <br/> * @ Param endpt [in] linear end point <br/> * @ Param linecor [in] linear color <br/> * @ return none <br/> * /<br/> void cdrawmsg:: dda_drawline (CDC * PDC, cpoint & beginpt, cpoint & endpt, colorref linecor) <br/>{< br/> long ydis = (endpt. y-beginpt. y); <br/> long xdis = (endpt. x-BeginPt.x); <br/> long maxstep = max (ABS (xdis), ABS (ydis); // Number of walking steps <br/> float fxunitlen = 1.0f; // step x <br/> float fyunitlen = 1.0f; // step Y <br/> fyunitlen = static_cast <float> (ydis) /static_cast <float> (maxstep); <br/> fxunitlen = static_cast <float> (xdis)/static_cast <float> (maxstep ); <br/> // set the start pixel color <br/> PDC-> setpixel (beginpt. x, beginpt. y, linecor); <br/> float x = static_cast <float> (beginpt. x); <br/> float y = static_cast <float> (beginpt. y); <br/> // cyclic step <br/> for (long I = 1; I <= maxstep; I ++) <br/>{< br/> X = x + fxunitlen; <br/> Y = Y + fyunitlen; <br/> PDC-> setpixel (floattointeger (x ), floattointeger (Y), linecor); <br/>}< br/>
Bresenham is an improved algorithm for DDA algorithm. In essence, it also adopts the idea of stepping. However, it is optimized compared with the DDA algorithm to avoid floating-point calculation during step-by-step operations, and provides a good idea for selecting points that conform to the linear equation. First, the slope of the straight line determines whether to perform the Unit Step in the X direction or the y direction: when the absolute value of the slope k | K | <1, the Unit Step in the X direction; when the absolute value of slope k is | K |> 1, the unit step is performed in the Y direction.
The mathematical basis for deriving the bresenham Algorithm Based on | K | <1 is as follows:
See, there is a known line y = kx + B, | K | <1. We determine the X direction as the unit step through the slope. When x = XM, y = ym. So when x executes a unit step (that is, when x = XM + 1), is Y equal to ym or ym + 1 more consistent with this linear equation? It is difficult for us to draw a conclusion with the naked eye, the best way is to compare the difference between the y values of ym and ym + 1 and the real equation (that is, yreal = K * (XM + 1) + B ), let's see which one is closer to the Y value of the real equation.
We set
Dupper = ym + 1-yreal = ym + 1-K * (XM + 1) + B); represents the difference between ym + 1 and the true values of the equation
Ddown = yreal-ym = K * (XM + 1) + B)-ym; represents the difference between ym and the real values of the equation.
That is, we need to compare the dupper and ddown sizes. Hypothesis
Diff = dupper-ddown = (ym + 1-K * (XM + 1) + B)-(K * (XM + 1) + B)-ym)
Let △x be the spacing of Line X direction, and △y be the spacing of line y direction.
PM = △x * diff = 2 * △x * Ym-2 * △y * Xm-2 * △y-△x * (2b-1 );
PM + 1 = PM + 2 * △x * (ym + 1-ym)-2 * △y;
The ym + 1-ym value is 0 or 1, depending on the PM symbol.
According to the equation diff = dupper-ddown = (ym + 1-K * (XM + 1) + B)-(K * (XM + 1) + B)-ym) and k = △y/△x, we can get the P0 value of the starting pixel (x0, y0) parameter:
P0 = △X-2 * △y;
Similarly we launched | K |> 1, Qm = 2 * XM * △Y-2 * ym * △x + (2b-2) * △x + △y;
QM + 1 = QM + 2 * (XM + 1-xm) * △Y-2 * △x;
XM + 1-xm is equal to 0 or 1, depending on the Qm symbol.
The first parameter q0 = △Y-2 * △x;
After understanding the mathematical principles, we can quickly determine the algorithm steps:
1. Enter the start and end points of a line segment.
2. Determine whether the slope of a line segment exists (that is, whether the X coordinates of the start point and end point are the same). If the slope is the same, the slope does not exist,
You only need to calculate the Unit Step in the Y direction (△y + 1 time), and the coordinates in the X direction remain unchanged to draw a straight line.
3. Calculate the slope k of a line segment, which is divided into the following situations:
A. k is equal to 0, that is, the line segment is parallel to the X axis, that is, the program only needs to calculate the Unit Step in the X direction, and the value in the Y direction remains unchanged.
B. | K | equal to 1, that is, the Unit Step in the X direction of the line segment is the same as that in the Y direction. The X and Y coordinates are calculated by directly repeating △x.
4. Determine whether the Unit Step in the X and Y directions is 1 or-1 based on the size of the X and Y coordinates of the input start and end points.
6. Draw the first vertex.
7. If | K | <1, set m = 0, calculate P0. If PM> 0, the next point to be drawn is (XM + unit step, ym ),
PM + 1 = PM-2 * △y;
Otherwise, the point to be drawn is (XM + unit step, ym + unit step)
PM + 1 = PM + 2*△X-2 * △y;
8. Repeat Step 7 △X-1 times;
9. If | K |> 1, set m = 0 and calculate q0. If QM> 0, the next vertex to be drawn is (XM, ym + unit step ),
PM + 1 = PM-2 * △x;
Otherwise, the point to be drawn is (XM + unit step, ym + unit step)
PM + 1 = PM + 2*△Y-2 * △x;
10. Repeat Step 1 △Y-1 times;
References:
1. Computer Graphics, Author: (United States) Donald Hearn (United States) M. Paine Baker Translator: Cai Shijie, Song jiqiang, Cai min