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/34507.4792 . W -:914.9354394507.9438.4393:0/ *3.0 .7005038. *3.9438.

3/40750 ..3.0-09003/0..3/90:5507.2 84792 W &8084339007.90 . .88:23 03908450 /:5507 /407 .447/3.:.70803.9438 W &808/89.

.2047  W 4/40/0.:0 W 40..307747..3  084:/..447.0307..90:8039007 .07 98002800 7089:8314.:0 1 9007747.3/70809 9007747..9903080398453:5.935439 $4:943 2:95-498/08-849.98 1470.425..4:239490309 .470803.3/.:0824709.909007747 .4:23 9./ .447390019 248950 W %03 14790309./0 03431742430.447 .//.:2:.7/81742019 9479 W $9...447908.79-../0.2 478 W $:554809.:0 0 .0941:704:909070.:0  90780 .784383890.

08450 7010.7 W 130.088.!439 .3// / 44750   095 / / 90.70803.3/5  5 / 90780 903095439945498     ...9$905 /9208 W $9.9..8   !70 .7939  15 903903095439945498    .2 84792      35:990943003/54398.:. .3/8947001903/5439.3/!439130.9 W 1890058450 15.:08/ / /..9090.439030 89.3/ .3/5  5 / /  #050.

709070  "30.709070 47 .9354394507.314.8905 42.9438.9438.94383 70803.2 84792 "30.3390074507.:2-0741 507.8905 42...

4470/ .7090508 .7090.793.2 84792. 9208905 W .3/03/3..3994/7.-05.7...9.90..7090.950   W .950   .9.250 W $:554800.:0841 5 /.9.4792 /  /  5 / / 9           5      !            / / /  !            ..:0841 90.47/394 70803.2 8.70803.3089.. .3// W .