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

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

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

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/86535969