SOURCE CODE
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
float w[5][2];
int i;
cout<<"Enter the co-ordinates of window:-\n" ;
int xmin,ymin,xmax,ymax;
cout<<"xmin"<<" "<<"ymin"<<":";
cin>>xmin>>ymin;
cout<<"xmax"<<" "<<"ymax"<<":";
cin>>xmax>>ymax;
line(xmin,ymin,xmax,ymin);
line(xmax,ymin,xmax,ymax);
line(xmax,ymax,xmin,ymax);
line(xmin,ymax,xmin,ymin);
float x1,x2,y1,y2,m;
float dy,dx;
float st[4][2];
int print[2][2];
int print1[2][2];
int print2[2][2];
int flag=0;
float final[4][2];
float final1[2][2];
char ch='y';
while(ch=='Y'||ch=='y')
{
cout<<"\nEnter the line co-ordinates:-\n" ;
int ln[2][2];
for(i=0;i<2;i++)
{
cout<<"x"<<i<<" y"<<i<<":-";
cin>>ln[i][0]>>ln[i][1]; //FOR LINE
}
for(i=0;i<1;i++)
{
line(ln[i][0],ln[i][1],ln[i+1][0],ln[i+1][1]);
}
int bit1[4];
for(i=0;i<1;i++)
{
int res1;
res1=ln[i][0]-xmin;
if(res1<0)
bit1[0]=1;
else
bit1[0]=0;
//Region code for 1st point
int res2;
res2=xmax-ln[i][0];
if(res2<0)
bit1[1]=1;
else
bit1[1]=0;
int res3;
res3=ln[i][1]-ymin;
if(res3<0)
bit1[2]=1;
else
bit1[2]=0;
int res4;
res4=ymax-ln[i][1];
if(res4<0)
bit1[3]=1;
else
bit1[3]=0;
}
int bit2[4];
for(i=1;i<2;i++)
{
int res1;
res1=ln[i][0]-xmin;
if(res1<0)
bit2[0]=1;
else
bit2[0]=0;
//Region code for 2nd point
int res2;
res2=xmax-ln[i][0];
if(res2<0)
bit2[1]=1;
else
bit2[1]=0;
int res3;
res3=ln[i][1]-ymin;
if(res3<0)
bit2[2]=1;
else
bit2[2]=0;
int res4;
res4=ymax-ln[i][1];
if(res4<0)
bit2[3]=1;
else
bit2[3]=0;
}
int sum1=0,sum2=0;
cout<<"\nRegion code for 1st point:-";
for(i=3;i>=0;i--)
{ //Printing region code
sum1+=bit1[i];
cout<<bit1[i];
}
cout<<"\nRegion code for 2nd point:-";
for(i=3;i>=0;i--)
{ sum2+=bit2[i]; //Printing region code
cout<<bit2[i];
}
int rsum=0 ;
int res[4];
for(i=0;i<4;i++)
{
res[i]=bit1[i]&bit2[i];
}
cout<<"\n\nResult of AND operation is:-"; //AND oprn on Region code
for(i=3;i>=0;i--)
{
rsum+=res[i];
cout<<res[i];
}
if((sum1==0)&&(sum2==0))
{
cout<<"\nLine is completely inside the window!!";
for(i=0;i<1;i++)
{
setcolor(GREEN);
line(ln[i][0],ln[i][1],ln[i+1][0],ln[i+1][1]);
}
}
else if(rsum>0)
{
cout<<"\nLine is completely outside the window!!";
}
else
{
if(sum1>=1&&sum2>=1)
{
cout<<"\nLine is partially inside the window!!";
dy=ln[1][1]-ln[0][1];
dx=ln[1][0]-ln[0][0];
m=dy/dx;
y1=ln[0][1]+(m*(xmin-ln[0][0])) ; //left intercept
y2=ln[0][1]+(m*(xmax-ln[0][0])) ; //right intercept
x1=ln[0][0]+((ymax-ln[0][1])/m); //top intercept
x2=ln[0][0]+((ymin-ln[0][1])/m); //bottom intercept
st[0][0]=xmin; //storing left-intercept co-ordinate
st[0][1]=y1;
st[1][0]=xmax; //storing right-intercept co-ordinate
st[1][1]=y2;
st[2][0]=x1; //storing top-intercept co-ordinate
st[2][1]=ymax;
st[3][0]=x2; //storing bottom-intercept co-ordinate
st[3][1]=ymin;
cout<<"\nLeft,Right,Top & Bottom Co-ordinates resp.:-";
for(i=0;i<4;i++)
{
cout<<"\n";
cout<<"x"<<i<<" y"<<i<<":-"<<st[i][0]<<" "<<st[i][1];
}
cout<<"\nVisible portion of the line is:-" ;
for(i=0;i<4;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final[i][0]=st[i][0];
final[i][1]=st[i][1];
print1[0][0]=st[i][0];
print1[0][1]=st[i][1];
++flag;
if(flag==1)
{
print2[0][0]=st[i][0];
print2[0][1]=st[i][1];
}
cout<<"("<<final[i][0]<<","<<final[i][1]<<")";
}
}
setcolor(GREEN);
line(print1[0][0],print1[0][1],print2[0][0],print2[0][1]);
//if((final[0][0]==st[0][0])&&(final[0][1]==st[0][1]))
} //if close
else if(sum1==1&&sum2==0)
{
cout<<"\nLine is partially inside the window!!";
dy=ln[1][1]-ln[0][1];
dx=ln[1][0]-ln[0][0];
m=dy/dx;
//compairing region codes
if(bit1[3]==0 && bit1[2]==0 && bit1[1]==0 && bit1[0]==1)
{
y1=ln[0][1]+(m*(xmin-ln[0][0])) ; //left intercept
st[0][0]=xmin; //storing left-intercept co-ordinate
st[0][1]=y1;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
else if(bit1[3]==0 && bit1[2]==0 && bit1[1]==1 && bit1[0]==0)
{
y2=ln[0][1]+(m*(xmax-ln[0][0])) ; //right intercept
st[0][0]=xmax; //storing right-intercept co-ordinate
st[0][1]=y2;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
else if(bit1[3]==1 && bit1[2]==0 && bit1[1]==0 && bit1[0]==0)
{
x1=ln[0][0]+((ymax-ln[0][1])/m); //top intercept
st[0][0]=x1; //storing top-intercept co-ordinate
st[0][1]=ymax;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
else if(bit1[3]==0 && bit1[2]==1 && bit1[1]==0 && bit1[0]==0)
{
x2=ln[0][0]+((ymin-ln[0][1])/m); //bottom intercept
st[0][0]=x2; //storing bottom-intercept co-ordinate
st[0][1]=ymin;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
}
else if(sum1==0&&sum2==1)
{
cout<<"\nLine is partially inside the window!!";
dy=ln[1][1]-ln[0][1];
dx=ln[1][0]-ln[0][0];
m=dy/dx;
//compairing region codes
if(bit2[3]==0 && bit2[2]==0 && bit2[1]==0 && bit2[0]==1)
{
y1=ln[0][1]+(m*(xmin-ln[0][0])) ; //left intercept
st[0][0]=xmin; //storing left-intercept co-ordinate
st[0][1]=y1;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
else if(bit2[3]==0 && bit2[2]==0 && bit2[1]==1 && bit2[0]==0)
{
y2=ln[0][1]+(m*(xmax-ln[0][0])) ; //right intercept
st[0][0]=xmax; //storing right-intercept co-ordinate
st[0][1]=y2;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
else if(bit2[3]==1 && bit2[2]==0 && bit2[1]==0 && bit2[0]==0)
{
x1=ln[0][0]+((ymax-ln[0][1])/m); //top intercept
st[0][0]=x1; //storing top-intercept co-ordinate
st[0][1]=ymax;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
else if(bit2[3]==0 && bit2[2]==1 && bit2[1]==0 && bit2[0]==0)
{
x2=ln[0][0]+((ymin-ln[0][1])/m); //bottom intercept
st[0][0]=x2; //storing bottom-intercept co-ordinate
st[0][1]=ymin;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
}
else if(sum1==2&&sum2==0)
{
cout<<"\n\nLine is partially inside the window!!";
dy=ln[1][1]-ln[0][1];
dx=ln[1][0]-ln[0][0];
m=dy/dx;
if(bit1[3]==0 && bit1[2]==1 && bit1[1]==0 && bit1[0]==1)
{
y1=ln[0][1]+(m*(xmin-ln[0][0])) ; //left intercept
x2=ln[0][0]+((ymin-ln[0][1])/m); //bottom intercept
st[0][0]=xmin; //storing left-intercept co-ordinate
st[0][1]=y1;
st[1][0]=x2; //storing bottom-intercept co-ordinate
st[1][1]=ymin;
cout<<"\nLeft intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nBottom intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
if(bit1[3]==0 && bit1[2]==1 && bit1[1]==1 && bit1[0]==0)
{
y2=ln[0][1]+(m*(xmax-ln[0][0])) ; //right intercept
x2=ln[0][0]+((ymin-ln[0][1])/m); //bottom intercept
st[0][0]=xmax; //storing right-intercept co-ordinate
st[0][1]=y2;
st[1][0]=x2; //storing bottom-intercept co-ordinate
st[1][1]=ymin;
cout<<"\nRight intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nBottom intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
if(bit1[3]==1 && bit1[2]==0 && bit1[1]==1 && bit1[0]==0)
{
y2=ln[0][1]+(m*(xmax-ln[0][0])) ; //right intercept
x1=ln[0][0]+((ymax-ln[0][1])/m); //top intercept
st[0][0]=xmax; //storing right-intercept co-ordinate
st[0][1]=y2;
st[1][0]=x1; //storing top-intercept co-ordinate
st[1][1]=ymax;
cout<<"\nRight intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nTop intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
if(bit1[3]==1 && bit1[2]==0 && bit1[1]==0 && bit1[0]==1)
{
y1=ln[0][1]+(m*(xmin-ln[0][0])) ; //left intercept
x1=ln[0][0]+((ymax-ln[0][1])/m); //top intercept
st[0][0]=xmin; //storing left-intercept co-ordinate
st[0][1]=y1;
st[1][0]=x1; //storing top-intercept co-ordinate
st[1][1]=ymax;
cout<<"\nLeft intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nTop intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
}
else if(sum1==0&&sum2==2)
{
cout<<"\n\nLine is partially inside the window!!";
dy=ln[1][1]-ln[0][1];
dx=ln[1][0]-ln[0][0];
m=dy/dx;
if(bit2[3]==0 && bit2[2]==1 && bit2[1]==0 && bit2[0]==1)
{
y1=ln[0][1]+(m*(xmin-ln[0][0])) ; //left intercept
x2=ln[0][0]+((ymin-ln[0][1])/m); //bottom intercept
st[0][0]=xmin; //storing left-intercept co-ordinate
st[0][1]=y1;
st[1][0]=x2; //storing bottom-intercept co-ordinate
st[1][1]=ymin;
cout<<"\nLeft intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nBottom intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
if(bit2[3]==0 && bit2[2]==1 && bit2[1]==1 && bit2[0]==0)
{
y2=ln[0][1]+(m*(xmax-ln[0][0])) ; //right intercept
x2=ln[0][0]+((ymin-ln[0][1])/m); //bottom intercept
st[0][0]=xmax; //storing right-intercept co-ordinate
st[0][1]=y2;
st[1][0]=x2; //storing bottom-intercept co-ordinate
st[1][1]=ymin;
cout<<"\nRight intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nBottom intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
if(bit2[3]==1 && bit2[2]==0 && bit2[1]==1 && bit2[0]==0)
{
y2=ln[0][1]+(m*(xmax-ln[0][0])) ; //right intercept
x1=ln[0][0]+((ymax-ln[0][1])/m); //top intercept
st[0][0]=xmax; //storing right-intercept co-ordinate
st[0][1]=y2;
st[1][0]=x1; //storing top-intercept co-ordinate
st[1][1]=ymax;
cout<<"\nRight intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nTop intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
if(bit2[3]==1 && bit2[2]==0 && bit2[1]==0 && bit2[0]==1)
{
y1=ln[0][1]+(m*(xmin-ln[0][0])) ; //left intercept
x1=ln[0][0]+((ymax-ln[0][1])/m); //top intercept
st[0][0]=xmin; //storing left-intercept co-ordinate
st[0][1]=y1;
st[1][0]=x1; //storing top-intercept co-ordinate
st[1][1]=ymax;
cout<<"\nLeft intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nTop intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
}
} //else close
Label1:
cout<<"\n\nDo You Wanna Continue(Y/N):-";
cin>>ch;
} //while close
closegraph();
}
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
float w[5][2];
int i;
cout<<"Enter the co-ordinates of window:-\n" ;
int xmin,ymin,xmax,ymax;
cout<<"xmin"<<" "<<"ymin"<<":";
cin>>xmin>>ymin;
cout<<"xmax"<<" "<<"ymax"<<":";
cin>>xmax>>ymax;
line(xmin,ymin,xmax,ymin);
line(xmax,ymin,xmax,ymax);
line(xmax,ymax,xmin,ymax);
line(xmin,ymax,xmin,ymin);
float x1,x2,y1,y2,m;
float dy,dx;
float st[4][2];
int print[2][2];
int print1[2][2];
int print2[2][2];
int flag=0;
float final[4][2];
float final1[2][2];
char ch='y';
while(ch=='Y'||ch=='y')
{
cout<<"\nEnter the line co-ordinates:-\n" ;
int ln[2][2];
for(i=0;i<2;i++)
{
cout<<"x"<<i<<" y"<<i<<":-";
cin>>ln[i][0]>>ln[i][1]; //FOR LINE
}
for(i=0;i<1;i++)
{
line(ln[i][0],ln[i][1],ln[i+1][0],ln[i+1][1]);
}
int bit1[4];
for(i=0;i<1;i++)
{
int res1;
res1=ln[i][0]-xmin;
if(res1<0)
bit1[0]=1;
else
bit1[0]=0;
//Region code for 1st point
int res2;
res2=xmax-ln[i][0];
if(res2<0)
bit1[1]=1;
else
bit1[1]=0;
int res3;
res3=ln[i][1]-ymin;
if(res3<0)
bit1[2]=1;
else
bit1[2]=0;
int res4;
res4=ymax-ln[i][1];
if(res4<0)
bit1[3]=1;
else
bit1[3]=0;
}
int bit2[4];
for(i=1;i<2;i++)
{
int res1;
res1=ln[i][0]-xmin;
if(res1<0)
bit2[0]=1;
else
bit2[0]=0;
//Region code for 2nd point
int res2;
res2=xmax-ln[i][0];
if(res2<0)
bit2[1]=1;
else
bit2[1]=0;
int res3;
res3=ln[i][1]-ymin;
if(res3<0)
bit2[2]=1;
else
bit2[2]=0;
int res4;
res4=ymax-ln[i][1];
if(res4<0)
bit2[3]=1;
else
bit2[3]=0;
}
int sum1=0,sum2=0;
cout<<"\nRegion code for 1st point:-";
for(i=3;i>=0;i--)
{ //Printing region code
sum1+=bit1[i];
cout<<bit1[i];
}
cout<<"\nRegion code for 2nd point:-";
for(i=3;i>=0;i--)
{ sum2+=bit2[i]; //Printing region code
cout<<bit2[i];
}
int rsum=0 ;
int res[4];
for(i=0;i<4;i++)
{
res[i]=bit1[i]&bit2[i];
}
cout<<"\n\nResult of AND operation is:-"; //AND oprn on Region code
for(i=3;i>=0;i--)
{
rsum+=res[i];
cout<<res[i];
}
if((sum1==0)&&(sum2==0))
{
cout<<"\nLine is completely inside the window!!";
for(i=0;i<1;i++)
{
setcolor(GREEN);
line(ln[i][0],ln[i][1],ln[i+1][0],ln[i+1][1]);
}
}
else if(rsum>0)
{
cout<<"\nLine is completely outside the window!!";
}
else
{
if(sum1>=1&&sum2>=1)
{
cout<<"\nLine is partially inside the window!!";
dy=ln[1][1]-ln[0][1];
dx=ln[1][0]-ln[0][0];
m=dy/dx;
y1=ln[0][1]+(m*(xmin-ln[0][0])) ; //left intercept
y2=ln[0][1]+(m*(xmax-ln[0][0])) ; //right intercept
x1=ln[0][0]+((ymax-ln[0][1])/m); //top intercept
x2=ln[0][0]+((ymin-ln[0][1])/m); //bottom intercept
st[0][0]=xmin; //storing left-intercept co-ordinate
st[0][1]=y1;
st[1][0]=xmax; //storing right-intercept co-ordinate
st[1][1]=y2;
st[2][0]=x1; //storing top-intercept co-ordinate
st[2][1]=ymax;
st[3][0]=x2; //storing bottom-intercept co-ordinate
st[3][1]=ymin;
cout<<"\nLeft,Right,Top & Bottom Co-ordinates resp.:-";
for(i=0;i<4;i++)
{
cout<<"\n";
cout<<"x"<<i<<" y"<<i<<":-"<<st[i][0]<<" "<<st[i][1];
}
cout<<"\nVisible portion of the line is:-" ;
for(i=0;i<4;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final[i][0]=st[i][0];
final[i][1]=st[i][1];
print1[0][0]=st[i][0];
print1[0][1]=st[i][1];
++flag;
if(flag==1)
{
print2[0][0]=st[i][0];
print2[0][1]=st[i][1];
}
cout<<"("<<final[i][0]<<","<<final[i][1]<<")";
}
}
setcolor(GREEN);
line(print1[0][0],print1[0][1],print2[0][0],print2[0][1]);
//if((final[0][0]==st[0][0])&&(final[0][1]==st[0][1]))
} //if close
else if(sum1==1&&sum2==0)
{
cout<<"\nLine is partially inside the window!!";
dy=ln[1][1]-ln[0][1];
dx=ln[1][0]-ln[0][0];
m=dy/dx;
//compairing region codes
if(bit1[3]==0 && bit1[2]==0 && bit1[1]==0 && bit1[0]==1)
{
y1=ln[0][1]+(m*(xmin-ln[0][0])) ; //left intercept
st[0][0]=xmin; //storing left-intercept co-ordinate
st[0][1]=y1;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
else if(bit1[3]==0 && bit1[2]==0 && bit1[1]==1 && bit1[0]==0)
{
y2=ln[0][1]+(m*(xmax-ln[0][0])) ; //right intercept
st[0][0]=xmax; //storing right-intercept co-ordinate
st[0][1]=y2;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
else if(bit1[3]==1 && bit1[2]==0 && bit1[1]==0 && bit1[0]==0)
{
x1=ln[0][0]+((ymax-ln[0][1])/m); //top intercept
st[0][0]=x1; //storing top-intercept co-ordinate
st[0][1]=ymax;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
else if(bit1[3]==0 && bit1[2]==1 && bit1[1]==0 && bit1[0]==0)
{
x2=ln[0][0]+((ymin-ln[0][1])/m); //bottom intercept
st[0][0]=x2; //storing bottom-intercept co-ordinate
st[0][1]=ymin;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
}
else if(sum1==0&&sum2==1)
{
cout<<"\nLine is partially inside the window!!";
dy=ln[1][1]-ln[0][1];
dx=ln[1][0]-ln[0][0];
m=dy/dx;
//compairing region codes
if(bit2[3]==0 && bit2[2]==0 && bit2[1]==0 && bit2[0]==1)
{
y1=ln[0][1]+(m*(xmin-ln[0][0])) ; //left intercept
st[0][0]=xmin; //storing left-intercept co-ordinate
st[0][1]=y1;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
else if(bit2[3]==0 && bit2[2]==0 && bit2[1]==1 && bit2[0]==0)
{
y2=ln[0][1]+(m*(xmax-ln[0][0])) ; //right intercept
st[0][0]=xmax; //storing right-intercept co-ordinate
st[0][1]=y2;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
else if(bit2[3]==1 && bit2[2]==0 && bit2[1]==0 && bit2[0]==0)
{
x1=ln[0][0]+((ymax-ln[0][1])/m); //top intercept
st[0][0]=x1; //storing top-intercept co-ordinate
st[0][1]=ymax;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
else if(bit2[3]==0 && bit2[2]==1 && bit2[1]==0 && bit2[0]==0)
{
x2=ln[0][0]+((ymin-ln[0][1])/m); //bottom intercept
st[0][0]=x2; //storing bottom-intercept co-ordinate
st[0][1]=ymin;
cout<<"\nVisible portion of the line is:";
cout<<"("<<st[0][0]<<","<<st[0][1]<<") to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(st[0][0],st[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
}
else if(sum1==2&&sum2==0)
{
cout<<"\n\nLine is partially inside the window!!";
dy=ln[1][1]-ln[0][1];
dx=ln[1][0]-ln[0][0];
m=dy/dx;
if(bit1[3]==0 && bit1[2]==1 && bit1[1]==0 && bit1[0]==1)
{
y1=ln[0][1]+(m*(xmin-ln[0][0])) ; //left intercept
x2=ln[0][0]+((ymin-ln[0][1])/m); //bottom intercept
st[0][0]=xmin; //storing left-intercept co-ordinate
st[0][1]=y1;
st[1][0]=x2; //storing bottom-intercept co-ordinate
st[1][1]=ymin;
cout<<"\nLeft intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nBottom intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
if(bit1[3]==0 && bit1[2]==1 && bit1[1]==1 && bit1[0]==0)
{
y2=ln[0][1]+(m*(xmax-ln[0][0])) ; //right intercept
x2=ln[0][0]+((ymin-ln[0][1])/m); //bottom intercept
st[0][0]=xmax; //storing right-intercept co-ordinate
st[0][1]=y2;
st[1][0]=x2; //storing bottom-intercept co-ordinate
st[1][1]=ymin;
cout<<"\nRight intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nBottom intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
if(bit1[3]==1 && bit1[2]==0 && bit1[1]==1 && bit1[0]==0)
{
y2=ln[0][1]+(m*(xmax-ln[0][0])) ; //right intercept
x1=ln[0][0]+((ymax-ln[0][1])/m); //top intercept
st[0][0]=xmax; //storing right-intercept co-ordinate
st[0][1]=y2;
st[1][0]=x1; //storing top-intercept co-ordinate
st[1][1]=ymax;
cout<<"\nRight intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nTop intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
if(bit1[3]==1 && bit1[2]==0 && bit1[1]==0 && bit1[0]==1)
{
y1=ln[0][1]+(m*(xmin-ln[0][0])) ; //left intercept
x1=ln[0][0]+((ymax-ln[0][1])/m); //top intercept
st[0][0]=xmin; //storing left-intercept co-ordinate
st[0][1]=y1;
st[1][0]=x1; //storing top-intercept co-ordinate
st[1][1]=ymax;
cout<<"\nLeft intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nTop intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[1][0]<<","<<ln[1][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[1][0],ln[1][1]);
goto Label1;
}
}
else if(sum1==0&&sum2==2)
{
cout<<"\n\nLine is partially inside the window!!";
dy=ln[1][1]-ln[0][1];
dx=ln[1][0]-ln[0][0];
m=dy/dx;
if(bit2[3]==0 && bit2[2]==1 && bit2[1]==0 && bit2[0]==1)
{
y1=ln[0][1]+(m*(xmin-ln[0][0])) ; //left intercept
x2=ln[0][0]+((ymin-ln[0][1])/m); //bottom intercept
st[0][0]=xmin; //storing left-intercept co-ordinate
st[0][1]=y1;
st[1][0]=x2; //storing bottom-intercept co-ordinate
st[1][1]=ymin;
cout<<"\nLeft intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nBottom intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
if(bit2[3]==0 && bit2[2]==1 && bit2[1]==1 && bit2[0]==0)
{
y2=ln[0][1]+(m*(xmax-ln[0][0])) ; //right intercept
x2=ln[0][0]+((ymin-ln[0][1])/m); //bottom intercept
st[0][0]=xmax; //storing right-intercept co-ordinate
st[0][1]=y2;
st[1][0]=x2; //storing bottom-intercept co-ordinate
st[1][1]=ymin;
cout<<"\nRight intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nBottom intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
if(bit2[3]==1 && bit2[2]==0 && bit2[1]==1 && bit2[0]==0)
{
y2=ln[0][1]+(m*(xmax-ln[0][0])) ; //right intercept
x1=ln[0][0]+((ymax-ln[0][1])/m); //top intercept
st[0][0]=xmax; //storing right-intercept co-ordinate
st[0][1]=y2;
st[1][0]=x1; //storing top-intercept co-ordinate
st[1][1]=ymax;
cout<<"\nRight intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nTop intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
if(bit2[3]==1 && bit2[2]==0 && bit2[1]==0 && bit2[0]==1)
{
y1=ln[0][1]+(m*(xmin-ln[0][0])) ; //left intercept
x1=ln[0][0]+((ymax-ln[0][1])/m); //top intercept
st[0][0]=xmin; //storing left-intercept co-ordinate
st[0][1]=y1;
st[1][0]=x1; //storing top-intercept co-ordinate
st[1][1]=ymax;
cout<<"\nLeft intercept:";
cout<<st[0][0]<<","<<st[0][1];
cout<<"\nTop intercept:";
cout<<st[1][0]<<","<<st[1][1];
cout<<"\n\nVisible portion of the line is:";
for(i=0;i<2;i++)
{
if(((st[i][0]>=xmin)&&(st[i][0]<=xmax))&&((st[i][1]>=ymin)&&(st[i][1]<=ymax)))
{
final1[i][0]=st[i][0];
final1[i][1]=st[i][1];
print[0][0]=st[i][0];
print[0][1]=st[i][1];
cout<<"("<<final1[i][0]<<","<<final1[i][1]<<")";
}
}
cout<<" to ("<<ln[0][0]<<","<<ln[0][1]<<")";
setcolor(GREEN);
line(print[0][0],print[0][1],ln[0][0],ln[0][1]);
goto Label1;
}
}
} //else close
Label1:
cout<<"\n\nDo You Wanna Continue(Y/N):-";
cin>>ch;
} //while close
closegraph();
}
OUTPUT
No comments:
Post a Comment