Utility 4

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,x[10],y[10],pcount=0,ncount=0,z1,z2,k;
clrscr();
printf("Enter n the number of vertices of polygon: ");
scanf("%d",&n);

printf("Enter Coordinates of n vertices: ");
for(i=0;i<n;i++)
{
scanf("%d%d",&x[i],&y[i]);
}
printf("Enter point z1 and z2 to sort: ");
scanf("%d%d",&z1,&z2);
for(i=0;i<n-1;i++)
{
k=(x[i+1]-x[i])*(z2-y[i])-(z1-x[i])*(y[i+1]-y[i]);
if(k>0)
pcount++;
else if(k<0)
ncount++;
}
k=(x[0]-x[n-1])*(z2-y[n-1])-(z1-x[n-1])*(y[0]-y[n-1]);
if(k>0)
pcount++;
else if(k<0)
ncount++;
if((pcount==n) || (ncount==n))
printf("[%d %d] is Inside the polygon \n",z1,z2);
else if((pcount>=1) && (ncount>=1))
printf("[%d %d] is Outside the polygon \n",z1,z2);
else
printf("[%d %d] is on the polygon \n",z1,z2);
getch();
}

//Output
// For point z1 and z2 to sort: 2 5
Enter n the number of vertices of polygon: 4
Enter Coordinates of n vertices: 0 2
2 4
4 3
4 1
Enter point z1 and z2 to sort: 2 5
[2 5] is Outside the polygon

//For point z1 and z2 to sort: 4 2
Enter n the number of vertices of polygon: 4
Enter Coordinates of n vertices: 0 2
2 4
4 3
4 1
Enter point z1 and z2 to sort: 4 2
[4 2] is on the polygon

//For point z1 and z2 to sort: 2 3
Enter n the number of vertices of polygon: 4
Enter Coordinates of n vertices: 0 2
2 4
4 3
4 1
Enter point z1 and z2 to sort: 2 3
[2 3] is Inside the polygon

No comments:

Post a Comment