您的位置:首页 > 其它

hdu1756判断点是否在多边形内部

2017-08-30 00:12 453 查看
题目链接:点击打开链接

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=1e3;
const double eps=1e-8;
struct Point//点 向量
{
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
};
typedef Point Vector;
int dcmp(double x) //三态函数 处理与double零有关的精度问题
{
if(fabs(x) < eps) return 0;
return x<0 ? -1 : 1;
}
//向量运算
Vector operator + (Vector A, Vector B)
{
return Vector(A.x+B.x, A.y+B.y);
}
Vector operator - (Vector A, Vector B)
{
return Vector(A.x-B.x, A.y-B.y);
}
bool operator == (const Vector& A, const Vector& B)
{
return dcmp(A.x-B.x)==0 && dcmp(A.y-B.y)==0;
}
bool operator < (const Point&a,const Point &b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}

double Dot(Vector A, Vector B) //向量点积
{
return A.x * B.x + A.y * B.y;
}
double Cross(Vector A, Vector B) //向量叉积
{
return A.x * B.y - A.y * B.x;
}
bool OnSegment(Point p,Point a1,Point a2)//这里包括了端点
{
return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<=0;
}
int PointInPolygon(Point cp,Point p[],int n)
{
int i,k,d1,d2,wn=0;
p
=p[0];
for( i=0;i<n;i++)
{
if(OnSegment(cp,p[i],p[i+1])) return -1;//在边界上
k=dcmp(Cross(p[i+1]-p[i],cp-p[i]));
d1=dcmp(p[i+0].y-cp.y);
d2=dcmp(p[i+1].y-cp.y);
if(k>0&&d1<=0&&d2>0)wn++;
if(k<0&&d2<=0&&d1>0)wn--;
}
return wn!=0;
}
int main()
{
int n,m;
Point Polygon[maxn];
while(cin>>n)
{
for(int i=0;i<n;i++)
cin>>Polygon[i].x>>Polygon[i].y;
cin>>m;
Point temp;
for(int j=1;j<=m;j++)
{
cin>>temp.x>>temp.y;
if(PointInPolygon(temp,Polygon,n)==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: