计算几何-判断点在多边形的边上内部外部

//判断点在多边形的边上内部外部

#include<bits/stdc++.h>
using namespace std;
const double eps=1e-10;
struct point
{
    double x,y;
    point operator-(point &s)
    {
        return (point)
        {
            x-s.x,y-s.y
        };
    }
} po[10000],temp;
double operator * (point a,point b)
{
    return a.x*b.y-b.x*a.y;
}
int n;
bool bian(point Q,point P1, point P2)
{
    return fabs((Q-P1)*(P2-P1))<eps&&
           min(P1.x,P2.x)-eps<=Q.x&&Q.x-eps<=max(P1.x,P2.x)
           &&min(P1.y,P2.y)-eps<=Q.y&&Q.y-eps<=max(P1.y,P2.y);
}
double multi(point p0, point p1, point p2)//j计算差乘
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool across(point s1,point e1,point s2,point e2)//判断线段是否相交(非规范相交)
{
    return
        multi(s1,e1,s2)*multi(s1,e1,e2) < -eps&&
        multi(s2,e2,s1)*multi(s2,e2,e1) < -eps;
}
int main()
{
    int n,t;
    int i,j,k;
    int count;
    point temp1;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=0; i<n; i++)
            scanf("%lf%lf",&po[i].x,&po[i].y);
        scanf("%d",&t);
        while(t--)
        {
            count=0;
            scanf("%lf%lf",&temp.x,&temp.y);
            temp1=temp;
            temp1.x+=100008;
            temp1.y+=98430;
            po[i]=po[0];
            for(i=0; i<n; i++){
                if(bian(po[i],temp,temp1)){
                    cout<<"在边上"<<endl;
                    break;
                }
            }
            for(i=0; i<n; i++)
                if(across(temp,temp1,po[i],po[i+1]))
                    count++;
            if(count==0 || count%2==0)
                printf("No\n");
            else
                printf("Yes\n");
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值