#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
{
pout[j]=p2;
j++;
np++;
}
else if(p1.x==wmin.x && p2.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
{
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.x
{
}
else
{
p2=ipt;
pout[j]=ipt;
np++;
j++;
}
}
else if(p1.x
{
pout[j]=p2;
j++;
np++;
}
else if(p1.y==wmin.y && p2.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
{
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.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
n=i;
}
void showpoints()
{
int j;
printf("Vertex");
for(j=0;j
}
void drawpolygon()
{
int j;
setcolor(10);
rectangle(wmin.x,wmin.y,wmax.x,wmax.y);
setcolor(6);
for(j=0;j
}
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:
Post a Comment