Numerical differential (DDA) method
The linear segments of endpoint P0 (X0, Y0), P1 (X1, Y1) are L (P0, P1), and the horizontal coordinate X0 of the slope L of the slope L of the line segment L X0 to the endpoint P1 of the L Step, take a long = 1 (pixel), calculate the corresponding Y coordinate with the line equation Y = kx b, and take the pixel point (X, Round (Y)) as the coordinate of the current point. because:
P0
(
x0
,
Y0
),
P1
(
x1
,
Y1
The straight line segment is
L
(
P0
,
P1
), Straight segment
L
The slope of
L
Starting point
P0
Horizontal coordinates
x0
to
L
End point
P1
Horizontal coordinates
x1
Step, take a long time = 1 (pixel), use
L
Straight line equation
Y = kx b
Calculate the corresponding
y
Coordinate and taking pixel points
x
Round
y
)) As the coordinate of the current point. because:
Yi 1 = kxi 1 B
= K1XI B KDX
= yi kdx
So, when DX = 1; yi 1 = yi k. That is, when X is incremented by 1, Y increment k (ie, line slope). According to this principle, we can write a DDA line algorithm program.
DDA line algorithm
Void DDALINE (int X0, int y0, int x1, int y1, int color)
{INT X;
Float DX, DY, Y, K;
DX = X1-X0; DY = Y1-Y0;
K = DY / DX,; y = Y0;
For (x = x0; x {DrawPixel (X, INT (Y 0.5), Color; Y = y k; } } Note: We use integer variables to represent the color and grayscale of the pixel. Example: Scanning the two points P0 (0) and P1 (5, 2) line segments with DDA method scanning. x int (y 0.5) y 0.5 0 0 0 1 0 0.4 0.5 2 1 0.8 0.5 3 1 1.2 0.5 4 2 1.6 0.5 Figure 2.1.1 Scanning Conversion of Linear Segmentation Note: The algorithm of the above analysis is only suitable for | k | ≤1. In this case, X is increased by 1, and Y is up to 1. When | k |> 1, the X, Y position must be interchanged, and Y is 1 / k per increase, X corresponding 1 / k. In this algorithm, Y and K must be represented by floating point numbers, and each step is to be rounded and forth, which makes it not conducive to hardware implementation.