1 - jesminiit

Chapter 3
Scan Conversion
Lecture-02
Bresenham’s Line Algorithm
• Bresenham’s line algorithm
– is a highly efficient incremental method for scanconverting lines.
– It produces mathematically accurate results
using only integer addition, subtraction and
multiplication by 2, which can be accomplished
by a simple arithmetic shift operation.
Bresenham’s Line Algorithm
• Scan convert the line in the Figure,
• 0<m<1.
• Start with pixel P1(x1,y1).
• Choose either the pixel on the right or the
pixel right and up.
• The coordinates of the last chosen pixel
upon entering step i are (xi,yi).
• Choose the next between the bottom pixel
S and the top pixel T.
p2
p1
Bresenham’s Line Algorithm
• The difference is –
s – t = (y-yi)-[(yi+1)-y]
= y-yi-yi-1+y
= 2y-2yi-1
= 2(y-yi)-1
= 2 [ { m(xi + 1) + b } - yi) ] - 1
= 2 m(xi + 1) + 2b - 2yi – 1
∆x( s – t)=∆x [2m(xi + 1) + 2b - 2yi – 1]
So di = ∆x [2m(xi + 1) + 2b - 2yi – 1] ……………. (i)
• If the chosen pixel is the top pixel T (di ≥ 0) then xi+1 = xi+1
and yi+1 = yi + 1 and so
di+1 = di + 2 (Δy - Δx)
Bresenham’s Line Algorithm
• If the chosen pixel is pixel S (di < 0) then
xi+1= xi+1 and yi+1 = yi and so
• di+1 = di + 2Δy
• where di = 2Δy * xi - 2Δx * yi +C
• and C = 2Δy + Δx (2b – 1)
• We use here a decision variable di . For
the value of each di we calculate the
corresponding value of di+1 .
p2
p1
Bresenham’s Line Algorithm
• Hence, we have
• Finally, we calculate d1 ,the base case value for this recursive
formula, from the original definition of the decision variable di
• di
= ∆x [ 2m (x1 + 1) + 2b - 2y1 – 1]
= ∆x [ 2( mx1 + b - y1 ) + 2m – 1]
= ∆x [ 2*0 + 2m – 1]
= 2(∆y / ∆x)* ∆x - ∆x
=2∆y - ∆x
Bresenham’s Line Algorithm
•
•
•
•
•
•
•
•
•
•
•
Void Bresenham( )
{
Line 1: dx=x2-x1;
Line 2: dy=y2-y1;
Line 3: dT=2*(dy-dx);
Line 4: dS=2*dy;
Line 5: d=(2*dy)-dx;
Line 6: putpixel(x1,y1);
Line 7: while(x1<x2)
Line 8: {
Line 9: x1++;
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Line 10: if(d<0)
Line 11: {
Line 12: d=d+dS;
Line 13: putpixel(x1,y1);
Line 14: }
Line 15: else
Line 16: {
Line 17: y1++;
Line 18: d=d+dT;
Line 19: putpixel(x1,y1);
Line 20: }
Line 21: }
Line 22: putpixel(x2,y2);
}
Scan-Converting a Circle
Midpoint Circle Algorithm
• Implicit of equation of circle is:
x2 + y2 - R2 = 0
• Eight way symmetry  require to calculate one
octant
• Define decision variable d as:
d  F ( M )  F ( x p  1, y p  12 )
 x p  1   y p  12   R 2
2
( x p  1, y p  12 ) ( x p  2, y p  12 )
P=(xp, yp)
 M is inside Circle
E
M
 Choose E
 M is outside Circle
 Choose SE
d 0
 Choose either
 we choose E
xp
Previous
d 0
SE
yp
M
E
MSE
xp+1 xp+2
Next
d 0
Current
2
yp –
1
yp –
2
Midpoint Circle Algorithm
• If d <= 0 then midpoint m is inside circle
– we choose E
– Increment x
– y remains unchanged
 x p  2    y p 
2
d new  d  2 x p  3
E  2 x p  3
d new  d  E

1 2
2
P=(xp, yp)
E
M
d< 0
 R2
xp
yp
E
M
xp+1 xp+2
Next
d new  F ( x p  2, y p  )
1
2
 R2
( x p  1, y p  12 ) ( x p  2, y p  12 )
Current

1 2
2
Previous
d  x p  1   y p 
2
yp –
1
yp –
2
Midpoint Circle Algorithm
• If d > 0 then midpoint m is outside circle
– we choose S
– Increment x
– Decrement y
( x p  1, y p  12 ) ( x p  2, y p  32 )
 x p  2    y p 
2
d new  d  2 x p  2 y p  5
SE  2 x p  2 y p  5
d new  d  SE
d> 0
SE

3 2
2
R
2
xp
M
MSE
xp+1 xp+2
Next
d new  F ( x p  2, y p  32 )
R
yp
2
Current

1 2
2
Previous
d  x p  1   y p 
2
P=(xp, yp)
yp –
1
yp –
2
Midpoint Circle Algorithm
Initial condition
• Starting pixel (0, R)
• Next Midpoint lies at (1, R – ½)
• d0 = F(1, R – ½) = 1 + (R2 – R + ¼) – R2 = 5/4 – R
• To remove the fractional value 5/4 :
• d0= 1– R
Midpoint Circle Algorithm
•
We can now summarize the algorithm for generating all the pixel coordinates in
the 900 to 450 octant :
Line 1 : Int x=0,y=r,p=1-r;
Line 2 : While(x<=y)
Line 3 : {
Line 4 : setPixel(x,y);
Line 5 : If(p<0)
Line 6 : p=p+2x+3;
Line 7 : Else
Line 8 : {
Line 9 : P=p+2(x-y)+5;
Line 10 : y--;
Line 11 : }
Line 12 : x++;
Line 1 3: }