You are on page 1of 20

Computer Graphics:

Line Drawing Algorithms

Scan Conversion Algorithms


(Midpoint Line)

Course Website: http://www.comp.dit.ie/bmacnamee


2
of
32

Midpoint
3

0 1 2 3 4 5 6 7
3
of
32
4
of
32

(2, 4)  

(2, 3)

(2, 2)
5
of
32

   
6
of
32

 
7
of
32

 
8
of
32

f(M) Pixel chosen

upper

lower
9
of
32

   

 
   
10
of
32

   
 
 

    𝑑  >0 , 𝑠𝑜 𝑁𝐸 𝑖𝑠𝑐h𝑜𝑠𝑒𝑛
 
 
     
 
 

 
11
of
32

 
 
 
    𝑑  ≤0 , 𝑠𝑜 𝐸 𝑖𝑠 𝑐h𝑜𝑠𝑒𝑛
   

     
  𝑀
  2
 
 

 
12
of
32

 
Calculate d for 1st column.
Choose E/NE.
Update acc. to E/NE.
Use to choose E/NE again and repeat the
loop until the end.
13
of
32

𝑑 𝑖𝑛𝑖𝑡 =𝑓 ( 𝑀 0 )
 
6

4
Midpoint
𝑎𝑥 +𝑏𝑦+ 𝑐=0
 

3
𝑀 0 ( 𝑥 0 +1, 𝑦 0 +0.5) 𝑆𝑜
  , 𝑎 𝑥 0 +𝑏 𝑦 0 +𝑐 =0
 
2 ( 𝑥0, 𝑦0)

𝑑𝑖𝑛𝑖𝑡 =𝑎𝑥0 +𝑏 𝑦0 +𝑐+𝑎+0.5𝑏


   
0 1 2 3 4 5 6 7
14
of
32

𝑦=𝑚𝑥+𝐵
( 𝑥1 , 𝑦 1)
 
𝑑𝑦= 𝑦1 − 𝑦 0  
 

( 𝑥0, 𝑦0) 𝑑𝑥= 𝑥 1 − 𝑥 0


   
15
of
32

 
yes no

   

𝐶h𝑒𝑐𝑘
  𝑖𝑓 𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑝𝑖𝑥𝑒𝑙 𝑖𝑠 ( 𝑥 1 , 𝑦 1 )
no
yes
end
16
of
32

𝑃𝑢𝑡𝑡𝑖𝑛𝑔,
 

 
yes no

   

𝐶h𝑒𝑐𝑘
  𝑖𝑓 𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑝𝑖𝑥𝑒𝑙 𝑖𝑠 ( 𝑥 1 , 𝑦 1 )
no
yes
end
17
of
32

𝑦=𝑚𝑥+𝑏
( 𝑥1 , 𝑦 1)
 
𝑑𝑦= 𝑦1 − 𝑦 0  
 

( 𝑥0, 𝑦0) 𝑑𝑥= 𝑥 1 − 𝑥 0


   

2𝑥+3
 
𝑦+1=0
18
of
32

𝑦=𝑚𝑥+𝑏
( 𝑥1 , 𝑦 1)
 
𝑑𝑦= 𝑦1 − 𝑦 0  
 

( 𝑥0, 𝑦0) 𝑑𝑥= 𝑥 1 − 𝑥 0


   
19
of
32

𝑃𝑢𝑡𝑡𝑖𝑛𝑔,
 

 
yes no

   

𝐶h𝑒𝑐𝑘
  𝑖𝑓 𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑝𝑖𝑥𝑒𝑙 𝑖𝑠 ( 𝑥 1 , 𝑦 1 )
no
yes
end
20
of
32
Algorithm
func MidpointLine(int x0, int y0, int x1, int y1, int value){
int dx, dy, incrE, incrNE, d, x, y;
dx = x1 -x0;
dy = y1 -y0;
d = 2 * dy - dx;
incrE = 2 * dy;
incrNE = 2 * (dy - dx);
x = x0;
y = y0;
WritePixel (x, y, value);
while (x < x1) {
if (d <= 0) {
//choose E
d = d + incrE;
x = x + 1;
}
else {
//choose NE
d = d + incrNE;
x = x + 1;
y = y + 1;
}
WritePixel (x,y, value) //The selected pixel closest to the line
}
}

You might also like