POJ 1410 Intersection

和矩形某一条边相交,则输出T

在内部,则输出T

题目描述不对,后台数据的矩形坐标不保证是左上角和右下角,也可能是左下角和右上角,所以预先处理一下。

#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<list>
#include<algorithm>
using namespace std;

int T;
double xstart,ystart,xend,yend;
double X1,Y1,X2,Y2;
double a,b,c,d;

const double eps=1e-8;
#define zero(x)(((x)>0?(x):(-x))<eps)

struct point
{
    double x;
    double y;
    point (double a,double b){x=a;y=b;}
};

double xmult(point p1,point p2,point p0)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

int dots_inline(point p1,point p2,point p3)
{
    return zero(xmult(p1,p2,p3));
}

int same_side(point p1,point p2,point l1,point l2)
{
    return xmult(l1,p1,l2)*xmult(l1,p2,l2)>eps;
}

int dot_online_in(point p,point l1,point l2)
{
    return zero(xmult(p,l1,l2))&&(l1.x-p.x)*(l2.x-p.x)<eps&&(l1.y-p.y)*(l2.y-p.y)<eps;
}

int intersect_in(point u1,point u2,point v1,point v2)
{
    if(!dots_inline(u1,u2,v1)||!dots_inline(u1,u2,v2)) return !same_side(u1,u2,v1,v2)&&!same_side(v1,v2,u1,u2);
    return dot_online_in(u1,v1,v2)||dot_online_in(u2,v1,v2)||dot_online_in(v1,u1,u2)||dot_online_in(v2,u1,u2);
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lf%lf%lf%lf",&xstart,&ystart,&xend,&yend);
        scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
        bool fail=0;
        point la(xstart,ystart);
        point lb(xend,yend);
        X1=min(a,c);
        X2=max(a,c);
        Y1=max(b,d);
        Y2=min(b,d);
        point pa(X1,Y1);
        point pb(X2,Y1);
        point pc(X1,Y2);
        point pd(X2,Y2);

        //和矩形某一条边相交,则输出T
        if(intersect_in(la,lb,pa,pb)) fail=1;
        if(intersect_in(la,lb,pa,pc)) fail=1;
        if(intersect_in(la,lb,pd,pb)) fail=1;
        if(intersect_in(la,lb,pd,pc)) fail=1;

        //在内部,则输出T
        if(X1<=xstart&&xstart<=X2&&X1<=xend&&xend<=X2&&Y2<=ystart&&ystart<=Y1&&Y2<=yend&&yend<=Y1) fail=1;
        if(fail) printf("T\n");
        else printf("F\n");
    }
    return 0;
}

 

posted @ 2016-01-23 09:56  Fighting_Heart  阅读(216)  评论(0编辑  收藏  举报