Circle, Ellipse, Fill Algo's
Circle, Ellipse, Fill Algo's
Circle Equation F(xk,yk)=xk2+yk2-r2 Mid point equation for (xk+1,yk-1/2) Desired position p=f(xk+1,yk-1/2)= (xk+1)2+( yk-1/2)2-r2 pold= f(xk+1,yk-1/2)= (xk+1)2+( yk-1/2)2-r2
We will get two cases based on pold value If pold < 0 case 1 for upper pixel If pold >0 case 2 for lower pixel
Algorithm Step1:input radius r and (x,y)=(0,r) Step2: initial value of the decision parameter as P=1-r Step3: while(x<y) If(p<0) P=p+2xk+3; Else P=p+2xk-2yk+5; y--; endif; x++; plotpixe(x,y); end while; step 4:exit;
Ellipse Algorithm Step1: input radios Rx and Ry and (0,Ry) Step2:initial p1 value P1=Ry2-Rx2Ry+(1/4)Rx2 for region 1 Step3:While(2Rx2y>2Ry2 x) If(p<0) P=p+2Ry2x+3Ry2; Else P=P+2Rx2y-2Rx2+Ry2; y++; endif; x++;plotpixel(x,y); end while; step4: find initial value p2 for region 2 P2=Ry2(x0+1/2)2+Rx2(y0-1)2-Ry2Rx2 While(y>0) If(p2>0) p2=p2-2Rx2y+3Rx2; Else p2=p2+2Ry2x+2Ry2-2Rx2y+3Rx2 ,x++;
Example
Rx= and Ry= 8 6 P1= y2-Rx2Ry+ R (1/4)Rx2= -332
p -332 -224 -44 208 -108 288 244 (X,Y) (1,6 ) (2,6 ) (3,6 ) (4,5 ) (5,5 ) (6,4 ) (7,3 )
2 x2y r 7 68 7 68 7 68 6 40 6 40 5 12 3 84
2 P 2= R2(x0+1 /2)+ Rx2(y0-1)2-Ry2Rx2 y W hile(y >0 ) If(p2>0) Left p2= p 22 Rx2y+3R2; x E lse R ig ht 2 p2= p 2+2Rx+ 2R2-2Rx2y+3R2 y y x x++ ; en dif;y-; ; p lo t p ixel( x,y);
Boundary Fill algorithm In many graphics packages the user can fill a region (defined by a boundary). In the figure, the boundary is red and the filling color is blue
current = getPixel(x, y) If (current != boundary) and (current != fill) { setPixel(x, y, fill) boundaryFill(x+1, y, fill, boundary) boundaryFill(x , y+1, fill, boundary) boundaryFill(x-1, y, fill, boundary) boundaryFill(x , y-1, fill, boundary)
The following pseudo-code fills a 8-connected area: boundaryFill(x, y, fill, boundary) { current = getPixel(x, y) If (current != boundary) and (current != fill) { setPixel(x, y, fill)
boundaryFill(x+1, y, fill, boundary) boundaryFill(x , y+1, fill, boundary) boundaryFill(x-1, y, fill, boundary) boundaryFill(x , y-1, fill, boundary)
boundaryFill(x+1, y+1, fill, boundary) boundaryFill(x+1 , y-1, fill, boundary) boundaryFill(x-1, y+1, fill, boundary) boundaryFill(x -1, y-1, fill, boundary)