Thursday, October 28, 2010

Graphics program for polygon clipping

//Polygon clipping

#include"stdio.h"
#include"graphics.h"
struct point
{
int x;
int y;
}wmin,wmax,p1,p2,ipt,pin[50],pout[50];
int np=0,n;
void newpin();
void drawpolygon();
void showpoints();
void clippolygon()
{
float m=0.0;
int i,j;
np=0;

for(i=0,j=0;i=wmin.x||p1.x>=wmin.x&&p2.x<=wmin.x) { if(p1.x!=p2.x) m=(float)(p1.y-p2.y)/(float)(p1.x-p2.x); ipt.x=wmin.x; ipt.y=p1.y+(wmin.x-p1.x)*m; if(p1.xwmin.x) || (p1.x==wmin.x && p2.x==wmin.x))
{
pout[j]=p2;
j++;
np++;
}
else if(p1.x==wmin.x && p2.xwmin.x&&p2.x>wmin.x)
{
pout[j]=p2;
np++;
j++;
}
}
newpin();
cleardevice();
outtextxy(220,10,"After left clip");
drawpolygon();
showpoints();
getch();
// while (!kbhit());
// closegraph();
// initgraph(&gd,&gm,"NULL");
np=0;
for(i=0,j=0;i=wmax.x&&p2.x<=wmax.x||p1.x<=wmax.x&&p2.x>=wmax.x)
{
if(p1.x!=p2.x)
m=(float)(p1.y-p2.y)/(float)(p1.x-p2.x);
ipt.x=wmax.x;
ipt.y=p1.y+(wmax.x-p1.x)*m;
if(p1.x>wmax.x)
{
pout[j]=ipt;
pout[j+1]=p2;
np=np+2;
j=j+2;
}
else if((p1.x==wmax.x && p2.xwmax.x)
{
}
else
{
p2=ipt;
pout[j]=ipt;
np++;
j++;
}
}
else if(p1.x=wmin.y||p1.y>=wmin.y&&p2.y<=wmin.y) { if(p1.x!=p2.x) m=(float)(p1.y-p2.y)/(float)(p1.x-p2.x); ipt.y=wmin.y; if(m!=0) ipt.x=p1.x+(float)(wmin.y-p1.y)*(float)(1/m); else ipt.x=p1.x; if(p1.ywmin.y) || (p1.y==wmin.y && p2.y==wmin.y))
{
pout[j]=p2;
j++;
np++;
}
else if(p1.y==wmin.y && p2.ywmin.y&&p2.y>wmin.y)
{
pout[j]=p2;
np++;
j++;
}
}
newpin();

cleardevice();
outtextxy(220,10,"After bottom clip");
drawpolygon();
showpoints();
getch();
// while (!kbhit());
// closegraph();
// initgraph(&gd,&gm,"NULL");
np=0;
for(i=0,j=0;i=wmax.y&&p2.y<=wmax.y||p1.y<=wmax.y&&p2.y>=wmax.y)
{
if(p1.x!=p2.x)
m=(float)(p1.y-p2.y)/(float)(p1.x-p2.x);
ipt.y=wmax.y;
if(m!=0)
ipt.x=p1.x+(float)(wmax.y-p1.y)*(float)(1/m);
else
ipt.x=p1.x;
if(p1.y>wmax.y)
{
pout[j]=ipt;
pout[j+1]=p2;
np=np+2;
j=j+2;
}
else if((p1.y==wmax.y && p2.ywmax.y)
{
}
else
{
p2=ipt;
pout[j]=ipt;
np++;
j++;
}
}
else if(p1.y {
pout[j]=p2;
np++;
j++;
}
}
newpin();
cleardevice();
outtextxy(220,10,"clipped Polygon");
showpoints();
drawpolygon();
}
void newpin()
{
int i,j;
for(j=0,i=0;j pin[i]=pout[j];
n=i;
}
void showpoints()
{
int j;
printf("Vertex");
for(j=0;j printf("\n%d.(%d,%d)",j+1,pin[j].x,pin[j].y);
}
void drawpolygon()
{
int j;
setcolor(10);
rectangle(wmin.x,wmin.y,wmax.x,wmax.y);
setcolor(6);
for(j=0;j line(pin[j].x,pin[j].y,pin[(j+1)%n].x,pin[(j+1)%n].y);
}
int main()
{
int c,j;
int gd=DETECT,gm;
initgraph(&gd,&gm,"..//BGI");
printf("Enter the window minimum coordinates");
scanf("%d%d",&wmin.x,&wmin.y);
printf("Enter the window max coordinates");
scanf("%d%d",&wmax.x,&wmax.y);
printf("Enter the no of sides in polygon:\n");
scanf("%d",&n);
printf("Enter the coordinates(x,y)\n");
for(j=0;j{
printf("Enter the coordinate %d\n",j+1);
scanf("%d%d",&pin[j].x,&pin[j].y);
}
initgraph(&gd,&gm,"NULL");
outtextxy(220,10,"Orginal Polygon");
drawpolygon();
getch();
// while (!kbhit());
// closegraph();
// initgraph(&gd,&gm,"NULL");
clippolygon();
while (!kbhit());
closegraph();
return 0;
}

No comments: