Presentation is loading. Please wait.

Presentation is loading. Please wait.

Line clipping: Line clipping algorithm is method of eliminate lines of outside area of the object,so outside of object viewing is Removed. Typically, any.

Similar presentations


Presentation on theme: "Line clipping: Line clipping algorithm is method of eliminate lines of outside area of the object,so outside of object viewing is Removed. Typically, any."— Presentation transcript:

1 Line clipping: Line clipping algorithm is method of eliminate lines of outside area of the object,so outside of object viewing is Removed. Typically, any line or part thereof which is outside of the viewing area is removed. Cohen-Sutherland line clipping algorithm: The Cohen- Sutherland algorithm is a line clipping algorithm. The algorithm divides a 2D space into 9 parts, of which only the middle part (viewport) is visible.line clipping First we test whether both endpoints are inside (and hence draw the line segment) or whether both are left of, right of, below, or above (then we ignore line segment). Otherwise we split the line segment into two pieces at a clipping edge (and thus reject one part). Now we proceed iteratively. A rather simple accept-reject test is the following: Line clipping

2 Divide the plane into 9 regions and assign a 4 bit code to each: 1000...above top edge 0100...below bottom edge 0010...right of right edge 0001...left of left edge math calculate the corresponding bit-codes for both endpoints. Figure: Codes for the 9 regions associated to clipping rectangle If both codes are zero then the line segment is completely inside the rectangle. If the bitwise-and of these codes is not zero then the line does not hit since both endpoints lie on the wrong side of at least one boundary line (corresponding to a bit equal to 1). Otherwise take a line which is met by the segment (for this find one non-zero bit), divide the given line at the intersection point in two parts and reject the one lying in the outside halfplane. bit 4bit 3bit 2bit 1

3 Cohen-Sutherland line clipping

4 Flowchart : Line clipping start int driver, mode; float i,xmax,ymax,xmin,ymin,x11,y11,x22,y22,m; float a[4],b[4],c[4],x1,y1; input xmin,ymin,xmax,ymax,x11,y11,x22,y22; detectgraph(&driver,&mode); initgraph(&driver,&mode,”\\BGI”); rectangle(xmin,ymin,xmax,ymax); line(x11,y11,x22,y22); i=0 if(i<4) a[i]=0; b[i]=0; i++ m=( y22-y11)/(x22-x11) true false

5 if(x11<xmin) a[3]=1 if(x11>xmax) a[2]=1 if(y11<ymin) a[1]=1 if(y11>ymax) a[0]=1 if(x22<xmin) b[3]=1 true false

6 if(x22>xmax) b[2]=1 if(y22<ymin) b[1]=1 if(y22>ymax) b[0]=1 i=0 if(i<4) a[i] i++ i=0 if(i<4) b[i] i++ true false true

7 i=0 if(i<4) c[i] = a[i]&&b[i] i++ i=0 if(i<4) c[i] if((c[0]==0)&&(c[1]==0)&&(c[2]==0)&&(c[3]==0)) i++ if((a[0]==0)&&(a[1]==0)&&(a[2]==0)&&(a[3]==0)&& (b[0]==0)&&(b[1]==0)&&(b[2]==0)&&(b[3]==0)) true false true false true line is totally visible and not a clipping candidate go to block-2 line is partially visible go to block-1 line is invisible rectangle(xmin, ymin,xmax,ymax) go to block-3

8 rectangle(xmin,ymin,xmax,ymax) line(x11,y11,x22,y22) rectangle(xmin,ymin,xmax,ymax) line(x11,y11,x22,y22) if(a[0]==0&&a[1]=1) x1=x11+(ymin-y11)/m x11=x1 y11=ymin if(b[0]==0&&b[1]==1) x1=x22+(ymin-y22)/m x22=x1 y22=ymin false true block-2 block-1 true false if((a[0]==1)&&(a[1]==0)) x1=x11+(ymax-y11)/m x11=x1 y11=ymax true else if(b[0]==1&&b[1]==0) x1=x22+(ymax-y22)/m x22=x1 y22=ymax false true go to block-3

9 if((a[2]==0)&&(a[3]==1)) y1=y11+(m*(xmin-x11)) y11=y1 x11=xmin if(b[2]==0&&b[3]==1) y1=y22+(m*(xmin-x22)) y22=y1 x22=xmin false true if((a[2]==1)&&(a[3]==0)) y1=y11+(m*(xmax-x11)) y11=y1 x11=xmax if(b[2]==1&&b[3]==0) y1=y22+(m*(xmax-x22)) y22=y1 x22=xmax false true

10 after clipping rectangle(xmin,ymin,xmax,ymax ) line(x11,y11,x22,y22 ) getch(); output stop block-3

11 Program: /*Cohen-Sutherland line clipping program to clip the line outside the window boundary*/ #include void main() { int gd=DETECT, gm; float i,xmax,ymax,xmin,ymin,x11,y11,x22,y22,m; float a[4],b[4],c[4],x1,y1; clrscr(); initgraph(&gd,&gm,"c:\\tc\\bgi"); printf("\nEnter the bottom-left coordinate of viewport: "); scanf("%f %f",&xmin,&ymin); printf("\nEnter the top-right coordinate of viewport: "); scanf("%f %f",&xmax,&ymax); rectangle(xmin,ymin,xmax,ymax); printf("\nEnter the coordinates of 1st end point of line: "); scanf("%f %f",&x11,&y11); printf("\nEnter the coordinates of 2nd endpoint of line: "); scanf("%f %f",&x22,&y22);

12 line(x11,y11,x22,y22); //initgraph(&gd,&gm,"c:\\tc\\bin"); for(i=0;i<4;i++) { a[i]=0; b[i]=0; } m=(y22-y11)/(x22-x11); if(x11<xmin) a[3]=1; if(x11>xmax) a[2]=1; if(y11<ymin) a[1]=1; if(y11>ymax) a[0]=1; if(x22<xmin) b[3]=1; if(x22>xmax) b[2]=1; if(y22<ymin) b[1]=1; if(y22>ymax) b[0]=1; printf("\nRegion code of 1st pt "); for(i=0;i<4;i++) { printf("%f",a[i]);} printf("\nRegion code of 2nd pt "); for(i=0;i<4;i++) { printf("%f",b[i]);} printf("\nAnding : ");

13 for(i=0;i<4;i++) { c[i]=a[i]&&b[i];} for(i=0;i<4;i++) printf("%f",c[i]); getch(); if((c[0]==0)&&(c[1]==0)&&(c[2]==0)&&(c[3]==0)) { if((a[0]==0)&&(a[1]==0)&&(a[2]==0)&&(a[3]==0)&& (b[0]==0)&&(b[1]==0)&&(b[2]==0)&&(b[3]==0)) { clrscr(); clearviewport(); printf("\nThe line is totally visible\n and not a clipping candidate"); rectangle(xmin,ymin,xmax,ymax); line(x11,y11,x22,y22); getch(); } else { clrscr(); clearviewport(); printf("\nLine is partially visible"); rectangle(xmin,ymin,xmax,ymax); line(x11,y11,x22,y22);

14 getch(); if((a[0]==0)&&(a[1]==1)) { x1=x11+(ymin-y11)/m; x11=x1; y11=ymin; } else if((b[0]==0)&&(b[1]==1)) { x1=x22+(ymin-y22)/m; x22=x1; y22=ymin; } if((a[0]==1)&&(a[1]==0)) { x1=x11+(ymax-y11)/m; x11=x1; y11=ymax; } else if((b[0]==1)&&(b[1]==0)) { x1=x22+(ymax-y22)/m; x22=x1; y22=ymax; } if((a[2]==0)&&(a[3]==1)) { y1=y11+(m*(xmin-x11)); y11=y1; x11=xmin; }

15 else if((b[2]==0)&&(b[3]==1)) { y1=y22+(m*(xmin-x22)); y22=y1; x22=xmin; } if((a[2]==1)&&(a[3]==0)) { y1=y11+(m*(xmax-x11)); y11=y1; x11=xmax; } else if((b[2]==1)&&(b[3]==0)) { y1=y22+(m*(xmax-x22)); y22=y1; x22=xmax; } clrscr(); clearviewport(); printf("\nAfter clippling:"); rectangle(xmin,ymin,xmax,ymax); line(x11,y11,x22,y22); getch(); }

16 else { clrscr(); clearviewport(); printf("\nLine is invisible"); rectangle(xmin,ymin,xmax,ymax); getch(); } closegraph(); getch(); } Output of the above listed program


Download ppt "Line clipping: Line clipping algorithm is method of eliminate lines of outside area of the object,so outside of object viewing is Removed. Typically, any."

Similar presentations


Ads by Google