1.DDA algorithm
DDA (Digital differential analyer): Numerical differential method
DDA algorithm thought: Incremental thinking
Formula derivation:
Efficiency: The use of floating-point addition and floating point display is required rounding
Code:
void LineDDA (int x0, int y0, int x1, int y1, int color) { int x; float dy, dx, y, m; DX = x1-x0; dy = y1-y0; m = dy/dx; y = y0; for (x = x0; <= x1; x + +) { putpixel (x, (int) (y + 0.5), color); Y + = m; }}
2. Midpoint Drawing Line Method
Adopt the general formula of straight line: ax+by+c=0
When K is in (0,1], each time the x direction is added in the direction of 1,y plus 1 or unchanged:
When Q is above M, the PU point is taken;
When Q is below M, take the PD point.
Next:
Then the calculation of the midpoint drawing line:
DI requires two multiplication and four addition calculation, which is much worse than DDA, but di can be obtained by incremental method
When d<0
When d>=0
The initial value of D is d0:
The midpoint algorithm is calculated as:
If D is replaced with 2d, only integer arithmetic is better than DDA algorithm.
Final formula:
Code:
void Linemidpoint (int x0, int y0, int x1, int y1, int color) { int x = x0, y = y0; int a = y0-y1, B = x1-x0; int cx = (b >= 0? 1: (b = B,-1)); int cy = (a <= 0? 1: (A = A,-1)); Putpixel (x, y, color); int d, D1, D2; if (-a <= B) //Slope absolute Value <= 1 { d = 2 * a + B; D1 = 2 * A; D2 = 2 * (A + b); while (x = x1) { if (d < 0) y + = cy, D + = D2; else D + = D1; x + = CX; Putpixel (x, y, color); } } else //slope absolute value > 1 { d = 2 * b + A; D1 = 2 * b; D2 = 2 * (A + b); while (Y! = y1) { if (d < 0) D + = D1; else x + = cx, D + = D2; Y + = cy; Putpixel (x, y, color);}}}
3.Bresenham algorithm
DDA has only one addition for each step of the drawing line.
The midpoint drawing line method makes the drawing line only one integer addition per step.
The Bresenham algorithm provides a more general algorithm to increase the scope of application.
The idea of the algorithm is to construct a set of virtual grid lines in the center of each row and column pixel, calculate the intersection point of the straight line and the vertical grid line according to the order of the line beginning to the end point, and then determine the pixel nearest to the node in the column pixel according to the symbol of the error item.
Assuming that the increment (minus) amount for each x+1,y is 0 or 1, it depends on the distance between the actual line and the nearest raster grid point, which has a maximum error of 0.5.
The initial value of the error term D 0=0,d=d+k, once d≥1, subtract 1, guaranteeing the relativity of D, and between 0 and 1.
The following formula is then calculated:
How to promote to Integer algorithm?
The answer is to let e=d-0.5
e0=-0.5
Once per cycle: E = e+k
if (e>0) then E = e-1
E+k This is still a floating-point addition
Because k= y/x
Because of the notation of the error term, you can replace E with e*2* x:
e0=-x
Once per cycle: E = e+2 y
if (e>0) Then E = e-2 x
This has become an integer addition
Algorithm steps:
The algorithm steps are:
1. Enter the ends of the line with P 0 (x 0,y 0) and P 1 (x 1,y 1).
2. Calculate the initial values x, Y, e=-X, x=x 0, Y=y 0.
3. Draw the point (x, y).
The 4.E is updated to E+2 y, which determines the sign of E. If e>0, the (x, y) is updated to
(x+1,y+1), and E is updated to e-2 x, otherwise (x, y) is updated to
(X+1,y).
5. Repeat steps 3 and 4 when the line has not finished drawing. otherwise end.
Code:
void lineBresenham1 (int x0, int y0, int x1, int y1, long color) { int dx = ABS (x1-x0); int dy = ABS (y1-y0); int x = x0; int y = y0; int stepx = 1; int stepy = 1; if (x0 > X1) //draw from right to left stepx =-1; if (y0 > y1) stepy =-1; if (dx > Dy) //forward along the longest axis { int e = dy * 2-DX; for (int i = 0; I <= DX; i++) { putpixel (x, y, color); x + = stepx; E + = dy; if (e >= 0) { y + = stepy; E-= dx ; }}} else { int e = 2 * dx-dy; for (int i = 0; I <= dy; i++) { putpixel (x, y, color); Y + = stepy; E + = dx; if (e >= 0) { x + = stepx; e-= dy;}}}
Computer graphics scanning Transformation Linear-dda,bresenham, midpoint drawing line algorithm