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

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

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=81

题意:

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

分析:

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

AC代码:

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值