POJ3304


枚举所有线段交点,依次判断是否与所有线段都有交点

#include 
#include 
#include 
#include 

#define x first
#define y second

using namespace std;

typedef pair<double, double> PDD;
const int N = 210;
const double eps = 1e-8;

int n;
PDD q[N], a[N], b[N];


int sign(double x)
{
    if (fabs(x) < eps) return 0;
    if (x < 0) return -1;
    return 1;
}


int cmp(double x, double y)
{
    if (fabs(x - y) < eps) return 0;
    if (x < y) return -1;
    return 1;
}


double cross(double x1, double y1, double x2, double y2)
{
    return x1 * y2 - x2 * y1;
}

double area(PDD a, PDD b, PDD c)
{
    return cross(b.x - a.x, b.y - a.y, c.x - a.x, c.y - a.y);
}


bool check()
{
    for(int i=0;i2;i++)
    {
        for(int j=i+1;j2;j++)
        {
            if(!cmp(q[i].x,q[j].x)&&!cmp(q[i].y,q[j].y))    continue;
            bool flag = true;
            for(int k=0;k)
            {
                if(sign(area(q[i],q[j],a[k]))*sign(area(q[i],q[j],b[k]))>0)
                {
                    flag=false;
                    break;
                }
            }
            if(flag)    return true;
        }
    }
    return false;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=0,k=0;i)
        {
            double x1,y1,x2,y2;
            cin>>x1>>y1>>x2>>y2;
            q[k++]={x1,y1};
            q[k++]={x2,y2};
            a[i]={x1,y1},b[i]={x2,y2};
        }
        if(check()) cout<<"Yes!"<<endl;
        else cout<<"No!"<<endl;
    }
    return 0;
}