Derivation Bresenham line drawing algorithm

Transfer: https://www.cnblogs.com/soroman/archive/2006/07/27/509602.html

Previously seen Bresenham line drawing algorithm, the direct use of them, not to derive it, recently, referring to some information, special finishing its algorithm derived as follows. Prawn If you know the details, quickly flashed, do not waste time.

Bresenham line drawing algorithm substantially idea is as follows:

// Assume that the segment located in the first quadrant the slope is greater than 0 and less than 1, the starting point is set (x1, y1), end point (X2, Y2).
// By symmetry, the line segments may be derived to the whole quadrant.
1 draw a starting point (x1, y1).
2. ready to draw the next point. x coordinate by 1, it determines if the end point is reached, is completed. Otherwise, seen from the drawing, the next to be drawn point either to the right neighbor of the current point, or upper right neighbor of the current point.
2.1. If the line ax + by + c = the intersection of 0 and x = x1 + 1 of the y-coordinate is larger than the point M y coordinates, then the next point of the U-(X1 +. 1, Y1 +. 1)
2.2. otherwise, the next point B (X1 +. 1, Y1)
3. Videos point (U or B) .
4. jump back to step 2.
5. end.
Here Insert Picture Description

Here is how to determine the need to refine the next point to be drawn to the right neighbor of the current point or the current point in the upper right neighbor.

Line segment equation: ax + by + c = 0 (x1 <x <x2, y1 <y <y2)
Order dx = x2-x1, dy = y2-y1
is: slope -a / b = dy / dx.

Starting from the first point, we have F. (X,. 1, Y1) = A X1 B + Y1 + C = 0
the following requirements line ax + by + c = 0 and the intersection point x = x1 + 1 is:
a a * ( x1 + 1) + b * y + c = 0, obtains the intersection coordinates y = (- ca (x1 + 1)) / b
so that the y coordinate of the intersection point M and the difference Sub1 = (-ca (x1 + 1 )) / b - (y1 + 0.5) = -a / b-0.5, i.e., at the beginning of Sub1 is -a / b-0.5.

When the conditions have to be Sub1 = -a / b-0.5> 0 time, i.e., the next point to U.
On the contrary, the next point B.
substituting a / b, then Sub1 = dy / dx-0.5.

Because it is to be determined Sub cycles, so you have determined the expression Sub cycle, we can find the expression of the difference Sub following requirements x Sub = x1 + 2 when, i.e., Sub2
1. If the next adjacent to the upper right point is the next point of the point, the
Sub2 = (-ca (X1 + 2)) / B - (Y1 + for 1.5) = -2a, 3,4,5 / B - for 1.5
Sub1 = -2a, 3,4,5 - Sub difference so Dsub = Sub2 / b - 1.5 - (-a / b-0.5) = -a / b - 1. substituting a / b = Dy obtained Dsub / DX -1;
2. If the next point is a point adjacent right next point,
Sub2 = (-ca (x1 + 2 )) / b - (y1 + 0.5) = -2a / b - 0.5
so that the difference Sub Dsub = Sub2 - Sub1 = -2a / b - 0.5 - (-a / b-0.5 .) = -a / b substituting a / b have Dsub = dy / dx;

Thus, we have an initial value at the Sub Sub1 = -a / b-0.5 = dy / dx-0.5, there has been a difference of Sub expression Dsub = dy / dx -1 (when Sub1> 0) or dy / dx (when Sub1 <0). refinement completed.

Thus pcode can be refined as follows:

The Bresenham Line for the Pcode //
// By SoRoMan
X = X1;
Y = Y1;
DX = X2-X1;
Dy = Y2-Y1;
Sub = Dy / DX-0.5; // initial value, the next point to be drawn and the difference between the midpoint

DrawPixel (x, y); // Videos starting
the while (X <X2)
{
X ++;
IF (Sub> 0) // next point to be drawn to the upper right of the current point adjacent point
{
Sub + Dy = / DX -. 1 ; // the next point to be drawn and the difference between the midpoint
y ++; // y required by the upper-right neighbor. 1
}
the else // next point to be drawn to the right neighbor of the current point
{
Sub = Dy + / DX;
}
// draw the next point
DrawPixel (X, Y);
}

PS: general optimization:
In order to avoid to integer and fractional division operation, since the only used for Sub negative determination, it is possible to make 2 = Sub DX Sub-DX = 2DY, the
corresponding DSub = 2dy - 2dx or 2dy.

Thinking 1: If the Sub = 0, will have to take two points can be a problem. This problem has not yet deeply.

Guess you like

Origin blog.csdn.net/QIJINGBO123/article/details/93785833