Chapter 3 Scan 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 P 1 (x 1,y 1 ). 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 (x i, y i ). Choose the next between the bottom pixel S and the top pixel T. p1 p2
The difference is – s – t = (y-y i )-[(y i +1)-y] = y-y i -y i -1+y = 2y-2y i -1 = 2(y-y i )-1 = 2 [ { m(x i + 1) + b } - y i ) ] - 1 = 2 m(x i + 1) + 2b - 2y i – 1 ∆x( s – t)=∆x [2m(x i + 1) + 2b - 2y i – 1] So di = ∆x [2m(x i + 1) + 2b - 2y i – 1] ……………. (i) If the chosen pixel is the top pixel T (di ≥ 0) then xi+1 = xi+1 and y i +1 = y i + 1 and so d i+1 = d i + 2 (Δy - Δx) Bresenham’s Line Algorithm
If the chosen pixel is pixel S (d i < 0) then x i+1 = x i +1 and y i+1 = y i and so d i+1 = d i + 2Δy where d i = 2Δy * x i - 2Δx * y i +C and C = 2Δy + Δx (2b – 1) We use here a decision variable d i. For the value of each d i we calculate the corresponding value of d i+1. p1 p2
Hence, we have Finally, we calculate d 1,the base case value for this recursive formula, from the original definition of the decision variable d i d i = ∆x [ 2m (x 1 + 1) + 2b - 2y 1 – 1] = ∆x [ 2( mx 1 + b - y 1 ) + 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: x 2 + y 2 - R 2 = 0 Eight way symmetry require to calculate one octant Define decision variable d as: P=(x p, y p ) M E x p +1xpxp x p +2 Previous Current Next MEME ypyp y p – 1 y p – 2 SE M SE
Midpoint Circle Algorithm If d <= 0 then midpoint m is inside circle – we choose E – Increment x – y remains unchanged P=(x p, y p ) M E x p +1xpxp x p +2 Previous Current Next MEME ypyp y p – 1 y p – 2 d < 0
Midpoint Circle Algorithm If d > 0 then midpoint m is outside circle – we choose S – Increment x – Decrement y Previous P=(x p, y p ) M SE x p +1xpxp x p +2 Current Next M SE ypyp y p – 1 y p – 2 d > 0
Midpoint Circle Algorithm Initial condition Starting pixel (0, R) Next Midpoint lies at (1, R – ½) d 0 = F(1, R – ½) = 1 + (R 2 – R + ¼) – R 2 = 5 / 4 – R To remove the fractional value 5 / 4 : d 0 = 1– R
We can now summarize the algorithm for generating all the pixel coordinates in the 90 0 to 45 0 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: } Midpoint Circle Algorithm