Polygon Clipping

Download as ".c" file http://www.ziddu.com/download/7938911/poly_clipping.zip.html
/* Polygon Clipping*/
#include
#include
int n,p,x,y,i,j;
int xwmin,ywmin,xwmax,ywmax;
int x1,y1,x2,y2,xmax,ymax;
float m;
char flag;
float dy,dx;
main()
{
int gd,gm;
int a[10][10],b[10][10],c[10][10],d[10][10],e[10][10];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc30\\bgi");
printf("\n\n\tenter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tenter the cordinates of polygon :\n\n\n ");
for(i=0;i {
printf("\tx%d y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
printf("\n\tenter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%d %d",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%d %d",&xwmax,&ywmax);
xmax = 640; ymax = 480;
cleardevice();
setcolor(35);
for(i=0;i {
line(a[i][0],ymax-a[i][1],a[i+1][0],ymax-a[i+1][1]);
}
rectangle(xwmin,ymax-ywmin,xwmax,ymax-ywmax);
getch();
setcolor(35);
clip_left(a,b);
clip_right(b,c);
clip_bottom(c,d);
clip_top(d,e);
cleardevice();
setcolor(63);
for(i=0;i {
line(e[i][0],ymax-e[i][1],e[i+1][0],ymax-e[i+1][1]);
}
getch();
}
clip_left(int a[10][10], int b[10][10])
{
p=0;flag=0;m=0;
for(i=0;i {
dy=a[i+1][1]-a[i][1];
dx=a[i+1][0]-a[i][0];
if((dx!=0)&&(dy!=0))
{
m=(dy/dx);
}
if((a[i][0]=xwmin) ) flag=0;
if((a[i][0]>=xwmin)&&(a[i+1][0]>=xwmin)) flag=1;
if((a[i][0]>=xwmin)&&(a[i+1][0] if((a[i][0] switch(flag)
{
case 0 :
x=xwmin;
if(dy!=0)
y=(int)(a[i][1]+m*(x-a[i][0]));
else
y=a[i][1];
b[p][0]=x;
b[p][1]=y;
p++;
b[p][0]=a[i+1][0];
b[p][1]=a[i+1][1];
p++;
break;
case 1 :
b[p][0]=a[i+1][0];
b[p][1]=a[i+1][1];
p++;
break;
case 2 :
x=xwmin;
if(dy!=0)
y=(int)(a[i][1]+m*(x-a[i][0]));
else
y=a[i][1];
b[p][0]=x;
b[p][1]=y;
p++;
}
}
b[p][0]=b[0][0];
b[p][1]=b[0][1];
cleardevice();
setcolor(35);
rectangle(xwmin,ymax-ywmin,xwmax,ymax-ywmax);
setcolor(63);
for(i=0;i {
line(b[i][0],ymax-b[i][1],b[i+1][0],ymax-b[i+1][1]);
getch();
}
getch();
}

clip_right(int b[10][10],int c[10][10])
{
n=p; p=0; m=0;
for(i=0;i {
dy=b[i+1][1]-b[i][1];
dx=b[i+1][0]-b[i][0];
if((dx!=0)&&(dy!=0))
{
m=(dy/dx);
}
if((b[i][0]>xwmax)&&(b[i+1][0]<=xwmax) ) flag=0;
if((b[i][0]<=xwmax)&&(b[i+1][0]<=xwmax) ) flag=1;
if((b[i][0]<=xwmax)&&(b[i+1][0]>xwmax) ) flag=2;
if((b[i][0]>xwmax)&&(b[i+1][0]>xwmax) ) flag=3;
switch(flag)
{
case 0 :
x=xwmax;
if(dy!=0)
y=(int)(b[i][1]+m*(x-b[i][0]));
else
y=b[i][1];
c[p][0]=x;
c[p][1]=y;
p++;
c[p][0]=b[i+1][0];
c[p][1]=b[i+1][1];
p++;
break;
case 1 :
c[p][0]=b[i+1][0];
c[p][1]=b[i+1][1];
p++;
break;

case 2 :
x=xwmax;
if(dy!=0)
y=(int)(b[i][1]+m*(x-b[i][0]));
else
y=b[i][1];
c[p][0]=x;
c[p][1]=y;
p++;
}
}
c[p][0]=c[0][0];
c[p][1]=c[0][1];
cleardevice();
setcolor(35);
rectangle(xwmin,ymax-ywmin,xwmax,ymax-ywmax);
setcolor(63);
for(i=0;i {
line(c[i][0],ymax-c[i][1],c[i+1][0],ymax-c[i+1][1]);
getch();
}
getch();
}

clip_bottom(int c[10][10],int d[10][10])
{
n=p; p=0; m=0;
for(i=0;i {
dy=c[i+1][1]-c[i][1];
dx=c[i+1][0]-c[i][0];
if((dx!=0)&&(dy!=0))
{
m=(dy/dx);
}
if((c[i][1]=ywmin) ) flag=0;
if((c[i][1]>=ywmin)&&(c[i+1][1]>=ywmin) ) flag=1;
if((c[i][1]>=ywmin)&&(c[i+1][1] if((c[i][1] switch(flag)
{
case 0 :
y=ywmin;
if(dx!=0)
x=abs((int)(c[i][0]+(1/m)*(y-c[i][1])));
else
x=c[i][0];
d[p][0]=x;
d[p][1]=y;
p++;
d[p][0]=c[i+1][0];
d[p][1]=c[i+1][1];
p++;
break;
case 1 :
d[p][0]=c[i+1][0];
d[p][1]=c[i+1][1];
p++;
break;
case 2 :
y=ywmin;
if(dx!=0)
x=abs((int)(c[i][0]+(1/m)*(y-c[i][1])));
else
x=c[i][0];
d[p][0]=x;
d[p][1]=y;
p++;
}
}
d[p][0]=d[0][0];
d[p][1]=d[0][1];
cleardevice();
setcolor(35);
rectangle(xwmin,ymax-ywmin,xwmax,ymax-ywmax);
setcolor(63);
for(i=0;i {
line(d[i][0],ymax-d[i][1],d[i+1][0],ymax-d[i+1][1]);
getch();
}
getch();
}

clip_top(int d[10][10],int e[10][10])
{
n=p;
p=0;
for(i=0;i {
dy=d[i+1][1]-d[i][1];
dx=d[i+1][0]-d[i][0];
if((dx!=0)&&(dy!=0))
{
m=(dy/dx);
}
if((d[i][1]>ywmax)&&(d[i+1][1]<=ywmax) ) flag=0;
if((d[i][1]<=ywmax)&&(d[i+1][1]<=ywmax) ) flag=1;
if((d[i][1]<=ywmax)&&(d[i+1][1]>ywmax) ) flag=2;
if((d[i][1]>ywmax)&&(d[i+1][1]>ywmax) ) flag=3;
switch(flag)
{
case 0 :
y=ywmax;
if(dx!=0)
x=abs((int)(d[i][0]+(1/m)*(y-d[i][1])));
else
x=d[i][0];
e[p][0]=x;
e[p][1]=y;
p++;
e[p][0]=d[i+1][0];
e[p][1]=d[i+1][1];
p++;
break;
case 1 :
e[p][0]=d[i+1][0];
e[p][1]=d[i+1][1];
p++;
break;
case 2 :
y=ywmax;
if(dx!=0)
x=abs((int)(d[i][0]+(1/m)*(y-d[i][1])));
else
x=d[i][0];
e[p][0]=x;
e[p][1]=y;
p++;
}
}/*-for()-*/
e[p][0]=e[0][0];
e[p][1]=e[0][1];
cleardevice();
setcolor(35);
rectangle(xwmin,ymax-ywmin,xwmax,ymax-ywmax);
setcolor(63);
for(i=0;i {
line(e[i][0],ymax-e[i][1],e[i+1][0],ymax-e[i+1][1]);
getch();
}
getch();
getch();
}

Download as ".c" file http://www.ziddu.com/download/7938911/poly_clipping.zip.html

0 comments: