Lecture02 Bresenham Line Algo
Lecture02 Bresenham Line Algo
Pixel addressing in raster graphics and how does computer draw line?
Screen made of pixels High-level language specifies line System must color pixels
Pixel address y y+1 y+2 y+3 x x+1 x+2 x+3 x+4
Theoretical length
Pixel
Actual length
b y0 mx0
yend y0 y m xend x0 x
y mx x
y
m
For lines with slope |m|<1, x can be set to a proportional to small deflection value, y can be calculated from the formula For lines with slope |m|<1, y can be set to a proportional to small deflection value, x can be calculated from the formula
y k +1 = y k + m x k +1 = x k 1 + m
1 m
( x = 1) ( y = 1)
( x = -1) ( y = -1)
y k +1 = y k - m x k +1 = x k -
Y_inc
X_inc
DDA Algorithm
Start with starting and ending coordinates of the line:
(x0, y0) and (x1, y1)
Color first pixel (round to nearest integer) Suppose x1-x0 > y1-y0 (gentle slope)
There will be x1-x0 steps (# pixels to be colored)
For each step, round off x and y to nearest integer, and color pixel
DDA Pseudo-code
// assume that slope is gentle DDA(float x0, float x1, float y0, float y1) { float x, y; float xinc, yinc; int numsteps; numsteps = Round(x1) Round(x0); xinc = (x1 x0) / numsteps; yinc = (y1 y0) / numsteps; x = x0; y = y0; ColorPixel(Round(x),Round(y)); for (int i=0; i<numsteps; i++) { x += xinc; y += yinc; ColorPixel(Round(x),Round(y)); }
Q: For each step, how many floating point operations are there? A: 4 Q: For each step, how many integer operations are there? A: 2
DDA Example
Suppose we want to draw a line starting at pixel (2,3) and ending at pixel (12,8). What are the values of the variables x and y at each timestep? What are the pixels colored, according to the DDA algorithm?
numsteps = 12 2 = 10 xinc = 10/10 = 1.0 yinc = 5/10 = 0.5 t 0 1 2 3 4 5 6 7 8 9 10 x 2 3 4 5 6 7 8 9 10 11 12 y 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8 R(x) 2 3 4 5 6 7 8 9 10 11 12 R(y) 3 4 4 5 5 6 6 7 7 8 8
DDA Algorithm
Advantage
Does not calculate coordinates based on the complete equation (uses offset method)
Disadvantage
Round-off errors are accumulated, thus line diverges more and more from straight line Round-off operations take time
Perform integer arithmetic by storing float as integers in numerator and denominator and performing integer arithmetic.
Bresenhams Algorithm
Uses only integer calculations Uses distance between ideal y-coordinate and the upper and lower pixel (assuming gentle slope)
dupper dlower
Section of the screen grid showing a pixel in column xk on scan line yk that is to be plotted along the path of a line segment with slope O<m<l.
Distances between pixel positions and the line y coordinate at sampling position xk+ I.
y = m(x+1) + b y
d2
d1
x+1
5.
4
5 6 7 8 9 10
0
-10 0 -10 0 -10 0
6
7 8 9 10 11 12
5
6 6 7 7 8 8