Download presentation
Presentation is loading. Please wait.
1
Scan Conversion Line(DDA Line and Bresenham Line)
2
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= x1 and y= y1 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=x1 and y-y1 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 x2 (|m|<=1 case) or y reaches y2 (for |m|>1 case) and all points found are scan converted to pixel co-ordinates.
3
DDA Line Algo: 1) Input (x1,y1) and (x2,y2)
2) Compute dx=|x2-x1| and dy=|y2-y1| 3) Set x=x1 and y=y1 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 Goto step 6 9) stop.
4
Example Find the intermediate co-ordinates for the given points (1,1) (8,9) Sol: 1) set x=x1 and y=y1 2) calculate dx=|x2-x1|=|8-1|=7, dy =|y2-y1|=|9-1|=8 3) Check dx > dy i.e 7>8( false) then steps=dy i.e steps=8 4) Compute increment values of x and y (i.e the value to be added to get new x and y) xx=dx/steps= 7/8= and yy= dy/steps=8/8=1 5) Compute next co-ordinate ( new x and y) x=x+xx= and y =y+yy=1+1=2 When the value exceed 8 program will stop because Steps exceed m= 8/7=1.14 and 1/m=0.87 X y round 1 1.87 2 2.87 3 3.87 4 4.87 5 5.87 6 6.87 7 7.87 8
5
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
6
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); }
7
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 p1(x1,y1), then select subsequent pixels as we work our way to the right one pixel position at a time in the horizontal direction towardsp2(x2,y2). 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 p1 and p2.
8
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
9
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= 2y-2yi-1 = 2m(xi+1)+2b-2yi-1 Substituting m=dy/dx and introducing a decision variable di=dx(s-t). di= 2dy*xi - 2dx*yi+C Where C=2dy+dx(2b-1) Similarly we can write decision variable di+1 for the next step as
10
di+1 = 2dy*xi+1-2dx*yi+1+c Then di+1-di= 2dy(xi+1-xi)-2dx(yi+1-yi) Since xi+1=xi+1, we have di+1=di+2dy-2dx(yi+1-yi) If the chosen pixel is the top pixel T (di>=0) then yi+1=yi+1 and so di+1=di+2dy-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+2dy
11
Algo: Input (x0,y0) and (xn,yn) Calculate dx ,dy and Plot (x0,y0)
Calculate p0=2dy-dx At each xk if pk<0 then { plot(xk+1,yk) Calculate pk+1=pk+2dy } Else { Plot(xk+1,yk+1) Calculate pk+1=pk+2dy-2dx } 6)Repeat step 5 dx times. 7) stop.
12
Line endpoints(20,10) and (30,18)
dy=y2-y1=18-10=8 dx= x2-x1=30-20=10 Initial Decision Parameterhas the value. 1) P0=2*dy-dx=2*8-10=16-10=6 2) If pk<0 then plot(xk+1,yk) Calculate pk+1=pk+2dy Else Plot(xk+1,yk+1) Calculate pk+1=pk+2dy-2dx (This Will repeat dx no of times) K Pk (xk+1,yk+1) 6 21,11 1 2 22,12 -2 23,12 3 14 24,13 4 10 25,14 5 26,15 27,16 7 28,16 8 29,17 9 30,18
13
Circle Drawing Algorithm
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.
14
Two Algorithms: Bresenham and Mid- point
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.
15
Derivation: T= (xi+1,yi) S=(xi+1,yi-1) D(T)= (xi+1)2 +yi2-r2
D(S)=(xi+1)2+(yi-1)2-r2 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+yi2+(yi-1)2-2r2 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.
16
di+1=2(xi+1+1)2 + yi2+1 +(yi+1-1)2 - 2r2
di+1-di=2(xi+1+1)2+ yi (yi+1-1)2 – 2(xi+1)2 - yi2 –(yi-1)2 Since xi+1 =xi+1 di+1=di+4xi+2(yi2+1-yi2)-2(yi+1-yi)+6 If T is chosen (meaning that di<0) then yi+1=yi and di+1=di+4xi+6 If s is chosen meaning di>0) then yi+1=yi-1 di+1=di + 4(xi-yi) + 10. d1= 3-2r
17
Algo Input x=0,y=r, d=3-2r Repeat while (x<=y) { plot(x,y,10);
if(d<0) then d=d+4x+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
18
Program: Main() { void circle1(int,int,int);
void plotpoints(int,int,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); circle1(x,y,r); } void circle1(int xc , int yc , int r) plotpoints(xc,yc,x,y); int x=0,y=r,p=3-(2*r);
19
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); }
20
Midpoint Circle Drawing Algorithm
Circle function: fcircle (x,y) = x2 + y2 –r2 > 0, (x,y) outside the circle < 0, (x,y) inside the circle = 0, (x,y) is on the circle boundary { fcircle (x,y) =
21
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 –r2 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 –r2 Since xi+1=xi+1 we have pi+1 = pi + 2(xi+1) +1+(yi2+1-yi2)-(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
22
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+2xi+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
23
Algo: Input x=0,y=r p=1-r; While(x<=y) { plot(x,y) if(p<0) p=p+2x+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;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.