在计算几何中,判断点是否在多边形上包括点在多边行上,点在多边形内两种情况
代码如下,模板题
#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#include<math.h>
#define N 105
#define eps 1e-9
#define pi acos(-1.0)
#define P system("pause")
using namespace std;
struct point
{
double x,y;
point(double a,double b){x=a;y=b;}
point (){};
}p[N],A;
int n;
point operator - (point A,point B)
{
return point(A.x-B.x,A.y-B.y);
}
double dot(point A,point B)//点积
{
return A.x*B.x+A.y*B.y;
}
double cross(point A,point B)//叉积
{
return A.x*B.y-A.y*B.x;
}
int dcmp(double x)//判断实数是否大于0,避免浮点误差
{
if(fabs(x)<eps) return 0;
if(x<0) return -1;
return 1;
}
int isonsegment(point a1,point a2)//判断点是否在线段上
{
if((a2.x==A.x&&a2.y==A.y)||(a1.x==A.x&&A.y==a1.y)) return 1;
return dcmp(cross(a1-A,a2-A))==0&&dcmp(dot(a1-A,a2-A))<0;
}
int isonpoly()//判断点是否在多边形内
{
int i,wn=0;
for(i=0;i<n;i++)
{
if(isonsegment(p[i],p[(i+n)%n]))
return 1;
int k=dcmp(cross(p[(i+1)%n]-p[i],A-p[i]));
int d1=dcmp(p[i].y-A.y);
int d2=dcmp(p[(i+1)%n].y-A.y);
if(k>0&&d1<=0&&d2>0) wn++;
if(k<0&&d2<=0&&d1>0) wn--;
}
if(wn!=0) return 1;
return 0;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int m;
while(scanf("%d",&n)!=EOF)
{
int i;
for(i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
scanf("%d",&m);
while(m--)
{
scanf("%lf%lf",&A.x,&A.y);
if(isonpoly())
printf("Yes\n");
else printf("No\n");
}
}
// P;
return 0;
}