# Mid Point Line Drawing Algorithm

Assist. Prof. Dr. Ahmet Sayar Computer Engineering Department Computer Graphics Course
Kocaeli University Fall 2012

How to Improve It?
y  mx  b

m

yH  yL xH  xL

2  40  1 6 3

(6,4)

x6 y4

(0,2)

Question: if we draw the current pixel, where is the next pixel? Next column, E or NE direction
2/110

y  1 ) ? • Is the line above or below 2 Below: move E Above: move NE 3/110 . determine whether we move E or NE on next step ( x 1.Midpoint Algorithm • Given a point just drawn.

4) (0.2) 4/110 .Above/Below the Line? (6.

Two Issues • How to evaluate if the midpoint is above or below the line? • How to incrementally evaluate this? 5/110 .

2) x 6/110 .7) (6.5) (0.Midpoint Algorithm – Implicit Forms •How do we tell if a point is above or below the line? y  mx  b y (3.

Midpoint Algorithm – Implicit Forms •How do we tell if a point is above or below the line? y  mx  b y ( x H  xL ) y  ( yH  yL xH  xL yH  yL xH  xL x b x  b)(xH  xL ) ( xH  xL ) y  ( yH  yL ) x  ( xH  xL )b ( xH  xL ) y  ( yL  yH ) x  ( xL  xH )b  0 7/110 .

y) above the line 8/110 .Midpoint Algorithm – Implicit Forms • How do we tell if a point is above or below the line? ( xH  xL ) y  ( yL  yH ) x  ( xL  xH )b  0 f ( x.y)>0 (x. y)  cx  dy  e c  yL  yH Properties d  xH  xL e  b( x L  x H ) f(x.y)=0 (x.y)<0 (x.y) on the line f(x.y) below the line f(x.

5) f (4.1)  18 x 9/110 . y)  6 y  3x 12 (0.4)  0 f (4.Midpoint Algorithm – Implicit Forms y  x2 3 6 y f (0.5) 18 f ( x.2) (6.

Two Issues • How to evaluate if the midpoint is above or below the line? • How to incrementally evaluate this? What about starting value? What is the middle point? (xL+1.yL+1/2) 10/110 .

Midpoint Algorithm What about starting value? (xL+1. y L )  c  1 d 2 2 ( xL . y L ) is on the line! f ( xL 1. y L  1 )  f ( xL . y L  1 )  c( xL 1)  d ( y L  1 )  e 2 2 f ( xL 1.yL+1/2) f ( xL 1. y L  1 )  f ( xL . y L  1 )  c  1 d 2 2 11/110 . y L )  c  1 d 2 2 f ( xL 1.

y L  1 )  c( xL 1)  d ( y L  1 )  e 2 2 f ( xL 1.Midpoint Algorithm What about starting value? f ( xL 1. y L  1 )  2c  d 2 Multiplying coefficients by 2 doesn’t change sign of f 12/110 .

y  1 ) if E chosen 2 13/110 .Midpoint Algorithm • Need value of f ( x 1. y  1 ) 2 – Find value of f ( x  2. y  1 ) to determine E or 2 NE • Build incremental algorithm • Assume we have value of f ( x 1.

y  1 )to determine E or NE 2 • Build incremental algorithm • Assume we have value of f ( x 1. y  1 ) 2 – Find value of f ( x  2.Midpoint Algorithm • Need value of f ( x 1. y  3 ) if NE chosen 2 14/110 . y  1 ) if E chosen 2 – Find value of f ( x  2.

y  1 )  c( x  2)  d ( y  1 )  e 2 2 f ( x  2. y  1 )  c  f ( x 1. y  1 ) 2 2 15/110 . y  1 ) 2 f ( x  2. find f ( x  2.Midpoint Algorithm If E was chosen.

y  3 )  c( x  2)  d ( y  3 )  e 2 2 f ( x  2. y  3 )  c  d  f ( x 1. y  1 ) 2 2 16/110 . find f ( x  2. y  3 ) 2 f ( x  2.Midpoint Algorithm If NE was chosen.

xL c=yL .Midpoint Algorithm – Summary x=xL y=yL d=xH .y) 17/110 .yH sum=2c+d //initial value draw(x.y) while ( x < xH) // iterate until reaching the end point if ( sum < 0 ) // below the line and choose NE sum += 2d y++ x++ sum += 2c draw(x.

xL c=yL .y) .Midpoint Algorithm – Summary x=xL y=yL d=xH .y) while ( x < xH) // iterate until reaching the end point if ( sum < 0 ) // below the line and choose NE sum += 2d y++ x++ sum += 2c draw(x.yH sum=2c+d //initial value draw(x.Update the function value of midpoint 18/110 .Update the current pixel .

y) while ( x < xH) // iterate until reaching the end point if ( sum < 0 ) // below the line and choose NE sum += 2d y++ x++ sum += 2c draw(x.Draw the pixel based on the function value of midpoint 19/110 .y) .Midpoint Algorithm – Summary x=xL y=yL d=xH .yH sum=2c+d //initial value draw(x.xL c=yL .

y) x 0 y  2 sum  2 (6.4) (0.Midpoint Algorithm – Example x=xL y=yL d=xH .y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x.yH sum=2c+d draw(x.2) d =6 c = -2 20/110 .xL c=yL .

y) x 0 y  2 sum  2 (6.4) (0.y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x.2) d =6 c = -2 21/110 .Midpoint Algorithm – Example x=xL y=yL d=xH .yH sum=2c+d draw(x.xL c=yL .

4) (0.xL c=yL .2) d =6 c = -2 22/110 .y) x 1 y  2 sum  2 (6.y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x.yH sum=2c+d draw(x.Midpoint Algorithm – Example x=xL y=yL d=xH .

4) (0.y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x.2) d =6 c = -2 23/110 .y) x2 y  3 sum  6 (6.xL c=yL .yH sum=2c+d draw(x.Midpoint Algorithm – Example x=xL y=yL d=xH .

y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x.Midpoint Algorithm – Example x=xL y=yL d=xH .2) d =6 c = -2 24/110 .4) (0.xL c=yL .y) x 3 y  3 sum  2 (6.yH sum=2c+d draw(x.

xL c=yL .y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x.Midpoint Algorithm – Example x=xL y=yL d=xH .2) d =6 c = -2 25/110 .4) (0.yH sum=2c+d draw(x.y) x  4 y  3 sum  2 (6.

2) d =6 c = -2 26/110 .4) (0.xL c=yL .Midpoint Algorithm – Example x=xL y=yL d=xH .yH sum=2c+d draw(x.y) x  5 y  4 sum  6 (6.y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x.

Midpoint Algorithm – Example x=xL y=yL d=xH .4) (0.2) d =6 c = -2 27/110 .y) x 6 y  4 sum  2 (6.y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x.xL c=yL .yH sum=2c+d draw(x.

Midpoint Algorithm • Only integer operations • Exactly the same as the more commonly found Bresenham’s line drawing algorithm • Extends to other types of shapes (circles) 28/110 .