poj 1410 Intersection(线段与矩形相交)

本文讨论了如何通过编程解决给定线段与矩形相交的问题,包括输入格式解析、相交条件判断及输出结果呈现。

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

Intersection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8611 Accepted: 2254

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

题意:判断给定的线段与矩形是否相交,线段在矩形内部也算相交
分析:这题还算比较裸的,不过貌似数据中矩形给的并不是左上角,右下角,还有一开始以为是直线与矩形相交,晕,wa了啊
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef double mType;

/**表示点或向量*/
struct Tpoint
{
    mType x,y;
    Tpoint(){}
    Tpoint(mType _x,mType _y):x(_x),y(_y){}
};


/**有起点和终点的向量或线段*/
struct Tsegment
{
    Tpoint start,end;
    Tsegment(){}
    Tsegment(Tpoint _start,Tpoint _end):start(_start),end(_end){}
    Tsegment(mType sx,mType sy,mType tx,mType ty):start(sx,sy),end(tx,ty){}
};

struct Trectangle
{
    mType l,r,b,t;
    Trectangle(){}
    Trectangle(mType _l,mType _r,mType _b,mType _t):l(_l),r(_r),b(_b),t(_t){}
};
/**生成一个点P到点Q的向量*/
Tpoint MakeVector(Tpoint P,Tpoint Q)
{
    return Tpoint(Q.x-P.x,Q.y-P.y);
}


/**向量P与Q的叉积PQ*/
mType CrossProduct(Tpoint P,Tpoint Q)
{
    return P.x*Q.y-P.y*Q.x;
}


/**向量QP与向量QR的叉积,用来判断向量的拐向
*  返回值: >0 向右拐, <0 向右拐,等于零同向或反向
*/
mType MultiCross(Tpoint P,Tpoint Q,Tpoint R)
{
    return CrossProduct(MakeVector(Q,P),MakeVector(Q,R));
}


/**判断线段P和线段Q是否相交*/
bool IsIntersect(Tsegment P,Tsegment Q)
{
    if(max(P.start.x,P.end.x)<min(Q.start.x,Q.end.x)||max(Q.start.x,Q.end.x)<min(P.start.x,P.end.x)||
       max(P.start.y,P.end.y)<min(Q.start.y,Q.end.y)||max(Q.start.y,Q.end.y)<min(P.start.y,P.end.y))return 0;
    return (MultiCross(P.end,P.start,Q.start)*MultiCross(P.end,P.start,Q.end)<=0&&
            MultiCross(Q.end,Q.start,P.start)*MultiCross(Q.end,Q.start,P.end)<=0);
}

mType sx,sy,ex,ey,l,r,t,b;
Tsegment P;
Trectangle Q;
int n;
bool IsSegRectInter(Tsegment P,Trectangle Q)
{
    if(Q.l<=P.start.x&&P.start.x<=Q.r&&Q.b<=P.start.y&&P.start.y<=Q.t)return 1;
    if(Q.l<=P.end.x&&P.end.x<=Q.r&&Q.b<=P.end.y&&P.end.y<=Q.t)return 1;
    if(IsIntersect(P,Tsegment(Q.l,Q.t,Q.r,Q.b)))return 1;
    if(IsIntersect(P,Tsegment(Q.l,Q.b,Q.r,Q.t)))return 1;
    return 0;
}
int main()
{
    while(~scanf("%d",&n))
        while(n--)
        {
            scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&sx,&sy,&ex,&ey,&l,&t,&r,&b);
            if(t<b)swap(t,b);
            if(r<l)swap(r,l);
            P=Tsegment(sx,sy,ex,ey);
            Q=Trectangle(l,r,b,t);
            puts(IsSegRectInter(P,Q)?"T":"F");
        }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值