C Language code:
typedef struct TAGVERTEX
{
Double X;
Double y;
}VERTEX;
Judging point and polygon relationship, 0-outside, 1-up, 2-inside
int Pointinpolygon (VERTEX &pt,vertex *vtx,int N)
{
int i,j,count=0;
Double CP;
for (i=0,j=n-1;i<n;j=i++)//j is the rear endpoint, I is the front end point
{
if (pt.y>=vtx[j].y&&pt.y<=vtx[i].y| | PT.Y>=VTX[I].Y&&PT.Y<=VTX[J].Y)//point in line y range
{
CP = (vtx[i].x-vtx[j].x) * (PT.Y-VTX[J].Y)-(pt.x-vtx[j].x) * (VTX[I].Y-VTX[J].Y);/vector ji and JP fork product
if (cp==0&& (pt.x>=vtx[j].x&&pt.x<=vtx[i].x| | pt.x>=vtx[i].x&&pt.x<=vtx[j].x))//dot on line
{
return 1;
}
if (cp>0&&vtx[i].y>vtx[j].y&&pt.y!=vtx[i].y| | CP<0&&VTX[I].Y<VTX[J].Y&&PT.Y!=VTX[J].Y)//assumes that the right horizontal ray and the line segment have intersection points with PT as the starting point
{
count++;
}
}
}
Return (count%2 2:0);
}
The above functions are tested by vc++6.0 and can correctly judge the position relationship between the points and the polygons.