/* PROGRAM TO IMPLEMENT POLYGON CLIPPING ALGORITHM */
#include
#include
#include
#include
#include
#include
void intersection(float x1i,float y1i,float x2i,float y2i);
float xx1,yy1,xx2,yy2;
int xwmin=100,ywmax=100,xwmax=400,ywmin=400;
char chr;
void main()
{
do
{
int gd=DETECT,gm,get;
initgraph(&gd,&gm,"c:\\tc\\bgi");
//clrscr();
cleardevice();
void clip_line(float x1,float y1,float x2,float y2);
int x1,x2,y1,y2,i,n,a[4],b[4],c[4],x[15],y[15];
char ch[5][7]={"first","second","third","fourth","fifth"};
gotoxy(10,4);
printf("Give no. of vertices of polygon");
scanf("%d",&n);
for(i=0;i
{
printf("\nGive %s vertex of polygon:",ch[i]);
scanf("%d%d",&x[i],&y[i]);
}
x[i]=x[0];
y[i]=y[0];
rectangle(xwmin,ywmax,xwmax,ywmin);
settextstyle(8,0,1);
outtextxy(50,40,"Before clipping");
for(i=0;i
line(x[i],y[i],x[i+1],y[i+1]);
getch();
cleardevice();
outtextxy(50,40,"After clipping");
for(i=0;i
clip_line(x[i],y[i],x[i+1],y[i+1]);
clip_line(x[i],y[i],x[0],y[0]);
getch();
cleardevice();
printf("\n\t\tdo u want 2 continue? y/n:");
chr=getch();
}while(chr=='y' || chr=='Y');
getch();
closegraph();
}
void clip_line(float x1,float y1,float x2,float y2)
{
int temp1,temp2,temp3,temp4;
float xc1,yc1,xc2,yc2;
xc1=x1;
xc2=x2;
yc1=y1;
yc2=y2;
rectangle(xwmin,ywmax,xwmax,ywmin);
if(xwmin<=x1 && x1<=xwmax)
if(ywmax<=y1 && y1<=ywmin)
if(xwmin<=x2 && x2<=xwmax)
if(ywmax<=y2 && y2<=ywmin)
{
line(x1,y1,x2,y2);
return;
}
if((xwmin<=x1 && x1<=xwmax) &&(ywmax<=y1 && y1<=ywmin))
{
temp1=x1;
temp2=y1;
while(1)
{
intersection(x1,y1,x2,y2);
if((xx1==float(xwmin)) || (xx1==float(xwmax))
|| (yy1==float(ywmax)) || (yy1==float(ywmin)))
{
line(xx1,yy1,temp1,temp2);
return;
}
if((xx1>xwmin && xx2
{
x1=xx1;
y1=yy1;
}
else
{
x2=xx1;
y2=yy1;
}
}
}
if((xwmin<=x2 && x2<=xwmax) && (ywmax<=y2 && y2<=ywmin))
{
temp1=x2;
temp2=y2;
while(1)
{
intersection(x1,y1,x2,y2);
if((xx1==float(xwmin)) || (xx1==float(xwmax))
|| (yy1==float(ywmax)) || (yy1==float(ywmin)))
{
line(xx1,yy1,temp1,temp2);
return;
}
if((xx1>xwmin && xx2
{
x2=xx1;
y2=yy1;
}
else
{
x1=xx1;
y1=yy1;
}
}
}
while(1)
{
intersection(x1,y1,x2,y2);
if(xx1==x1 || xx1==x2 || yy1==y1 || yy1==y2)
return;
if((xx1>xwmin && xx2
{
up1:
temp1=xx1;
temp2=yy2;
while(1)
{
intersection(x1,y1,x2,y2);
if((xx1==float(xwmin)) || (xx1==float(xwmax))
|| (yy1==float(ywmax)) || (yy1==float(ywmin)))
{
line(xx1,yy1,temp1,temp2);
goto jump1;
}
if((xx1>xwmin && xx2
{
x1=xx1;
y1=yy1;
}
else
{
x2=xx1;
y2=yy1;
}
}
jump1:
while(1)
{
intersection(xc1,yc1,xc2,yc2);
if((xx1==float(xwmin)) || (xx1==float(xwmax))
|| (yy1==float(ywmax)) || (yy1==float(ywmin)))
{
line(xx1,yy1,temp1,temp2);
return;
}
if((xx1>xwmin && xx2
{
xc2=xx1;
yc2=yy1;
}
else
{
xc1=xx1;
yc1=yy1;
}
}
}
else
{
while(1)
{
intersection(xc1,yc1,xc2,yc2);
if((xx1==float(xwmin)) || (xx1==float(xwmax))
|| (yy1==float(ywmax)) || (yy1==float(ywmin)))
{
goto up1;
}
if(xx1==xc1 || xx1==xc2 || yy1==yc1 || yy1==yc2)
break;
else
{
xc1=xx1;
yc1=yy1;
}
}
while(1)
{
intersection(xc1,yc1,xc2,yc2);
if((xx1>xwmin && xc1>xwmax && yy1>ywmin && yc1>ywmax ))
{
goto up1;
}
if(xx1==x1 || xx1==x2 || yy1==y1 || yy1==y2)
return;
else
{
xc1=xx1;
yc1=yy1;
}
}
}
}
}
void intersection(float x1i,float y1i,float x2i,float y2i)
{
xx1=(x1i+x2i)/2;
yy1=(y1i+y2i)/2;
}
0 comments:
Post a Comment