1410 Intersection

本文介绍了一个程序设计问题,即如何判断一条线段是否与一个矩形相交。通过具体的输入输出示例和C++代码实现,详细解释了判断逻辑,包括特殊情况处理。

Description

You are to write a program that has to decide whether a given line segment intersects a given rectangle.

An example:
line: start point: (4,9)
end point: (11,2)
rectangle: left-top: (1,5)
right-bottom: (7,1)
这里写图片描述
Figure 1: Line segment does not intersect rectangle

The line is said to intersect the rectangle if the line and the rectangle have at least one point in common. The rectangle consists of four straight lines and the area in between. Although all input values are integer numbers, valid intersection points do not have to lay on the integer grid.

Input

The input consists of n test cases. The first line of the input file contains the number n. Each following line contains one test case of the format:
xstart ystart xend yend xleft ytop xright ybottom

where (xstart, ystart) is the start and (xend, yend) the end point of the line and (xleft, ytop) the top left and (xright, ybottom) the bottom right corner of the rectangle. The eight numbers are separated by a blank. The terms top left and bottom right do not imply any ordering of coordinates.

Output

For each test case in the input file, the output file should contain a line consisting either of the letter “T” if the line segment intersects the rectangle or the letter “F” if the line segment does not intersect the rectangle.

Sample Input

1
4 9 11 2 1 5 7 1

Sample Output

F

Source

Southwestern European Regional Contest 1995


除了线段完全在矩形内和线段所在直线斜率不存在两种特殊情况外,相交的情况只有以下四种及其组合:
线段与x=xl、x=xr、y=yb、y=yt分别相交,讨论即可。


/*The rectangle consists of four straight lines and the area in between.注意矩形是实心的*/
#include<iostream>

using namespace std;

int xs, ys, xe, ye, xl, yt, xr, yb;

void swap(int &a, int &b)
{
    if (a > b)
    {
        int t = a;
        a = b;
        b = t;
    }
}

bool f1(int x)
{
    return x >= xl&&x <= xr;
}

bool f2(int y)
{
    return y >= yb&&y <= yt;
}

bool judge(int x)
{
    return (x >= xs&&x <= xe) || (x >= xe&&x <= xs);
}

bool judge1(int y)
{
    return (y >= ys&&y <= ye) || (y >= ye&&y <= ys);
}

bool judge_x(double x)
{
    return x >= (double)xl&&x <= (double)xr;
}

bool judge_y(double y)
{
    return y >= (double)yb&&y <= (double)yt;
}

int main()
{
    int n;
    cin >> n;
    while(n--)
    {
        cin >> xs >> ys >> xe >> ye >> xl >> yt >> xr >> yb;
        swap(xl, xr); //The terms top left and bottom right do not imply any ordering of coordinates.注意矩形端点大小,并非按照左上、右下的顺序给出
        swap(yb, yt);
        int b1 = ys, b2 = ye;
        swap(b1, b2);
        if (f1(xs) && f1(xe) && f2(ys) && f2(ye))  //线段在矩形内
        {
            cout << 'T' << endl;
            continue;
        }
        if (xs == xe)    //斜率不存在
        {
            if (f1(xs))
                if (f2(ys) || f2(ye) || (b1 <= yb&&b2 >= yt))
                {
                    cout << 'T' << endl;
                    continue;
                }
        }
        double slope = 1.0*(ys - ye) / (xs - xe);
        double b = ys - slope*xs;
        if (judge(xl))
        {
            double y = slope*xl + b;
            if (judge_y(y))
            {
                cout << 'T' << endl;
                continue;
            }
        }
        if (judge(xr))
        {
            double y = slope*xr + b;
            if (judge_y(y))
            {
                cout << 'T' << endl;
                continue;
            }
        }
        if (judge1(yb))
        {
            double x = (yb - b) / slope;
            if (judge_x(x))
            {
                cout << 'T' << endl;
                continue;
            }
        }
        if (judge1(yt))
        {
            double x = (yt - b) / slope;
            if (judge_x(x))
            {
                cout << 'T' << endl;
                continue;
            }   
        }
        cout << 'F' << endl;
    }

    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值