POJ 1410 Intersection(判断线段是否在矩形面里)

题意:给你线段和矩形,问是否相交,或矩形包含线段;

注意:线段相交时,<(0,0),(1,0)> 和<(3,0),(4,0)> 不相交。。。。郁闷死了。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;
const double INF = 1e20;
const double EPS = 1e-6;
bool zero(double t){return -EPS<t&&t<EPS;}
struct cvector{
    double x,y;
    cvector(double a,double b){x=a,y=b;}
    cvector(){}
};
cvector operator+(cvector a,cvector b){
    return cvector(a.x+b.x,a.y+b.y);
}
cvector operator-(cvector a,cvector b){
    return cvector(b.x-a.x,b.y-a.y);
}
cvector operator*(double a,cvector b){
    return cvector(a*b.x,a*b.y);
}
double operator*(cvector a,cvector b){
    return a.x*b.x+a.y*b.y;
}
double operator^(cvector a,cvector b){
    return a.x*b.y-b.x*a.y;
}
double length(double t){return t>0?t:-t;}
double length(cvector t){return sqrt(t*t);}
struct cpoint{
    double x,y;
    cpoint(double a,double b){x=a,y=b;}
    cpoint(){}
};
cvector operator-(cpoint a,cpoint b){
    return cvector(b.x-a.x,b.y-a.y);
}
double dist(cpoint a,cpoint b){
    return length(b-a);
}
struct cline{
    cpoint a,b;
};
bool intersect(cline a,cline b){
    if(zero((a.a-a.b)^(b.a-b.b))) return false;
    return ((a.a-b.a)^(b.b-b.a))*((a.b-b.a)^(b.b-b.a))<EPS&&
    ((b.a-a.a)^(a.b-a.a))*((b.b-a.a)^(a.b-a.a))<EPS;
}
cpoint st,en;
cline b[4],tmp;
bool in(cpoint t)
{
    for(int i=0;i<4;i++)
    if(((t-b[i].a)^(b[i].b-b[i].a))>EPS)
    {
        return false;
    }
    return true;
}
int main()
{
    freopen("in.txt","r",stdin);
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%lf%lf%lf%lf",&tmp.a.x,&tmp.a.y,&tmp.b.x,&tmp.b.y);
        scanf("%lf%lf%lf%lf",&st.x,&st.y,&en.x,&en.y);
        if(st.x>en.x) swap(st.x,en.x);
        if(st.y<en.y) swap(st.y,en.y);
        b[0].a.x=st.x,b[0].a.y=en.y;b[0].b=en;
        b[1].a=en;b[1].b.x=en.x,b[1].b.y=st.y;
        b[2].a=b[1].b;b[2].b=st;
        b[3].a=st;b[3].b = b[0].a;
        if(in(tmp.a)||in(tmp.b))
        {
            printf("T\n"); continue;
        }
        bool f =false;
        for(int i=0;i<4;i++)
        if(intersect(tmp,b[i]))
        {
            f = true;break;
        }
        if(f)
        printf("T\n");
        else
        printf("F\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值