Friday, April 3, 2009

source code

#include
#include
#include
#include
#include
class clip
{
private:
int left,bottom,top,right;
int x1,y1,x2,y2,b;
int bit1,bit2;
float m;
public:
void clipping();
void lineclip();
int check(int,int);
};
int clip::check(int x,int y)
{
int b=0000;
if(xb=b|1;
if(x>right)
b=b|2;
if(y>bottom)
b=b|4;
if(yb=b|8;
return(b);
}
void clip::clipping()
{
if((bit1&bit2)!=0)
{
x1=x2=y1=y2=0;
}
if((bit1&bit2)==0)
{
m=float(y2-y1)/float(x2-x1);
if((bit1&1)==1)
{
y1=y1+int((left-x1)*(m));
x1=left;
if((x1==left)&&(y1>top)&&(y1bit1=0000;
}
if((bit1&2)==2)
{
y1=y1+int((right-x1)*(m));
x1=right;
if((x1==right)&&(y1>top))
bit1=0000;
}
if((bit1&8)==8)
{
x1=x1+int((bottom-y1)/(m));
y1=top;
}
if((bit2&1)==1)
{
y2=y1+int(left-x1)*(m);
x2=left;
if((x2==left)&&(y2>top)&&(y2bit2=0000;
}
if((bit2&2)==2)
{
y2=y1+int((right-x1)*(m));
x2=right;
if((x2==right)&&(y2>top)&&(y2bit2=0000;
}
if((bit2&4)==4)
{
x2=x1+int((bottom-y1)/(m));
y2=bottom;
}
if((bit2&8)==8)
{
x2=x1+int((top-y1)/(m));
y2=top;
}}}
void clip::lineclip()
{
int gd=DETECT,gm;
cout<<"\n enter window coordinate(l,t,r,b)";
cin>>left>>top>>right>>bottom;
cout<<"\n enter the line coordinate";
cin>>x1>>y1>>x2>>y2;
outtextxy(400,400,"before clipping");
rectangle(left,top,right,bottom);
line(x1,y1,x2,y2);
bit1=check(x1,y1);
bit2=check(x2,y2);
getch();
closegraph();
clipping();
initgraph(&gd,&gm,"c:\\Tc\\BGI");
outtextxy(400,400,"after clipping");
if((bit1&bit2)==0)
{
cout<<"\n\tpart of the line is inside\n";
rectangle(left,top,right,bottom);
line(x1,y1,x2,y2);
getch();
}
if((bit1&bit2)!=0)
{
cout<<"\n the line is completely outside\n";
rectangle(left,top,right,bottom);
cout<<"clipped line coordinates";
cout<<"(x1,y1)("<cout<<"(x2,y2)("<}
getch();
closegraph();
}
void main()
{
clrscr();
int gd=DETECT;
int gm,ch;
char op;
clip obj;
initgraph(&gd,&gm,"c:\\Tc\\BGI");
do
{
cout<<"\n line clipping\n";
obj.lineclip();
cout<<"\n do you want to continue\n";
cin>>op;
}
while(op!='n');
getch();
closegraph();
}}


OUTPUT:Line clipping

Enter Window co-ordinate (l,t,r,b) 50 50 350 350
Enter Window co-ordinate 25 25 350 350