码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1410 Intersection

时间:2015-03-04 22:44:10      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

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

线段在矩形内要考虑。

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

#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;
}


POJ 1410 Intersection

标签:

原文地址:http://blog.csdn.net/u013621213/article/details/44067191

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!