POJ 1410 Intersection

这题我就是用最原始的思考方法,其中有许多细节要注意。主体思想就是四条边分别和线段比较。

线段在矩形内要考虑。

我的代码有点乱有点长,其中有的部分可以写成函数。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
int x_s,y_s,x_e,y_e,x_l,y_t,x_r,y_b;
bool f1(int x)
{
    return x>=x_l&&x<=x_r;
}
bool f2(int y)
{
    return y>=y_b&&y<=y_t;
}
bool judge(int x)
{
    return (x>=x_s&&x<=x_e)||(x>=x_e&&x<=x_s);
}
bool judge1(int y)
{
    return (y>=y_s&&y<=y_e)||(y>=y_e&&y<=y_s);
}
bool judge_y(double y)
{
    return y>=(double)y_b&&y<=(double)y_t;
}
bool judge_x(double x)
{
    return x>=(double)x_l&&x<=(double)x_r;
}
void _swap(int &a,int &b)
{
    if(a>b)
    {
        int t=a;
        a=b,b=t;
    }
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d%d%d%d%d%d",&x_s,&y_s,&x_e,&y_e,&x_l,&y_t,&x_r,&y_b);
        _swap(x_l,x_r);//这里要注意大小问题,题目中说了不是按顺序给的
        _swap(y_b,y_t);
        int a1=x_s,b1=y_s,a2=x_e,b2=y_e;
        _swap(a1,a2);
        _swap(b1,b2);
        if(f1(x_s)&&f1(x_e)&&f2(y_s)&&f2(y_e))
        {
            printf("T\n");
            continue;
        }
        if(x_s==x_e)//考虑斜率不存在
        {
            if(f1(x_s))
                if(f2(y_s)||f2(y_e)||(b1<=y_b&&b2>=y_t))
            {
                printf("T\n");
                continue;
            }
        }
        if(y_s==y_e)//考虑斜率为0
        {
            if(f2(y_s))
                if(f1(x_s)||f1(x_e)||(a1<=x_l&&a2>=x_r))
            {
                printf("T\n");
                continue;
            }
        }
        double k=(y_s-y_e)*1.0/(x_s-x_e);
        double b=y_s*1.0-k*x_s;
        if(judge(x_l))
        {
            double y=k*x_l+b;
            if(judge_y(y))
            {
                printf("T\n");
                continue;
            }
        }
        if(judge(x_r))
        {
            double y=k*x_r+b;
            if(judge_y(y))
            {
                printf("T\n");
                continue;
            }
        }
        if(judge1(y_b))
        {
            double x=(y_b*1.0-b)/k;
            if(judge_x(x))
            {
                printf("T\n");
                continue;
            }
        }
        if(judge1(y_t))
        {
            double x=(y_t*1.0-b)/k;
            if(judge_x(x))
            {
                printf("T\n");
                continue;
            }
        }
        printf("F\n");
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值