The so-called entity generation refers to the transformation of the parameter representation of the finished entity (as specified by the user of the graphics package) to the bitmap representation (the representation required by the raster Display system refresh). Usually also referred to as a scan transformation entity.
Scan transformation of a line: determines the set of pixels that are best approximated to the line, and writes the pixels in the order of scan lines.
Three commonly used algorithms: 1, numerical differential method dda;2, Midpoint drawing line method, 3, Bresenham algorithm.
Create a target to get a set of pixels that are sufficiently close to the line segment
The precondition of generation: 1, the pixel grid is uniform, the coordinate is integer value, 2, the line segment width is 1, 3, the line segment slope k value range is [ -1,1].
1. Numerical differential method1.1 Basic Ideas
The straight segment of the P0 (x0, y0), P1 (x1, y1) is known to have a line slope of L: y=kx+b
, X starts at the left endpoint x0, and the right endpoint X1 in step =1 (pixels), calculates the corresponding y-coordinate y=kx+b, and takes the pixel point (x, Y (x)) as the coordinates of the current point.
Calculate yi+1 = Kxi+1+b
= K1XI+B+KDX
= YI+KDX when dx = 1; Yi+1 = Yi+k.
1.2 Analysis
- When x increments by 1,y increments K(that is, the linear slope);
- Note that the above analysis algorithm only applies to | k| The situation of ≤1. In this case,x increments by 1 andy increases by up to 1.
- When | k| >1, you must swap x,y position
- It is divided into 8 quadrants according to the direction of the line from (X0,y0) to (x1,y1). For a straight line in the 1th a quadrant, Dx=1, Dy=k. For a straight line in the 1b quadrant, the value Dy=1, dx=1/k.
The corresponding relationship is as follows:
2. Midpoint Drawing Line Method2.1 Basic Ideas
The current pixel point is P (XP, YP), and the next pixel is P1 or P2. Set m= (xp+1, yp+0.5), the midpoint of P1 and P2, Q is the intersection of the ideal line and the x=xp+1 perpendicular. Compare Q to the y-coordinate of M.
As shown in the following:
When M is below the Q, then the P2 should be the next pixel point;
When M is above the Q, the P1 should be taken as the next point.
2.2 Analysis
Construction discriminant:d=f (M) =f (xp+1,yp+0.5) = A (xp+1) +b (yp+0.5) +c
among them a=y0-y1, b=x1-x0, C=x0y1-x1y0
When the d<0,m is under the L (Q Point), take the top right PU as the next pixel;
When the d>0,m is above the L (Q point), the right PD is the next pixel;
When d=0, choose P1 or P2 can, agreed to take PD as the next pixel;
D is the linear function of XP, YP, so it can be used in increment calculation to improve operation efficiency. Initial value d0=f (x0+1, y0+0.5) =a+0.5b.
(1) If the current pixel is in the d>=0 case, then take the positive right pixel PD (xp+1, YP), to be sentenced to the next pixel position, it should be calculated: Di+1=f (xp+2, yp+0.5) =a (xp+2) +b (yp+0.5) =d+a;
(2) If d<0, then take the upper right pixel pu (xp+1, yp+1). To determine the next pixel, calculate: di+1= F (xp+2, yp+1.5) =a (xp+2) +b (yp+1.5) +c=d+a+b, and increment to a+b.
Draw line starting from (x0, y0), the initial value of D
D0=f (x0+1, y0+0.5) =f (x0, y0) +a+0.5b =a+0.5b.
You can use 2d instead of D to get rid of decimals and improve efficiency.
2.3 Example Demo
The line of P0 (0,0), P1 (5,2) is drawn by the midpoint drawing method.
First, the discriminant formula is constructed:d = A (xp+1) +b (yp+0.5) +c . where a = Y0-y1 = -2,b = x1-x0 = 5,c = x0y1-x1y0 = 0.
Then the initial value D0 = a + 0.5b = 0.5. Take the point P ' (1,0) to the position of the next pixel. According to the rules (1) and (2), the following table of results can be obtained:
I |
Xi |
Yi |
D |
1 |
0 |
0 |
0.5 |
2 |
1 |
0 |
-1.5 |
3 |
2 |
1 |
1.5 |
4 |
3 |
1 |
-0.5 |
5 |
4 |
2 |
2.5 |
expressed in coordinates:
3. Bresenham algorithm3.1 Basic Ideas
A set of virtual gridlines is constructed across rows of pixel centers. Calculates the intersection of the line and each vertical gridline in the order of the line from the start point to the end point, and then determines the nearest pixel in the column's pixel to that intersection, based on the symbol of the error item.
3.2 Analysis
Set the linear equation to: , where k=dy/DX. Since the starting point of the line is in the center of the pixel, the initial value of the error item D d0=0.
for each increment of 1, the value ofD increments the slope value of the line by K, i.e. d=D+K. Once d≥ 1, subtract 1, so that D is between 0 and 1.
When D≥0.5, closest to the upper right pixel of the current pixel (XI+1,yi+ 1)
When D<0.5, it is closer to the right pixel (XI+1,Yi).
For ease of calculation, e=d-0.5,e has an initial value of-0.5, and the increment is K.
When e≥ 0 o'clock, take the pixel at the topright of the current pixel (Xi, Yi) (Xi+1,yi+ 1);
When e<0, it is closer to the right pixel (XI+1,Yi).
3.3 Example Demo
Use the Bresenham algorithm to draw the P0 (0,0), P1 (5,2) line.
The calculated slope k = DY/DX = 0.4.
The result is the following table:
I |
X |
Y |
E |
1 |
0 |
0 |
-0.5 |
2 |
1 |
0 |
-0.1 |
3 |
2 |
1 |
0.3 |
4 |
3 |
1 |
-0.3 |
5 |
4 |
2 |
0.1 |
6 |
5 |
2 |
-0.5 |
Coordinate representation:
The Bresenham algorithm uses decimal and division when calculating the slope and error terms of a line. You can use integers to avoid division. Because the algorithm uses only the symbol of the error term, it can be replaced as follows
So the initial value of E ' is-DX, because of K=DY/DX, so the increment of E ' becomes 2*dy.
Advantages of the 3.4 Bresenham algorithm
- You do not have to calculate the slope of the line, so do not divide;
- No floating-point numbers, only integers;
- Only integer plus subtraction and multiply 2 operations, and multiply 2 operations can be implemented with hardware shift.
- The Bresenham algorithm is fast and suitable for hardware implementations.
Straight Line Generation algorithm