/*CSEMATTER.BLOGSPOT.IN
PROGRAM NAME-PROGRAM TO IMPLEMENT COHEN SUTHERLAND LINE CLIPING*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
struct p
{
int x,y;
}p1,p2;
void main()
{
int gd=DETECT,gm;
int i,q=0,x1,y1,x2,y2,l,b,t,r,code1[4],code2[4];
int k=0,counter1=0,counter2=0,temp[5];
clrscr();
initgraph(&gd,&gm,"");
printf("enter the value of left, right ,bottom & top value");
scanf("%d%d%d%d",&l,&r,&b,&t);
rectangle(l,t,r,b);
printf("enter the first point of line");
scanf("%d%d",&p1.x,&p1.y);
printf("enter the end point of line");
scanf("%d%d",&p2.x,&p2.y);
if(p1.x<l)
{
code1[0]=1;
}
else
code1[0]=0;
if(p1.x<r)
code1[1]=0;
else
code1[1]=1;
if(p1.y>b)
code1[2]=1;
else
code1[2]=0;
if(p1.y<t)
code1[3]=1;
else
code1[3]=0;
if(p2.x<l)
{
code2[0]=1;
}
else
code2[0]=0;
if(p2.x<r)
code2[1]=0;
else
code2[1]=1;
if(p2.y>b)
code2[2]=1;
else
code2[2]=0;
if(p2.y<t)
code2[3]=1;
else
code2[3]=0;
for(i=0;i<=3;i++)
{
if(code1[i]==0)
counter1++;
}
for(i=0;i<=3;i++)
{
if(code2[i]==0)
counter2++;
}
if((counter1==4)&&(counter2==4))
{
printf("line is completly visible");
line(p1.x,p1.y,p2.x,p2.y);
}
else
{
for(i=0;i<4;i++)
{
temp[i]=code1[i]&code2[i];
if(temp[i]==1)
{q=0;
printf("line completely invisible");
break;}
else
{
q=1;
}}
if(q==1)
{
if(p1.x<l|| p2.x<l)
{if(p1.x<l)
p1.x=l;
else
p2.x=l;}
if(p1.x>r || p2.x>r)
{if(p1.x>r)
p1.x=r;
else
p2.x=r;}
if(p1.y>b|| p2.y>b)
{if(p1.y>b)
p1.y=b;
else
p2.y=b;}
if(p1.y<t|| p2.y<t)
{if(p1.y<t)
p1.y=t;
else
p2.y=t;}
line(p1.x,p1.y,p2.x,p2.y);
}
}
getch();
closegraph();}
No comments:
Post a Comment