Sie sind auf Seite 1von 25

Scan Conversion of Lines

MIDPOINT LINE DRAWING ALGORITHM


Nihar Ranjan Roy

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

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

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

Is the line above or below ?

Below: move E

Above: move NE

( x 1, y )
1
2

( x, y )

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm Implicit


Forms
How do we tell if a point is above or below
the line?

y mx B

yH yL
xH xL

xB

yH yL
xH xL

x B)(xH xL )

( x H xL ) y (

( xH xL ) y ( yH yL ) x ( xH xL ) B
Nihar Ranjan Roy,
https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm Implicit


Forms
How do we tell if a point is above or below the line?

( xH xL ) y ( yH yL ) x ( xH xL ) B
( yH yL ) x ( xH xL ) y ( xH xL ) B 0

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm Implicit


Forms
How do we tell if a point is above or below the line?

( yH yL ) x ( xH xL ) y ( xH xL ) B 0
d f ( x, y ) ax by c
Location of midpoint d=?
f(x,y)=0 (x,y) on the line

f(x,y)<0 (x,y) above the line


f(x,y)>0 (x,y) below the line

a yH yL y
b ( xH xL ) x

c B( xH xL )

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm Implicit


Forms
y x2
3
6

(6,5)
(0,2)
x

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm Implicit


Forms
y

y x2
3
6

f ( x, y ) 3x 6 y 12

(6,5)

(0,2)
Location of midpoint d=?
f(x,y)=0 (x,y) on the line

f(x,y)<0 (x,y) above the line


f(x,y)>0 (x,y) below the line

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm Implicit


Forms
y

y x2
3
6

f ( x, y ) 3x 6 y 12

f (0,5) 18

(6,5)
(0,2)

Location of midpoint d=?


f(x,y)=0 (x,y) on the line

f(x,y)<0 (x,y) above the line


f(x,y)>0 (x,y) below the line

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm Implicit


Forms
y

y x2
3
6

f ( x, y ) 3x 6 y 12

(6,5)
(0,2)

f (4,1) 18

Location of midpoint d=?


f(x,y)=0 (x,y) on the line

f(x,y)<0 (x,y) above the line


f(x,y)>0 (x,y) below the line

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm Implicit


Forms
y

y x2
3
6

f ( x, y ) 3x 6 y 12

(6,5)
f (4,4) 0
(0,2)

Location of midpoint d=?


f(x,y)=0 (x,y) on the line

f(x,y)<0 (x,y) above the line


f(x,y)>0 (x,y) below the line

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm Implicit


Forms
y

y x2
3
6

f ( x, y ) 3x 6 y 12

(6,5)
(0,2)

Location of midpoint d=?


f(x,y)=0 (x,y) on the line

f(x,y)<0 (x,y) above the line


f(x,y)>0 (x,y) below the line

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

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

Location of midpoint d=?


d=f(x,y)=0 (x,y) on the line

NE

d=f(x,y)<0 (x,y) above the line


E

d=f(x,y)>0 (x,y) below the line

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

d?

Midpoint Algorithm d <0


1
f
(
x

2
,
y

If E was chosen, find


2)

dnew f ( x 2, y 12 ) a( x 2) b( y 12 ) c
dnew dold a
dnew dold y

( x 2, y 12 )
NE

Nihar Ranjan Roy,


old
https://sites.google.com/site/niharranjanroy/

dnew

Midpoint Algorithm

d>0

3
f
(
x

2
,
y

If NE was chosen, find


2)

dnew f ( x 2, y 32 ) a( x 2) b( y 32 ) c
NE

dold dnew
Nihar Ranjan Roy,
https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm

d>0
f ( x 2, y 32 )

If NE was chosen, find

dnew f ( x 2, y 32 ) a b f ( x 1, y 12 )
dnew dold a b
dnew dold y x

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm

d0 ?

What about starting value?

d 0 f ( xL 1, yL 12 ) a( xL 1) b( yL 12 ) c

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm d0 ?
What about starting value?

d 0 f ( xL 1, yL 12 ) a( xL 1) b( yL 12 ) c
d 0 f ( xL 1, yL 12 ) f ( xL , yL ) a 12 b

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm
What about starting value?

d 0 f ( xL 1, yL 12 ) a( xL 1) b( yL 12 ) c

f ( xL 1, yL 12 ) f ( xL , yL ) a 12 b

( xL , yL ) is on the line! =0
d 0 f ( xL 1, yL 12 ) a 12 b

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm
d 0 f ( xL 1, yL 12 ) a 12 b
Multiplying coefficients by 2 doesnt change sign of f

d 0 f ( xL 1, yL 12 ) 2a b
d 0 f ( xL 1, yL 12 ) 2y x

Similarly equation 2 and 3 becomes

dnew dold 2y

dnew dold 2(y x)

E is Chosen

NE is Chosen

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm Summary


int dx, dy, d, incE, incNE, x, y;
dx = x2 - x1;
dy = y2 - y1;
d = 2*dy - dx;
incE = 2*dy;
incNE = 2*(dy - dx);
y = y1;
for (x=x1; x<=x2; x++)
{ setpixel(x, y, colour);
if (d>0)
{ d = d + incNE;
y = y + 1; }
else
{ d = d + incE; }
}

NE

Location of midpoint d=?


d=f(x,y)=0 (x,y) on the line
d=f(x,y)<0 (x,y) above the line
d=f(x,y)>0 (x,y) below the line
Nihar Ranjan Roy,
https://sites.google.com/site/niharranjanroy/

Problem

Plot all the raster points that make a line


from (0,2) to (6,4) using Mid-point line
drawing algorithm.

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm Example


int dx, dy, d, incE, incNE, x, y;
dx = x2 - x1;
dy = y2 - y1;
d = 2*dy - dx;
incE = 2*dy;
incNE = 2*(dy - dx);
y = y1;
for (x=x1; x<=x2; x++)
{ setpixel(x, y, colour);
if (d>0)
{ d = d + incNE;
y = y + 1; }
else
{ d = d + incE; }
}

Draw a line from (0,2) to (6,4)


dx=6 dy=2 d0=-2
incE=4
incNE=-8
d

-2

(1,2)

NE

(x,y)
(1,2)

(2,3)

(2,3)

-6

(3,3)

(3,3)

-2

(4,3)

(4,3)

2
-6

(5,4)
(6,4)

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

(5,4)
(6,4)

output

-2

(1,2)

NE

(x,y)

(2,3)

(2,3)

-6

(3,3)

(3,3)

-2

(4,3)

(4,3)

2
-6

(5,4)
(6,4)

(6,4)

(1,2)

(5,4)

(0,2)

(6,4)

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Midpoint Algorithm vs DDA


Only integer operations
Faster than DDA
No multiplication no division

Nihar Ranjan Roy,


https://sites.google.com/site/niharranjanroy/

Das könnte Ihnen auch gefallen