Upload
percival-thomas
View
239
Download
1
Embed Size (px)
Citation preview
Chapter 3Scan Conversion
Lecture-02
Bresenham’s Line Algorithm
• Bresenham’s line algorithm– is a highly efficient incremental method for scan-
converting 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.
p1
p2
• 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
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 .
p1
p2
• 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
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:
SE Choose
Circle outside s
E Choose
Circle inside s
iM
d
iM
d
Ryx
yxFMFd
pp
pp
0
0
1
),1()(
22
212
21
P=(xp, yp)
ME
xp+1xp xp+2
Pre
vio
us
Curr
ent
Next
),1( 21 pp yx ),2( 2
1 pp yx
ME
yp
yp – 1
yp – 2
SE
MSE
E choose we
either Choose
0d
Midpoint Circle Algorithm• If d <= 0 then midpoint m is inside circle
– we choose E– Increment x– y remains unchanged
Edd
xE
xdd
Ryx
yxFd
Ryxd
new
p
pnew
pp
ppnew
pp
32
32
2
),2(
1
22
212
21
22
212
P=(xp, yp)
ME
xp+1xp xp+2
Pre
vio
us
Curr
ent
Next
),1( 21 pp yx ),2( 2
1 pp yx
ME
yp
yp – 1
yp – 2
d < 0
Midpoint Circle Algorithm• If d > 0 then midpoint m is outside circle
– we choose S– Increment x– Decrement y
SEdd
yxSE
yxdd
Ryx
yxFd
Ryxd
new
pp
ppnew
pp
ppnew
pp
522
522
2
),2(
1
22
232
23
22
212
Pre
vio
us
P=(xp, yp)
MSE
xp+1xp xp+2
Curr
ent
Next
),1( 21 pp yx ),2( 2
3 pp yx
MSE
yp
yp – 1 yp – 2
d > 0
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
• 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 : ElseLine 8 : {Line 9 : P=p+2(x-y)+5;Line 10 : y--;Line 11 : }Line 12 : x++;Line 1 3: }
Midpoint Circle Algorithm