您的位置:首页 > 其它

ZOJ 1081 Points Within(点在多边形内判定)

2017-09-09 21:17 405 查看
ZOJ 1081 Points Within(点在多边形内判定)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=81
题意:

       给你一个简单多边形,然后给你m个点,问你这m个点分别是否在多边形内部(包括边界上)?

分析:

       直接用刘汝佳的判定点在多边形内部的模板即可.不再赘述了.

#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const double eps=1e-10;
const int maxn=1000+10;
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
return x<0?-1:1;
}
struct Point
{
double x,y;
Point(){}
Point(double x,double y):x(x),y(y){}
};
typedef Point Vector;
Vector operator-(Point A,Point B)
{
return Vector(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 InSegment(Point P,Point A,Point B)
{
return dcmp(Cross(A-B,P-A))==0 && dcmp(Dot(A-P,B-P))<=0;
}
bool PointInPolygon(Point p,Point* poly,int n)
{
int wn=0;
for(int i=0;i<n;++i)
{
if(InSegment(p,poly[i],poly[(i+1)%n])) return true;
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 true;
return false;
}
int main()
{
int n,m,kase=0;
while(scanf("%d",&n)==1 && n)
{
if(kase>0) printf("\n");
printf("Problem %d:\n",++kase);

scanf("%d",&m);
Point poly[maxn];
for(int i=0;i<n;++i)
{
scanf("%lf%lf",&poly[i].x,&poly[i].y);
}
for(int i=0;i<m;++i)
{
Point p;
scanf("%lf%lf",&p.x,&p.y);
if(PointInPolygon(p,poly,n)) printf("Within\n");
else printf("Outside\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: