线段相交判断

#include<bits/stdc++.h>
using namespace std;
struct point
{
    double x,y;
};
int ccw(point a,point b,point c)
{
    b.x=b.x-a.x;
    b.y=b.y-a.y;
    c.x=c.x-a.x;
    c.y=c.y-a.y;
    if(b.x*c.y-b.y*c.x<0) return -1;
    else if(b.x*c.y-b.y*c.x>0) return 1;
    else return 0;
}
bool inter(point a,point b,point c,point d)
{
    if(min(a.x, b.x)>max(c.x, d.x)||min(a.y, b.y)>max(c.y, d.y)|| min(c.x, d.x)>max(a.x, b.x)||min(c.y, d.y)>max(a.y, b.y))
    return false;
    return true;
}//跨立实验
int main()
{
    int n,m,i,j,k,t;
    cin>>t;
    while(t--)
    {
        point a,b,c,d;
        cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y>>d.x>>d.y;
        if(inter(a,b,c,d))
        {
            if(ccw(a,b,c)*ccw(a,b,d)<=0&&ccw(c,d,a)*ccw(c,d,b)<=0) cout<<"YES\n";
            else cout<<"NO\n";
        }
        else cout<<"NO\n";
    }
}

猜你喜欢

转载自www.cnblogs.com/ww123/p/9087885.html