Scan Conversion LineDDA Line and Bresenham Line 1
Scan Conversion Line(DDA Line and Bresenham Line) 1
• DDA Line: – The Digital Differential Analyzer algorithm is an incremental scan-conversion method. Such an approach is characterized by performing calculations at each step using result from the preceding step. – Suppose at step I we have calculated (xi, yi) to be a point on the line. Since the next point (Xi+1, yi+1) should satisfy m=dy/dx – If |m|<=1 we start with x= x 1 and y= y 1 and set dx=1 (i. e unit increment in the x-direction). The y co-ordinate of each succesive point is calculated using yi+1=yi+m. – if |m|>1 we start with x=x 1 and y-y 1 and set dy=1(i. e. unit increment in the y direction). The x-coordinate of each successive point on the line is calculated using xi+1=xi+1/m. – This process continues until x reaches x 2 (|m|<=1 case) or y reaches y 2 (for |m|>1 case) and all points found are scan converted to pixel co-ordinates. 2
• DDA Line Algo: 1) Input (x 1, y 1) and (x 2, y 2) 2) Compute dx=|x 2 -x 1| and dy=|y 2 -y 1| 3) Set x=x 1 and y=y 1 4) If dx>dy then Steps=dx Else Steps=dy 5) Compute incremental values of x and y xx=dx/steps and yy=dy/steps 6) Draw (x, y) 7) Compute next co-ordinates. x=x+xx and y=y+yy 8) If no of iteration >steps then Stop Else Goto step 6 9) stop. 3
• Example Find the intermediate co-ordinates for the given points (1, 1) (8, 9) Sol: 1) set x=x 1 and y=y 1 2) calculate dx=|x 2 -x 1|=|8 -1|=7, dy =|y 2 -y 1|=|9 -1|=8 X 3) Check dx > dy i. e 7>8( false) then steps=dy i. e steps=8 y roun d 4) Compute increment values of x and y (i. e the value to be added to get new x 1 1 and y) 1. 87 2 2. 87 3 3. 87 4 4. 87 5 5 5 When the value exceed 8 program will stop because 5. 87 6 6. 87 7 7. 87 8 8 8 xx=dx/steps= 7/8= 0. 87 and yy= dy/steps=8/8=1 5) Compute next co-ordinate ( new x and y) x=x+xx= 1+0. 87 and y =y+yy=1+1=2 Steps exceed m= 8/7=1. 14 and 1/m=0. 87 4
• Find the intermediate point for the given points(1, 2)(14, 15) • dy=15 -2=13 dx=14 -1=13 • Dx>dy(false) therefor steps= dy(13) • xx=dx/steps=13/13 and yy=dy/steps=13/13=1 • x=x+xx=1+1=2 • y=y+yy=2+1=3 5
Program: dx=abs(xb-xa); dy=abs(yb-ya); if(dx>=dy) { steps=dx; } else { steps=dy; } xincr=dx/steps; yincr=dy/steps; x=xa; y=ya; putpixel(x, y, RED); for(k=1; k<steps; k++) { x=xincr+x; y=yincr+y; putpixel(x, y, k); } 6
• Bresenham 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. – We start with pixel p 1(x 1, y 1), then select subsequent pixels as we work our way to the right one pixel position at a time in the horizontal direction towardsp 2(x 2, y 2). – Once a pixel is chosen at any step, the next pixel is either the one to its right or the one to its right and up due to limit on m. – The line is best approximated by those pixels that fall the least distance from its true path between p 1 and p 2. 7
• Working: – The co-ordinates of the last chosen pixel upon entering step I are(xi, yi). – Our task is to choose the next one between the bottom pixel S and the top pixel T. – If S is chosen we have xi+1=xi+1 and yi+1=yi. – If T is chosen, we have xi+1=xi+1 and yi+1=yi+1 – The actual y coordinate of the line at x=xi+1 is y=m(xi+1)+b – The distance from S to actual line in the y-direction is s=y-yi – The distance from T to actual line is t=(yi +1)-y 8
• Now consider the difference between these two distance values: s-t • When s-t is less than zero we have s<t and the closest pixel is S. • When s-t is greater than zero we have s>t and the closest pixel is T • We also choose T when s-t=0 • Derivation: s-t=(y-yi)-[(yi+1)-y] s-t= 2 y-2 yi-1 = 2 m(xi+1)+2 b-2 yi-1 Substituting m=dy/dx and introducing a decision variable d i=dx(s-t). di= 2 dy*xi - 2 dx*yi+C Where C=2 dy+dx(2 b-1) Similarly we can write decision variable di+1 for the next step as 9
di+1 = 2 dy*xi+1 -2 dx*yi+1+c Then di+1 -di= 2 dy(xi+1 -xi)-2 dx(yi+1 -yi) Since xi+1=xi+1, we have di+1=di+2 dy-2 dx(yi+1 -yi) If the chosen pixel is the top pixel T (di>=0) then yi+1=yi+1 and so di+1=di+2 dy-dx On the other hand if the chosen pixel is the bottom pixel S( meaning that di<0) then yi+1=yi and so di+1=di+2 dy 10
• Algo: 1) Input (x 0, y 0) and (xn, yn) 2) Calculate dx , dy and 3) Plot (x 0, y 0) 4) Calculate p 0=2 dy-dx 5) At each xk if pk<0 then { plot(xk+1, yk) Calculate pk+1=pk+2 dy } Else { Plot(xk+1, yk+1) Calculate pk+1=pk+2 dy-2 dx } 6)Repeat step 5 dx times. 7) stop. 11
• Line endpoints(20, 10) and (30, 18) dy=y 2 -y 1=18 -10=8 dx= x 2 -x 1=30 -20=10 Initial Decision Parameterhas the value. 1) P 0=2*dy-dx=2*8 -10=16 -10=6 2) If pk<0 then plot(xk+1, yk) Calculate pk+1=pk+2 dy Else Plot(xk+1, yk+1) Calculate pk+1=pk+2 dy-2 dx (This Will repeat dx no of times) K Pk (xk+1, yk+1) 0 6 21, 11 1 2 22, 12 2 -2 23, 12 3 14 24, 13 4 10 25, 14 5 6 26, 15 6 2 27, 16 7 -2 28, 16 8 14 29, 17 9 10 30, 18 12
Circle Drawing Algorithm n 8 segments of octants for a circle: If point 1 (x, y) were calculated with a circle algorithm seven more points could be found by reflection. 13 13
• Two Algorithms: Bresenham and Midpoint 1) The best approximation of the true circle will be described by those pixels in the raster that fall the least distance from the true circle. 2) Each new point closest to the true circle can be found by taking either of the two actions a) move in the x direction. b) move in x-direction one unit and move in negative y direction by 1. 14
• Derivation: • T= (xi+1, yi) • S=(xi+1, yi-1) D(T)= (xi+1)2 +yi 2 -r 2 D(S)=(xi+1)2+(yi-1)2 -r 2 D(T) will always be positive outside the circle and D(S) will always be negative inside the circle. di=D(T)+D(S) di=2(xi+1)2+yi 2+(yi-1)2 -2 r 2 When di<0 we have D(T) <D(S) and pixel T is chosen. When di>0 then D(T)>=D(S) and pixel S is selected. 15
• di+1=2(xi+1+1)2 + yi 2+1 +(yi+1 -1)2 - 2 r 2 • di+1 -di=2(xi+1+1)2+ yi 2+1 + (yi+1 -1)2 – 2(xi+1)2 - yi 2 –(yi-1)2 • Since xi+1 =xi+1 • di+1=di+4 xi+2(yi 2+1 -yi 2)-2(yi+1 -yi)+6 • If T is chosen (meaning that di<0) then yi+1=yi and di+1=di+4 xi+6 If s is chosen meaning di>0) then yi+1=yi-1 di+1=di + 4(xi-yi) + 10. d 1= 3 -2 r 16
• Algo 1) 2) Input x=0, y=r, d=3 -2 r Repeat while (x<=y) { plot(x, y, 10); if(d<0) then { d=d+4 x+6 } else { d=d+4(x-y)+10 y- } 3. Plot (x, y, 10) { plot the co-ordinate x and y and all other symmetric point } 17
• Program: Main() { void circle 1(int, int); void plotpoints(int, int); cout<<"Enter the location of the circle: "; cin>>x>>y; cout<<endl<<"Enter the radius of circle: "; cin>>r; putpixel(x, y, 15); circle 1(x, y, r); } void circle 1(int xc , int yc , int r) { plotpoints(xc, yc, x, y); int x=0, y=r, p=3 -(2*r); 18
while(x<y) {if(p<0) { x++; p=p+4*x+6; } else { x++; y--; p=p+4*(x-y)+10 } plotpoints(xc, yc, x, y); sleep(1); }} void plotpoints(int xc , int yc , int x , int y) { putpixel(xc+x, yc+y, 11); putpixel(xc+x, yc-y, 11); putpixel(xc-x, yc+y, 11); putpixel(xc-x, yc-y, 11); putpixel(xc+y, yc+x, 11); putpixel(xc-y, yc-x, 11); putpixel(xc-y, yc+x, 11); putpixel(xc+y, yc-x, 11); } 19
Midpoint Circle Drawing Algorithm n Circle function: fcircle (x, y) = x 2 + y 2 –r 2 { > 0, (x, y) outside the circle fcircle (x, y) = < 0, (x, y) inside the circle = 0, (x, y) is on the circle boundary 20 20
• Now consider the Co-ordinates of the point halfway between pixel T and Pixel S • (Xi+1, yi-1/2). This is called mid-point and we use it to define a decision parameter. • pi=f(xi+1, yi-1/2)=(xi+1)2 + (yi-1/2)2 –r 2 • If pi is negative then the midpoint is inside the circle, and we choose T pixel. On the other hand if pi is positive (or equal to zero), the mid-point is outside the circle(or on the circle) and we choose pixel S similarly the decision parameter for the next step is • pi+1=(xi+1)2 + (yi+1 -1/2)2 –r 2 • Since xi+1=xi+1 we have pi+1 = pi + 2(xi+1) +1+(yi 2+1 -yi 2)-(yi+1 -yi) If pixel T is chosen (meaning pi<0) we have yi+1=yi on the other hand if pixel S is chosen(meaning pi>0) we have yi+1=yi-1 21
• Pi+1=pi+2(xi+1)+1 if pi<0 pi+2(xi+1)+1 -2(yi-1) if pi>=0 Final pi+1=pi+2 xi+3 if pi<0 pi+1 = pi+2( xi-yi )+5 if pi>0 Finaly we compute the initial value for the decision parameter using the original defination pi and (0, r) pi=5/4 -r 22
» Algo: 1) Input x=0, y=r p=1 -r; 2) While(x<=y) { plot(x, y) if(p<0) { p=p+2 x+3} Else { p=p+2(x-y)+5 y- } X+ + } 3) Plot (x, y, 10) { Plot the co-ordinate x and y and all the other symmetric point; } 23
- Slides: 23