E

来源:互联网 发布:地摊制作广告软件 编辑:程序博客网 时间:2024/05/10 03:48

判断一个点在是否在四边形的内部还是外部还是在四边形的边上,队友说是一个板子题目,就直接过了

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const double eps = 1e-8;int cmp(double x){    if(fabs(x)<eps)return 0;    if(x>0)return 1;    return -1;}struct point{    double x,y;    point(double a = 0, double b = 0):x(a),y(b){}    friend point operator-(const point &a,const point &b)    {        return point(a.x-b.x,a.y-b.y);    }};double dot(const point &a,const point &b){    return a.x*b.x + a.y*b.y;}double det(const point &a,const point &b){    return a.x*b.y - a.y*b.x;}bool PointOnSegment(point p,point s,point t){    return cmp(det(p-s,t-s))==0 && cmp(dot(p-s,p-t))<=0;}struct polygon{    int n;    point a[100];    polygon(){}    int Point_In(point t)    {        int num = 0,i,d1,d2,k;        a[n] = a[0];        for(int i = 0; i < n; i++)        {            if(PointOnSegment(t,a[i],a[i+1]))                return 2;            k = cmp(det(a[i+1]-a[i],t-a[i]));            d1 = cmp(a[i].y - t.y);            d2 = cmp(a[i+1].y - t.y);            if(k>0 && d1<=0 && d2>0)num++;            if(k<0 && d2<=0 && d1>0)num--;        }        return num != 0;    }}poly;int main(){    int T;    int s[100];    scanf("%d",&T);    while(T--)    {        int cnt = 0;        int n;        point p;        scanf("%d",&n);        scanf("%lf%lf",&p.x,&p.y);        while(n--)        {            scanf("%d",&poly.n);            for(int i = 0; i < poly.n; i++)            {                scanf("%lf%lf",&poly.a[i].x,&poly.a[i].y);            }            int ans = poly.Point_In(p);            s[cnt++] = ans;        }        for(int i = 0; i < cnt; i++)        {            if(i==0)            {                if(s[i]==2)                    cout<<"0";                else if(s[i]==1)                    cout<<"1";                else                    cout<<"2";            }            else            {                if(s[i]==2)                    cout<<" 0";                else if(s[i]==1)                    cout<<" 1";                else                    cout<<" 2";            }        }        cout<<endl;    }    return 0;}