Line Clipping Using Cyrus-Beck Method

//LINE CLIPPING USING CYRUS-BECK METHOD #include<iostream.h> #include<conio.h> #include<graphics.h> #define round(a) (int(a+0.

5)) float max(float a,float b) { return ((a>b) ? a : b); } float min(float a,float b) { return ((a>b) ? b : a); } void main() { int gdriver=DETECT,gmode; float x,y,xmin,xmax,ymin,ymax,xi,xf,yi,yf,t,t1,t2=1; clrscr(); cout<<"Coordinates of rectangular clip window :\nxmin,ymin :"; cin>>xmin>>ymin; cout<<"xmax,ymax :"; cin>>xmax>>ymax; cout<<"\n\nCoordinates of line to be clipped :\nInitial :"; cin>>xi>>yi; cout<<"Final :"; cin>>xf>>yf; initgraph(&gdriver,&gmode,"C:\\TurboC3\\BGI"); rectangle(xmin,ymax,xmax,ymin); cout<<"\t\tUNCLIPPED LINE"; setcolor(RED); line(round(xi),round(yi),round(xf),round(yf)); getch(); cleardevice(); t=(xmin-xi)/(xf-xi); if(xf>xi) t1=max(t1,t); else t2=min(t2,t); t=(ymax-yi)/(yf-yi); if(yi>yf) t1=max(t1,t); else t2=min(t2,t); t=(xmax-xi)/(xf-xi); if(xi>xf) t1=max(t1,t); else t2=min(t2,t); t=(ymin-yi)/(yf-yi); if(yf>yi) t1=max(t1,t); else

t2=min(t2,t); if(t1<t2) { x=xf; y=yf; xf=xi+t2*(xf-xi); yf=yi+t2*(yf-yi); xi+=t1*(x-xi); yi+=t1*(y-yi); line(round(xi),round(yi),round(xf),round(yf)); } setcolor(WHITE); rectangle(xmin,ymax,xmax,ymin); cout<<"\t\tCLIPPED LINE"; getch(); closegraph(); }

Sign up to vote on this title
UsefulNot useful