You are on page 1of 10

# How does computer draw line?

• Screen made of pixels • High-level language specifies line • System must color pixels

and color pixel . – increment x by (x1-x0)/numsteps. y0) and (x1.DDA Algorithm • Start with starting and ending coordinates of the line: (x0. round off x and y to nearest integer. and – increment y by (y1-y0)/numsteps • For each step. 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) • Set x=x0. y=y0 • At each step.

for (int i=0. float xinc. numsteps = Round(x1) – Round(x0). xinc = (x1 – x0) / numsteps. float y0. } } Q: For each step. ColorPixel(Round(x). ColorPixel(Round(x).Round(y)). yinc. int numsteps. i++) { x += xinc. y = y0.Round(y)). y. how many integer operations are there? A: 2 . how many floating point operations are there? A: 4 Q: For each step. x = x0. y += yinc.DDA Pseudo-code // assume that slope is gentle DDA(float x0. yinc = (y1 – y0) / numsteps. float y1) { float x. i<numsteps. float x1.

5 6 6.8). • What are the values of the variables x and y at each timestep? • What are the pixels colored.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.DDA Example • Suppose we want to draw a line starting at pixel (2. according to the DDA algorithm? numsteps = 12 – 2 = 10 xinc = 10/10 = 1.5 7 7.0 yinc = 5/10 = 0.5 5 5.5 4 4.3) and ending at pixel (12.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 (continued) Y_inc X_inc • … but floating point operations and rounding operations are expensive .

Bresenham’s Algorithm • Uses only integer calculations • Uses distance between ideal y-coordinate and the upper and lower pixel (assuming gentle slope) dupper dlower .

for each x value). add an error value. we should color y+1 and reset the error value. . • However. If the error value is more than 0. • Start by coloring the left-most pixel.General idea how Bresenham works • Suppose that the line is gently sloping upwards from left to right. multiply both sides by 2 so that we use integer comparisons instead. color y and accumulate the error value. we have to figure out whether we color the same y or y+1. it seems like we’re still using floating point – Solution.5. • How do we decide? – When going from one column to the next. • Then. for the next column (that is. Otherwise.

y0) Pre-calculate the values dx. flip y and x . 2dy and 2dy .2dx Color pixel (x0. 3. 2. 5. 4.y0) Let p0 = 2dy – dx At each xk along the line. yk + 1). the next point to plot is (xk + 1.yk). and pk+1 = pk + 2dy Otherwise. and pk+1 = pk + 2dy – 2dx 6. starting with k=0: If pk<0. then the next point to plot is (xk + 1. reflect • If steep slope.Bresenham’s Algorithm 1. Repeat Step-4 dx times • Switch Point 0 and Point 1 if necessary • If negative slope. dy. Input the two line endpoints and store left endpoint as (x0.

how many integer operations are there? A: 3 or 4 .Number of Operations in Bresenham’s Algorithm Q: In each step. how many floating point operations are there? A: 0 Q: In each step.

8). • What are the values of p0.Bresenham’s Algorithm Example • Suppose we want to draw a line starting at pixel (2. according to Bresenham’s algorithm? dx = 12 – 2 = 10 dy = 8 – 3 = 5 p0 = 2dy – dx = 15 t 0 1 2 3 p 0 -10 0 -10 P(x) 2 3 4 5 2dy = 10 2dy – 2dx = -10 P(y) 3 4 4 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 . dx and dy? • What are the values of the variable p at each timestep? • What are the pixels colored.3) and ending at pixel (12.

.  3.

   9                                        #            #            .

3/ 74:3/34507.4792 . W -:914.9354394507.9438.4393:0/ *3.0 .7005038. *3.9438.

3/40750 ..3.0-09003/0..3/90:5507.2 84792 W &8084339007.90 . .88:23 03908450 /:5507 /407 .447/3.:.70803.9438 W &808/89.

.2047  W 4/40/0.:0 W 40..307747..3  084:/..447.0307..90:8039007 .07 98002800 7089:8314.:0 1 9007747.3/70809 9007747..9903080398453:5.935439 \$4:943 2:95-498/08-849.98 1470.425..4:239490309 .470803.3/.:0824709.909007747 .4:23 9./ .447390019 248950 W %03 14790309./0 03431742430.447 .//.:2:.7/81742019 9479 W \$9...447908.79-../0.2 478 W \$:554809.:0 0 .0941:704:909070.:0  90780 .784383890.

08450 7010.7 W 130.088.!439 .3// / 44750   095 / / 90.70803.3/5  5 / 90780 903095439945498     ...9\$905 /9208 W \$9.9..8   !70 .7939  15 903903095439945498    .2 84792      35:990943003/54398.:. .3/8947001903/5439.3/!439130.9 W 1890058450 15.:08/ / /..9090.439030 89.3/ .3/5  5 / /  #050.

709070  "30.709070 47 .9354394507.314.8905 42.9438.9438.94383 70803.2 84792 "30.3390074507.:2-0741 507.8905 42...

4470/ .7090508 .7090.793.2 84792. 9208905 W .3/03/3..3994/7.-05.7...9.90..7090.950   W .950   .9.250 W \$:554800.:0841 5 /.9.4792 /  /  5 / / 9           5      !            / / /  !            ..:0841 90.47/394 70803.2 8.70803.3089.. .3// W .