You are on page 1of 28

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

yH yL xH xL

2 40 1 6 3

(6,4)

x6 y4

(0,2)

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

Midpoint Algorithm
Given a point just drawn, determine whether we move E or NE on next step

( x 1, y 1 ) ? Is the line above or below 2


Below: move E Above: move NE

3/110

Above/Below the Line?

(6,4)

(0,2)

4/110

Two Issues
How to evaluate if the midpoint is above or below the line? How to incrementally evaluate this?

5/110

Midpoint Algorithm Implicit Forms


How do we tell if a point is above or below the line? y mx b
y

(3,7)
(6,5) (0,2)
x
6/110

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

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) cx dy e
c yL yH Properties d xH xL e b( x L x H )

f(x,y)=0 (x,y) on the line f(x,y)<0 (x,y) below the line f(x,y)>0 (x,y) above the line
8/110

Midpoint Algorithm Implicit Forms


y x2
3 6

y
f (0,5) 18

f ( x, y) 6 y 3x 12
(0,2)

(6,5) f (4,4) 0
f (4,1) 18
x

9/110

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,yL+1/2)

f ( xL 1, y L 1 ) c( xL 1) d ( y L 1 ) e 2 2 f ( xL 1, y L 1 ) f ( xL , y L ) c 1 d 2 2 f ( xL 1, y L 1 ) f ( xL , y L ) c 1 d 2 2

( xL , y L ) is on the line!
f ( xL 1, y L 1 ) c 1 d 2 2
11/110

Midpoint Algorithm
What about starting value?

f ( xL 1, y L 1 ) c( xL 1) d ( y L 1 ) e 2 2 f ( xL 1, y L 1 ) 2c d 2
Multiplying coefficients by 2 doesnt change sign of f

12/110

Midpoint Algorithm
Need value of f ( x 1, y 1 ) to determine E or 2 NE Build incremental algorithm Assume we have value of f ( x 1, y 1 ) 2
Find value of f ( x 2, y 1 ) if E chosen 2

13/110

Midpoint Algorithm
Need 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, y 1 ) if E chosen 2 Find value of f ( x 2, y 3 ) if NE chosen 2

14/110

Midpoint Algorithm
If E was chosen, find

f ( x 2, y 1 ) 2

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

Midpoint Algorithm
If NE was chosen, find

f ( x 2, y 3 ) 2

f ( x 2, 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

Midpoint Algorithm Summary


x=xL y=yL d=xH - xL c=yL - 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,y)
17/110

Midpoint Algorithm Summary


x=xL y=yL d=xH - xL c=yL - 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,y)

- Update the current pixel - Update the function value of midpoint


18/110

Midpoint Algorithm Summary


x=xL y=yL d=xH - xL c=yL - 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,y)
- Draw the pixel based on the function value of midpoint
19/110

Midpoint Algorithm Example


x=xL y=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)

x 0

y 2 sum 2 (6,4)

(0,2)

d =6 c = -2
20/110

Midpoint Algorithm Example


x=xL y=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)

x 0

y 2 sum 2 (6,4)

(0,2)

d =6 c = -2
21/110

Midpoint Algorithm Example


x=xL y=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)

x 1

y 2 sum 2 (6,4)

(0,2)

d =6 c = -2
22/110

Midpoint Algorithm Example


x=xL y=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)

x2

y 3 sum 6 (6,4)

(0,2)

d =6 c = -2
23/110

Midpoint Algorithm Example


x=xL y=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)

x 3

y 3 sum 2 (6,4)

(0,2)

d =6 c = -2
24/110

Midpoint Algorithm Example


x=xL y=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)

x 4 y 3 sum 2
(6,4)

(0,2)

d =6 c = -2
25/110

Midpoint Algorithm Example


x=xL y=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)

x 5 y 4 sum 6
(6,4)

(0,2)

d =6 c = -2
26/110

Midpoint Algorithm Example


x=xL y=yL d=xH - xL c=yL - yH sum=2c+d draw(x,y) while ( x < xH) if ( sum < 0 ) sum += 2d y++ x++ sum += 2c draw(x,y)

x 6

y 4 sum 2 (6,4)

(0,2)

d =6 c = -2
27/110

Midpoint Algorithm
Only integer operations Exactly the same as the more commonly found Bresenhams line drawing algorithm Extends to other types of shapes (circles)

28/110

You might also like