POJ 1410 Intersection

#include <iostream>  
#include <cstring>  
#include <algorithm>  
#include <cstdio>  
using namespace std;  
  
struct Point  
{  
    double x,y;  
};  
struct Segment  
{  
    Point p1,p2;  
};  
  
double cross(const Point& o,const Point & a,const Point& b)  
{  
    return (a.x - o.x)*(b.y-o.y) - (a.y - o.y)* (b.x - o.x);  
}  
  
double dot(const Point& o,const Point & a,const Point& b)  
{  
    return (a.x - o.x) * (b.x - o.x) + (a.y-o.y) * (b.y - o.y);  
}  
  
const double eps = 1E-8;  
int sig(double d)  
{  
    return (d > eps) - (d < -eps);  
}  
  
int btw(const Point& x,const Point& a,const Point&b)  
{  
    return sig(dot(x,a,b));  
}  
  
int SegCross(const Point & a,const Point & b,const Point& c,const Point& d)  
{  
    int d1,d2,d3,d4;  
    d1 = sig(cross(a,b,c));  
    d2 = sig(cross(a,b,d));  
    d3 = sig(cross(c,d,a));  
    d4 = sig(cross(c,d,b));  
    if((d1^d2)==-2&&(d3^d4)==-2)  
    {  
        return 1;  
  
    }  
    if(d1 == 0 && btw(c,a,b)<=0||  
            d2 == 0 && btw(d,a,b)<=0 ||  
            d3 == 0 && btw(a,c,d)<=0 ||  
            d4 == 0 && btw(b,c,d) <=0)  
        return 2;  
    return 0;  
}  
  
int main()  
{  
    int n;  
    cin >> n;  
    Segment seg;  
    Segment rseg[4];  
    Point p1,p2,p3,p4;  
    int flag;  
    while(n--)  
    {  
        flag = 0;  
        scanf("%lf%lf%lf%lf",&seg.p1.x,&seg.p1.y,&seg.p2.x,&seg.p2.y);  
        scanf("%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y);  
        p3.x = p2.x;  
        p3.y = p1.y;  
        p4.x = p1.x;  
        p4.y = p2.y;  
        flag += SegCross(seg.p1,seg.p2,p1,p3);  
        flag += SegCross(seg.p1,seg.p2,p1,p4);  
        flag += SegCross(seg.p1,seg.p2,p2,p3);  
        flag += SegCross(seg.p1,seg.p2,p2,p4);  
        //cout << "flag = " << flag << endl;  
        if(flag)  
            cout << "T" << endl;  
        else  
        {  
            if((min(p1.x,p2.x) < min(seg.p1.x,seg.p2.x )) &&  
                    (min(p1.y,p2.y) < min(seg.p1.y,seg.p2.y )) &&  
                    (max(p1.x,p2.x) > max(seg.p1.x,seg.p2.x )) &&  
                    (max(p1.y,p2.y) > max(seg.p1.y,seg.p2.y )) )  
                cout << "T" << endl;  
            else  
                cout << "F" << endl;  
        }  
  
  
    }  
    return 0;  
}  

原文地址:https://www.cnblogs.com/java20130726/p/3218253.html