📄 polygon.c
字号:
# include "polygon.h"
char MemoryError1[]="Can't allocate sufficient Memory !";
BOOL OnPolygon(POLYGON * pPoly,POINT * pX)
{
/* Judge the point x whether locates inside the polygon
Note: the polygon must be concave !!***
TRUE -- means that the point x locates in the polygon
FALSE -- means that the point x locates outside of the polygon
t -- the number of jointed point
xmin -- the minimum value of x
xmax -- the maximum value of x
ymin -- the minimum value of y
ymax -- the maximum value of y
yp -- the coordinate of the jointed point
xp -- the coordinate of the jointed point
vx[i]-- the coordinate of the ith Vertex
vy[i]-- the coordinate of the ith Vertex
draw the two straight lines through the point X and judge the point X
whether is inner point */
double xmin,ymin,xmax,ymax;
double tmp_y,tmp_x,tx,ty;
double xpmin,xpmax,ypmin,ypmax;
double *vx ,*vy,*yp,*xp;
int t ,i,flag1,flag2;
/* determine the maximum and minimum coordinate of the polygon */
vx=(double *)malloc(sizeof(double)*(pPoly->nVertex+1));
if(vx==NULL)
{
printf("\n%s\n",MemoryError1);
exit(-1);
}
vy=(double *)malloc(sizeof(double)*(pPoly->nVertex+1));
if(vy==NULL)
{
printf("\n%s\n",MemoryError1);
exit(-1);
}
yp=(double *)malloc(sizeof(double)*pPoly->nVertex);
if(yp==NULL)
{
printf("\n%s\n",MemoryError1);
exit(-1);
}
xp=(double *)malloc(sizeof(double)*pPoly->nVertex);
if(xp==NULL)
{
printf("\n%s\n",MemoryError1);
exit(-1);
}
/* temporarily store the coordinate of the polygon */
for(i=0;i<pPoly->nVertex;i++)
{
vx[i]=pPoly->pVertex[i].x;
vy[i]=pPoly->pVertex[i].y;
}
vx[pPoly->nVertex]=vx[0];
vy[pPoly->nVertex]=vy[0];
xmax=xmin=vx[0];
ymin=ymax=vy[0];
for(i=0;i<pPoly->nVertex;i++)
{
if (vx[i]>xmax) xmax=vx[i];
if (vx[i]<xmin) xmin=vx[i];
if (vy[i]>ymax) ymax=vy[i];
if (vy[i]<ymin) ymin=vy[i];
}
/* determine the point X if locates the outside of maxmium and minium */
if((pX->x>xmax)||(pX->x<xmin))
{
dfree_1(vx);
dfree_1(vy);
dfree_1(xp);
dfree_1(yp);
return FALSE;
}
if((pX->y>ymax)||(pX->y<ymin))
{
dfree_1(vx);
dfree_1(vy);
dfree_1(xp);
dfree_1(yp);
return FALSE;
}
/* determine the points locate inside the maxium and minimum */
/* draw the straight line x=x(Point X) through the Point X */
t=0;
for(i=0;i<pPoly->nVertex;i++)
{
tx=vx[i+1]-vx[i];
if(fabs(tx)>1.e-4)
{
tmp_y=((vy[i+1]-vy[i])*pX->x+vx[i+1]*vy[i]-vx[i]*vy[i+1])/tx;
if((pX->x-vx[i])*(pX->x-vx[i+1])>0.0) continue; /* No jointed */
else
{
yp[t]=tmp_y;
t++;
}
}
else
{
if ( fabs(pX->x-vx[i])>1.e-4 ) continue;
else
{
if(((pX->y-vy[i])*(pX->y-vy[i+1]))<1.e-4)
{
dfree_1(vx);
dfree_1(vy);
dfree_1(xp);
dfree_1(yp);
return TRUE ;
}
else
{
yp[t]=vy[i];
t++;
}
}
}
}
ypmax=ypmin=yp[0];
for(i=0;i<t;i++)
{
if(yp[i]>ypmax) ypmax=yp[i];
if(yp[i]<ypmin) ypmin=yp[i];
}
if ((pX->y>ypmin)&&(pX->y<ypmax)) { flag1=1 ; }
else
{
flag1=0;
}
/* draw the straight line y=y(Point X) through the Point X */
t=0;
for(i=0;i<pPoly->nVertex;i++)
{
ty=vy[i+1]-vy[i];
if((pX->y-vy[i])*(pX->y-vy[i+1])>0.0) continue;
if(fabs(ty)>1.e-4)
{
tmp_x=(vx[i+1]*(pX->y-vy[i])+vx[i]*(vy[i+1]-pX->y))/ty;
if(fabs(pX->x-tmp_x)<1.e-4){
dfree_1(vx);
dfree_1(vy);
dfree_1(xp);
dfree_1(yp);
return TRUE;
}
else
{
xp[t]=tmp_x;
t++;
}
}
else
{
if(fabs(pX->y-vy[i])>1.e-4) continue;
if((pX->x-vx[i])*(pX->x-vx[i+1])<1.e-4) {
dfree_1(vx);
dfree_1(vy);
dfree_1(xp);
dfree_1(yp);
return TRUE;
}
else
{
xp[t]=vx[i];
t++;
}
}
}
xpmax=xpmin=xp[0];
for(i=0;i<t;i++)
{
if(xp[i]>xpmax) xpmax=xp[i];
if(xp[i]<xpmin) xpmin=xp[i];
}
if ((pX->x>xpmin)&&(pX->x<xpmax)) { flag2=1;}
else
{
flag2=0;
}
/* *******************/
if((flag1&&flag2)==1)
{
dfree_1(vx);
dfree_1(vy);
dfree_1(xp);
dfree_1(yp);
return TRUE;
}
else
{
dfree_1(vx);
dfree_1(vy);
dfree_1(xp);
dfree_1(yp);
return FALSE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -