Вы находитесь на странице: 1из 25

Chapter 2

Line Drawing Algorithms


1. Cartesian Slope-intercept 2. DDA Algorithm 3. Bresenhams Line Algorithm

Scan converting lines:


A scan conversion algorithm for lines computes the coordinates of the pixels the lie on or near an ideal, infinitely thin straight line imposed on a 2D raster grid. In principle, we would like the sequence of pixels to lie as close to the ideal line as possible and to be as straight as possible. Consider a 1-pixel-thick approximation to an ideal line, it should have the following properties:
.

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

if x > y x = x +1 and calculate y for each step.

m=1

if y > x y = y +1 and calculate x for each step.

Line Drawing Algorithm


The Cartesian slope-intercept equation for a straight line is
y = m.x + b

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

and we can calculate and

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

DDA Algorithm (digital differential analyzer)


DDA is a scan conversion line algorithm based on calculating either y m= y or x let x 1. If slope less than or equal to 1 ( m 1 ), we sample at unit x intervals (x is increment by 1) and compute each successive y value
yi +1 = m.xi +1 + b, if we let xi +1 = xi + 1 = m( xi + 1) + b yi +1 = yi + m
y axis

xi + 1 = 2 xi +1 = 2 xi + 1 = xi +1

= m.xi + m + b = m.xi + b + m, yi = m.xi + b

x is increment by 1

each step of calculated y values must be rounded to the nearest integer

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
.

4. When m > -1, let y increment by 1


x axis yi +1 = yi + 1 1 xi 1 = xi + m

x
y axis

y
1 1 x = = slope = y m y x

Rounding of Real World to Computer Coordinate

( 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)
.

Bresenhams Line Algorithm

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

difference between these two separations is : d1 d 2 = m( xk + 1) + b yk yk 1 + m( xk + 1) + b d1 d 2 = 2m( xk + 1) + 2b 2 yk 1 d1 d 2 = 2m( xk + 1) 2 yk + 2b 1

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)
.

pk = 2x.m.xk + 2x.m 2x. yk + 2x.b x y y 2x. yk + 2x.b x xk + 2x pk = 2x x x pk = 2y.xk + 2y 2x. yk + 2x.b x

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 = 2y.xk 2x. yk + 2y + x(2b 1)

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

+ 2x( yk yk +1 ) inverse sign 2x( yk + yk +1 )

1 2 xi xi+1
.

Where the term yk+1 - yk is either 0 or 1, depending on the sign of parameter pk


y yk +1 yk = 1 yk +1 yk = 0

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
.

let ( x0 , y0 ) = (0,0), thus b = y0 m.x0 = 0 m.0 = 0 p0 = 2y.(0) 2x.(0) + 2y + x(2(0) 1) p0 = 2y 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

Bresenhams Line-Drawing Algorithm for |m|<1


1. Input the two line end points and store the left endpoint in (xk,yk). 2. Plot the first point. 3. Calculate constants x, y,2y, and 2y 2x, and obtain the starting value for the decision parameter as
p0 = 2y x

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 )

yk+1 and yk are the same position then yk+1 = yk

pk +1 = pk + 2y 2x( yk yk ) pk +1 = pk + 2y
.

otherwise (If pk 0 ), the next point to plot is (xk+1, yk+1) and


pk +1 = pk + 2y 2x( yk +1 yk )
y is increment by 1

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) -----------------------------------------------------------------------------------.

Вам также может понравиться