算法的思路是从点处引出一条向右的射线,逆时针穿过时wn++,顺时针穿过时wn--,何为顺时针穿过呢,就是向量poly[i]-p在向量poly[i]-ploy[i+1]的顺时针方向,逆时针同理。
#include<bits/stdc++.h>
using namespace std;
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y) { }
};
typedef Point Vector ;
struct Polygon
{
double x,y;
Polygon(double x=0,double y=0):x(x),y(y) { }
}poly[1005];
double Dot(Vector A,Vector B)
{
return A.x*B.x+A.y*B.y;
}
bool operator == (Point A,Polygon B)
{
if(A.x==B.x&&A.y==B.y) return 1;
return 0;
}
Vector operator - (Polygon A,Point B)
{
return Vector (A.x-B.x,A.y-B.y);
}
double Length(Vector A)
{
return sqrt(Dot(A,A));
}
bool isPointOnsegment(Point p,Polygon a,Polygon b)
{
if(p==a||p==b) return 1;//线段的端点
if(Dot(a-p,b-p)==-1*Length(a-p)*Length(b-p))//线段内
return 1;
return 0;
}
double eps=1e-10;
double dcmp(double x)
{
if(fabs(x)<eps) return 0;
return x<0?-1:1;
}
int isPointInPolygon(Point p,Polygon poly)
{
int wn=0;
int n;
for(int i=0;i<n;i++)
{
if(isPointOnsegment(p,poly[i],poly[(i+1)%n])) return -1;//边界
int k=dcmp(Cross(poly[(i+1)%n]-poly[i],p-poly[i]));
int d1=dcmp(poly[i].y-p.y);
int d2=dcmp(poly[(i+1)%n].y-p.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()
{
return 0;
}