This article is to Zhao Ming teacher "computer graphics" MOOC course part of the Chapter Small Summary.
The straight line is the basis of composing the graph, its algorithm is often called many times, its good or bad directly affects the graph display effect and the speed. Here are some common algorithms for drawing straight lines.
1. DDA algorithm:
This algorithm is based on incremental thinking.
For the oblique truncation of the line: Y=kx+b, consider each x increment 1, there is y[i+1] = Y[i] + K, so that the KX part of the multiplication into a recursive addition.
Since the pixel points are integer coordinates, every y is evaluated to take the whole operation, using the method of adding 0.5 to the integer part: (int) (y[i]+0.5).
However, when |k| > 1 o'clock, as x increments by 1 each time, the grating sampled points are diluted, which may not be enough to approximate the ideal linear line in the mathematical sense.
The efficiency of this algorithm is: floating-point number addition level.
2, Midpoint drawing line method:
This algorithm is still based on incremental thinking.
For the general formula of a straight line: ax+by+c=0, when |k|<1, consider each x increment 1,y[i+1] can only be equal to y[i] or y[i]+1, it depends on the midpoint error item judgment.
How can I tell if q is above or below m? Put M-point into straight line general type:.
So the basic principle of the midpoint drawing line method is:
The next key is to derive the recursion of D to convert the multiplication operation to the addition operation.
When D0<0, that is, to fetch Pu, for the next point:
;
When D0>0, that is, Pd is taken, for the next point:
。
(d you can use 2d amplification to eliminate floating-point arithmetic) by simply judging its symbols.
3, Bresenham algorithm:
And the algorithm does not depend on the type of linear equation.
This increases the efficiency to the integer addition level.
and is not dependent on the form of the linear equation.
Computer graphics--Introduction to linear algorithm of grating graphics