code for Line clip


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{ clrscr();
 int gd,gm;
 gd=DETECT;
 gm=0;
 initgraph(&gd,&gm,"Z:\TC\BGI");
int xi1,yi1,xi2,yi2,m,xwmin=20,xwmax=300,ywmin=20,ywmax=500,x1,y1,x2,y2,b11,b12,b13,b14,b21,b22,b23,b24;
cout<<"enter end points of line";
cin>>x1>>y1>>x2>>y2;
rectangle(xwmin,ywmax,xwmax,ywmin);
line(x1,y1,x2,y2);
if(xwmin-x1>0)
{ b11=1;}
else{b11=0;}
if(x1-xwmax>0)
{ b12=1;}
else{b12=0;}
if(ywmin-y1>0)
{ b13=1;}
else{b13=0;}
if(y1-ywmax>0)
{ b14=1;}
else{b14=0;}
if(xwmin-x2>0)
{ b21=1;}
else{b21=0;}
if(x2-xwmax>0)
{ b22=1;}
else{b22=0;}
if(ywmin-y2>0)
{ b23=1;}
else{b23=0;}
if(y2-ywmax>0)
{ b24=1;}
else{b24=0;}
if(b11==0&&b12==0&&b13==0&&b14==0&&b21==0&&b22==0&&b23==0&&b24==0)
{cout<<"completely lie inside";}
else
{if(((b11&b21)==0)&&((b12&b22)==0)&&((b13&b23)==0)&&((b14&b24)==0))
 { m=(y2-y1)/(x2-x1);
   if(b11==1||b12==1)
   {if(b11==1)
    {xi1=xwmin;}
    else
    {xi1=xwmax;}
    yi1=y1+(m*(xi1-x1));
   }
   else
   {if(b13==1)
    {yi1=ywmin;}
    else
    {yi1=ywmax;}
    xi1=x1+((yi1-y1)/m);
   }
   if(b21==1||b22==1)
   {if(b21==1)
    {xi2=xwmin;}
    else
    {xi2=xwmax;}
    yi2=y2+(m*(xi2-x2));
   }
   else
   {if(b23==1)
    {yi2=ywmin;}
    else
    {yi2=ywmax;}
    xi2=x2+((yi2-y2)/m);
   }
   sleep(3);
   clrscr();
   rectangle(xwmin,ywmax,xwmax,ywmin);
   line(xi1,yi1,xi2,yi2);
  }
  else
  {cout<<"lie completely outside";}
  }
  getch();
}

0 comments :