Академический Документы
Профессиональный Документы
Культура Документы
1. For lines with slopes between -1 and 1 inclusive, exactly 1 pixel should be illuminated in each column; 2. For lines with outside [-1, 1], exactly 1 pixel should be illuminated in each row; 3. All lines should be drawn with constant brightness, independent of length and orientation, and as rapid as possible.
m=1
m=1
m representing the slope of the line b as the y intercept The two endpoints of a line are specified at position p ( x , y ), p ( x , y ) , we can determine values for the slope m and y intercept b with the following calculations:
1 1 1 2 2 2
.
y2 y1 , if y = y2 y1 , x = x2 x1 m= x2 x1
then
y m= x
b = y1 m.x1
y and x from m as y = mx
y x = m
.
Example
y2 y1 , b = y1 m.x1 yi = m.xi + b, m = x2 x1 p1 ( x1 , y1 ) = (2,2), p2 ( x2 , y2 ) = (6,5) 52 3 = = 0.75, b = 2 (0.75)2 = 0.5 m= 62 4 y axis = m.xi + b, increment x by 1 5 = (0.75)2 + 0.5 = 2 4 = (0.75)3 + 0.5 = 2.75 3 3 = (0.75)4 + 0.5 = 3.5 4 2 = (0.75)5 + 0.5 = 4.25 4 1 x axis = (0.75)6 + 0.5 = 5 1 2 3 4 5 6
.
yi yi yi yi yi yi
xi + 1 = 2 xi +1 = 2 xi + 1 = xi +1
x is increment by 1
1 2 xi xi+1
x axis
.
2. If lines with a positive slope greater than 1 ( m > 1 ), we reverse the roles of x and y. That is, we sample at unit y intervals (y is increment by 1) and compute each successive x value
yi +1 = yi + 1 (because y increment by 1)
1 xi +1 = xi + m
x axis
1 x = slope = y y x m >1
1 = m
x y
y axis
3. If this precessing is reversed, so that the starting endpoint is at the y right, then we have m = x and if m 1 , let x is decrement by 1 y axis
xi 1 = xi 1
yi +1 = yi + m
y
x axis
x
y m= x
.
x
y axis
y
1 1 x = = slope = y m y x
( xi + 1, Round ( yi + m))
( xi + 1, yi + m)
.
Example
p1 ( x1 , y1 ) = (5,2), p2 ( x2 , y2 ) = (2,6) x = x2 x1 = 2 5 = 3 y = y2 y1 = 6 2 = 4 yi +1 = yi + 1 1 xi 1 = xi + m yi = 2, xi = 5 yi +1 = 3, xi 1 = 5 0.7518 = 4.2482 4 yi +1 = 4, xi 1 = 4.2482 0.7518 = 3.4964 3 yi +1 = 5, xi 1 = 3.4964 0.7518 = 2.7446 3 yi +1 = 6, xi 1 = 2.7446 0.7518 = 1.9928 2
.
y axis
6 5 4 3 2 1 1 2 3 4 5
4 y = = 1.33 m= x 3
x axis
An accurate and efficient raster line-generating algorithm, developes by Bresenham, see the problem.
The next sample position whether to plot the pixel at position (11,11) or the one at (11,12)?. Assuming determined that the pixel at (x k ,y k ) is to be displayed, next need to decide which pixel to plot in column x k+ 1. Our choices are the pixels at positions (xk+1,yk) and (xk+1,yk+1)
.
12 11
10 10 11 12
yk+1 y yk
d2 d1
xk xk+1
y = m( xk + 1) + b then d1 = y y k
At sampling position xk+1, we label vertical pixel separations from the mathematical line path as d1 and d2. The y coordinate on the mathematical at pixel column position x k +1 is calculated as
and d 2 = ( yk + 1) y d 2 = yk + 1 m( xk + 1) b
d1 = m( xk + 1) + b yk
A decision parameter pk for the kth step in the line algorithm can be obtained by rearranging d1-d2, so that it involves only integer y calculations. We accomplish this by substituting m = x , where y and x are the vertical and horizontal separations of the endpoint positions, and defining:
pk = x(d1 d 2) pk = x(2m( xk + 1) 2 yk + 2b 1)
.
The sign of pk is the same as the sign of d1-d2, since x > 0 If the pixel at yk is closer to the line path than the pixel at yk+1 (that is, d1<d2), then decision parameter pk is negative. In that case, we plot the lower pixel; otherwise, we plot the upper pixel. Coordinate changes along the line occur in unit steps in either the x or y direction. Therefore, we can obtain the values of successive decision parameters using incremental integer calculations. At step k+1, the decision parameter is evaluated from pk as
.
pk +1 = 2y.xk +1 2x. yk +1 + 2y + x(2b 1) substracting pk +1 pk , we have pk +1 pk = 2y.xk +1 2x. yk +1 + 2y + x(2b 1) 2y.xk + 2x. yk 2y x(2b 1) pk +1 pk = 2y ( xk +1 xk ) + 2x. yk 2x. yk +1 pk +1 pk = 2y ( xk +1 xk ) + 2x( yk yk +1 ) pk +1 pk = 2y ( xk +1 xk ) 2x( yk + yk +1 ) pk +1 pk = 2y ( xk +1 xk ) 2x( yk +1 yk ) But xk +1 = xk + 1, ( x is increment by 1) pk +1 = pk + 2y ( xk + 1 xk ) 2x( yk +1 yk ) pk +1 = pk + 2y 2x( yk +1 yk ) xk +1 = 2 xk + 1 = 2
x is increment by 1
1 2 xi xi+1
.
This recursive calculation of decision parameters is performed at each integer x position, starting at the left coordinate endpoint of the line. The first parameter, p0, is evaluated from pk at the starting pixel position (x0, y0) and with m evaluated as y
x
.
Bresenham line drawing for a line with a positive slope less than 1 in the following listed steps. The constants 2y and 2y 2x are calculated once
4. At each xk along the line, perform the following test: If pk < 0, the next point to plot is (xk+1, yk) and
pk +1 = pk + 2y 2x( yk +1 yk )
yk+1 yk
y is increment by 1 or not
( xk , y k ) ( xk +1 , yk )
pk +1 = pk + 2y 2x( yk yk ) pk +1 = pk + 2y
.
yk+1 yk
( xk +1 , yk +1 ) ( xk , y k )
yk +1 = yk + 1
pk +1 = pk + 2y 2x( yk + 1 yk ) pk +1 = pk + 2y 2x
5. Repeat step 4,
x
times.
.
Example
p1 ( x1 , y1 ) = (20,10), p2 ( x2 , y2 ) = (30,18) x = 10, y = 8 The initial decision parameter has the value p0 = 2y x =6 and the increments for calculating successive decision parameter are
2y = 16, 2y 2x = 4
.
We plot the initial point (x0,y0)=(20,10), and determine successive pixel positions along the line path from the decision parameter as -----------------------------------------------------------------------------------k pk (xk+1, yk+1) k pk (xk+1, yk+1) 0 6 (21, 11) 5 6 (26, 15) 1 2 (22, 12) 6 2 (27, 16) 2 -2 (23, 12) 7 -2 (28, 16) 3 14 (24, 13) 8 14 (29, 17) 4 10 (25, 14) 9 10 (30, 18) -----------------------------------------------------------------------------------.