Line Drawing Algo
Line Drawing Algo
Output Primitives : basic geometric structures that form a complex picture structure ex) point,line,circle,filled-area,etc. Straight line : y = mx + b For given two end points(x1,y1),(x2,y2) For any given x internal x
m= y2 y1 x y = m x (1.a ) 1 x = y (1.b) m
y2 y1 m= , b = y1 mx1 x2 x1
m = -1/2 m = -1/3
m = 1/ 2 m = 1/3
m=0
m=0
xk +1 = xk + 1
yk +1 = yk + m
1 xk +1 = xk + m
yk +1 = yk + 1
xk +1 = xk 1
yk +1 = yk m
xk +1 = xk
1 m
yk +1 = yk 1
DDA Algorithm - Faster method than y = mx + b using but may specifies inaccurate pixel section - Rounding off operations and floating point arithmetic operations are time consuming.
(xk+1, yk+1)
4
(xk, yk)
3
(xk+1, yk)
2
For example, from position (2, 3) we have to choose between (3, 3) and (3, 4) We would like the point that is closer to the original line
5
y = m( xk + 1) + b
Deriving The Bresenham Line Algorithm (cont) So, dupper and dlower are given as follows:
and:
d lower = y yk = m( xk + 1) + b yk
d upper = ( yk + 1) y
= yk + 1 m( xk + 1) b
We can use these to make a simple decision about which pixel is closer to the mathematical line
Deriving The Bresenham Line Algorithm (cont) This simple decision is based on the difference between the two pixel positions:
= 2y xk 2x yk + c
Deriving The Bresenham Line Algorithm (cont) So, a decision parameter pk for the kth step along a line is given by: pk = x(d lower d upper )
= 2y xk 2x yk + c The sign of the decision parameter pk is the same as that of dlower dupper If pk is negative, then we choose the lower pixel, otherwise we choose the upper pixel
Deriving The Bresenham Line Algorithm (cont) Remember coordinate changes occur along the x axis in unit steps so we can do everything with integer calculations At step k+1 the decision parameter is given as:
Deriving The Bresenham Line Algorithm (cont) But, xk+1 is the same as xk+1 so: pk +1 = pk + 2y 2x( yk +1 yk ) where yk+1 - yk is either 0 or 1 depending on the sign of pk The first decision parameter p0 is evaluated at (x0, y0) is given as: p0 = 2y x
Bresenham Example
Lets have a go at this Lets plot the line from (20, 10) to (30, 18) First off calculate all of the constants: x: 10 y: 8 2y: 16 2y - 2x: -4 Calculate the initial decision parameter p0: p0 = 2y x = 6
k
0 1 2 3 4 5 6 7 8
20 21 22 23 24 25 26 27 28 29 30
pk
(xk+1,yk+1)
Comparing this to the DDA algorithm, DDA has the following problems:
Accumulation of round-off errors can make the pixelated line drift away from what was intended The rounding operations and floating point arithmetic involved are time consuming
Example :To illustrate the algorithm, we digitize the line with endpoints (20, 10) and (30, 18) Slope = 0.8 (x=10, y=8) Initial decision parameter: p0 = 2y x = 6 Increments:2y = 16, 2y 2x =4 Plot the initial point (x0, y0) = (20, 10) and determine successive pixel positions: k 0 1 2 pk 6 2 2 (xk+1, yk+1) (21, 11) (22, 12) (23, 12) k 3 4 5 pk (xk+1, yk+1) 14 (24, 13) 10 (25, 14) 6 (26, 15) k 6 7 8 pk (xk+1, yk+1) 2 (27, 16) 2 (28, 16) 14 (29, 17)
#include <stdlib.h> #include <math.h> /* Bresenhams algorithm for |m|<1.0 */ void lineBres( int x0, int y0, int xEnd, int yEnd ) { int dx = fabs(xEnd x0), dy = fabs(yEnd y0); int p = 2 * dy dx; int twoDy = 2 * dy, twoDyMinusDx = 2 * (dy dx); /* Determine which endpoint to use as start position */ if( x0 > xEnd ) { x = xEnd; y = yEnd; xEnd = x0; } else { x = x0; y = y0; } setPixel( x, y ); while( x < xEnd ) { x++; if( p < 0 ) p += twoDy; else { y++; p += twoDyMinusDx; } setPixel( x, y ); } }