Points in the polygon within the judgment

Source: Internet
Author: User

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&GT;=VTX[I].Y&AMP;&AMP;PT.Y&LT;=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&LT;0&AMP;&AMP;VTX[I].Y&LT;VTX[J].Y&AMP;&AMP;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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.