A CPP program to show demo on Line clip
***********************************************************************************************************************
Program : A CPP program to show demo on Line clip
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | #include<graphics.h> #include<conio.h> #include<iostream.h> struct Point { int x,y; // for storing x&y co-ordinates of a point unsigned int code[4]; // to store 4digit code for a point }; struct Line { Point a,b; // Line has two points }; Point wmin,wmax; // to store windows min& max co-ordinates int xc,yc; // to store maxx,maxy void changePts(Point*,float); // func that finds intersection points of line on windows void getCode(Point*); // func to get the 4 digit code for a point int getAttr(Line); // func that decides whether to clip the line or not void main() { int i,n,gd=0,gm,chk=0; float s; //to store slope when required Line m[10]; // to store points of Lines initgraph(&gd,&gm,"c:\\tc\\bgi"); //initiates graphics mode xc=getmaxx()/2,yc=getmaxy()/2; // to get x&y centers line(xc,0,xc,2*yc),line(0,yc,2*xc,yc); // to draw axis lines cout<<"\n Enter no of lines : "; cin>>n; //to accept no of lines cout<<"\n Enter Co-ordinates : "; for(i=0;i<n;i++) // to accept end points for given n lines cin>>m[i].a.x>>m[i].a.y>>m[i].b.x>>m[i].b.y; cout<<"\n Enter windows min&max co-ordinates : "; cin>>wmin.x>>wmin.y>>wmax.x>>wmax.y; //to accept windows min&max co-ordinates rectangle(xc+wmin.x,yc-wmin.y,xc+wmax.x,yc-wmax.y); // to draw window for(i=0;i<n;i++)// to draw the given n lines & also find 4digit code for each point { line(xc+m[i].a.x,yc-m[i].a.y,xc+m[i].b.x,yc-m[i].b.y); getCode(&(m[i].a)),getCode(&(m[i].b)); // to find 4digit code } for(i=0;i<n;i++) // for clipping the lines if(getAttr(m[i])==1)//to decide whether to clip the lines or leave it { //alone (if iniside) completely remove the lines (if outside) if(m[i].a.x==m[i].b.x) // clipping of lines if slope is infinite { if(m[i].a.code[3]==1) m[i].a.y=wmax.y; else if(m[i].a.code[2]==1) m[i].a.y=wmin.y; if(m[i].b.code[3]==1) m[i].b.y=wmax.y; else if(m[i].b.code[2]==1) m[i].b.y=wmin.y; } else // clipping of lines { start : chk++; // for calc the slope of line float s = float((m[i].b.y-m[i].a.y))/(m[i].b.x-m[i].a.x); // for finding intersection point and replacing the // present co-ordinates and finding codes for new co-ordinates changePts(&(m[i].a),s),getCode(&(m[i].a)); changePts(&(m[i].b),s),getCode(&(m[i].b)); if(getAttr(m[i])==1&&chk<2) // checking if line is clipped completly goto start; // if not clipped repeat the process if(chk==2) //chk is to take care that the clipping process { chk=0; goto end; }//is not repeated more than 2 times // if its so line is to be removed completely } } else // for removing lines completely { end : int d=0; for(int j=0;j<n;j++) //finding if line is completely inside if(m[i].a.code[i]!=0||m[i].b.code[i]!=0) d=1; if(d==1) // if line is not inside { for(j=i;j<n-1;j++) // to remove the line m[j]= m[j+1]; n=n-1; i--; // to decrese no of lines } } getch(); cleardevice(); // clearing screen line(xc,0,xc,2*yc),line(0,yc,2*xc,yc); // draw axis rectangle(xc+wmin.x,yc-wmin.y,xc+wmax.x,yc-wmax.y);//draw windows for(i=0;i<n;i++)// drawing lines with the changed co-ordinates line(xc+m[i].a.x,yc-m[i].a.y,xc+m[i].b.x,yc-m[i].b.y); getch(); closegraph(); //closing graphics mode } void getCode(Point *a) //to get 4digit code { if(a->x<wmin.x) a->code[0]=1; else a->code[0]=0; if(a->x>wmax.x) a->code[1]=1; else a->code[1]=0; if(a->y<wmin.y) a->code[2]=1; else a->code[2]=0; if(a->y>wmax.y) a->code[3]=1; else a->code[3]=0; } int getAttr(Line m) //to decide whether to clip the lines or leave it { //alone (if iniside) completely remove the lines (if outside) int a=1,b=0; for(int i=0;i<4;i++) if(m.a.code[i]&m.b.code[i]!=0) a=0; if(m.a.code[i]!=0||m.b.code[i]!=0) b=1; if(a==0||b==0) return 0; return 1; } void changePts(Point *a,float s) // for finding intersection point and { //replacing the present co-ordinates and finding codes for new co-ordinates if(a->code[0]==1) a->y+=(wmin.x-a->x)*s,a->x=wmin.x; else if(a->code[1]==1) a->y+=(wmax.x-a->x)*s,a->x=wmax.x; else if(a->code[2]==1) a->x+=(wmin.y-a->y)/s,a->y=wmin.y; else if(a->code[3]==1) a->x+=(wmax.y-a->y)/s,a->y=wmax.y; } // SSR |
74 total views, no views today