线段相交:Intersection

本文探讨了如何判断二维平面上两条线段是否相交的问题,详细阐述了线段相交的数学原理,并提供了输入输出的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Intersection Time Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%I64d & %I64u

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

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

using namespace std;

struct Point{
    int x;
    int y;
};

Point beg, end, lu, ru, ll, rl;

double direction(Point p1, Point p2, Point p3){//p3对于p1->p2的转向,为正则顺时针
    return (p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y);
}

bool online(Point p1, Point p2, Point p3){
    return (p3.x >= min(p1.x, p2.x) && p3.x <= max(p1.x, p2.x) && p3.y >= min(p1.y, p2.y) && p3.y <= max(p1.y, p2.y));
}

bool intersect(Point p1, Point p2, Point p3, Point p4){//p1-p2 与 p3-p4的相交情况
    double d1 = direction(p3, p4, p1);
    double d2 = direction(p3, p4, p2);
    double d3 = direction(p1, p2, p3);
    double d4 = direction(p1, p2, p4);
    if (d1 * d2 < 0 && d3 * d4 < 0) return true;
    else if (d1 == 0 && online(p3, p4, p1)) return true;
    else if (d2 == 0 && online(p3, p4, p2)) return true;
    else if (d3 == 0 && online(p1, p2, p3)) return true;
    else if (d4 == 0 && online(p1, p2, p4)) return true;
    return false;
}

int main(){
    int n, t;
    cin >> t;
    int tmp;
    int x1, y1, x2, y2;
    while(t --){
        scanf("%d %d %d %d", &beg.x, &beg.y, &end.x, &end.y);
        scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
        if(x1 > x2){
            tmp = x1;
            x1 = x2;
            x2 = tmp;
        }
        if(y1 < y2){
            tmp = y1;
            y1 = y2;
            y2 = tmp;
        }
        lu.x = x1;
        lu.y = y1;
        ru.x = x2;
        ru.y = y1;
        ll.x = x1;
        ll.y = y2;
        rl.x = x2;
        rl.y = y2;
        bool flag = false;
        if(intersect(lu, ru, beg, end))
            flag = true;
        else if(intersect(ll, rl, beg, end))
            flag = true;
        else if(intersect(lu, ll, beg, end))
            flag = true;
        else if(intersect(ru, rl, beg, end))
            flag = true;
        else if(beg.x >= x1 && beg.x <= x2 && beg.y <= y1 && beg.y >= y2 && end.x >= x1 && end.x <= x2 && end.y <= y1 && end.y >= y2)
            flag = true;
        if(flag)
            printf("T\n");
        else
            printf("F\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值