Cohen Sutherland Line Clipping Algorithm C++ Code
Learn the C++ Code of Cohen Sutherland Line Clipping Algorithm.
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
#include<iostream> #include<math.h> #include<graphics.h> using namespace std; int xmin=150; int ymin=100; int xmax=450; int ymax=350; typedef struct coordinate { int x,y; char code[4]; } PT; void drawline(PT p1,PT p2); PT setcode(PT p); int visibility(PT p1,PT p2); PT resetendpt(PT p1,PT p2); int main() { int gd=DETECT,v,gm; PT p1,p2,p3,p4,ptemp; cout<<"\nEnter x1 and y1\n"; cin>>p1.x>>p1.y; cout<<"\nEnter x2 and y2\n"; cin>>p2.x>>p2.y; initgraph(&gd,&gm,"c:\\turboc3\\bgi"); rectangle(xmin,ymin,xmax,ymax); delay(500); drawline(p1,p2); delay(500); cleardevice(); delay(500); p1=setcode(p1); p2=setcode(p2); v=visibility(p1,p2); delay(500); rectangle(xmin,ymin,xmax,ymax); delay(500); switch(v) { case 0: drawline(p1,p2); break; case 1: break; case 2: p3=resetendpt(p1,p2); p4=resetendpt(p2,p1); drawline(p3,p4); break; } delay(2000); closegraph(); } void drawline(PT p1,PT p2) { line(p1.x,p1.y,p2.x,p2.y); } PT setcode(PT p) //for setting the 4 bit code { PT ptemp; if(p.y< ymin) ptemp.code[0]='1'; //Top else ptemp.code[0]='0'; if(p.y> ymax) ptemp.code[1]='1'; //Bottom else ptemp.code[1]='0'; if(p.x> xmax) ptemp.code[2]='1'; //Right else ptemp.code[2]='0'; if(p.x< xmin) ptemp.code[3]='1'; //Left else ptemp.code[3]='0'; ptemp.x=p.x; ptemp.y=p.y; return(ptemp); } int visibility(PT p1,PT p2) { int i,flag=0; for(i=0; i<4; i++) { if((p1.code[i]!='0') || (p2.code[i]!='0')) flag=1; } if(flag==0) return(0); for(i=0; i<4; i++) { if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1')) flag='0'; } if(flag==0) return(1); return(2); } PT resetendpt(PT p1,PT p2) { PT temp; int x,y,i; double m,k; if(p1.code[3]=='1') x= xmin; if(p1.code[2]=='1') x= xmax; if((p1.code[3]=='1') || (p1.code[2]=='1')) { m=(double)(p2.y-p1.y)/(p2.x-p1.x); k=(p1.y+(m*(x-p1.x))); temp.y=k; temp.x=x; for(i=0; i<4; i++) temp.code[i]=p1.code[i]; if(temp.y<=ymax && temp.y>= ymin) return (temp); } if(p1.code[0]=='1') y=ymin; if(p1.code[1]=='1') y=ymax; if((p1.code[0]=='1') || (p1.code[1]=='1')) { m=(double)(p2.y-p1.y)/(p2.x-p1.x); k=(double)p1.x+(double)(y-p1.y)/m; temp.x=k; temp.y=y; for(i=0; i<4; i++) temp.code[i]=p1.code[i]; return(temp); } else return(p1); } |
To run this C++ code you have to install at first graphics.h in Codeblocks. Learn how to install graphics.h in codeblocks .