Intersection

Intersection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11593 Accepted: 3017

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


做这个题都快哭了,明明一个简单的题花了我一下午外加一晚上,最后错的原因是矩形时左上角坐标和右下角坐标并不确定。另外线段在矩形内部是也输出T,因为矩形是实心的,可以想到的错我全犯了,人才啊,下午的比赛啥也没干全弄它了。

其实看矩形的四条边和已知线段是否相交,和线段是否在矩形内部,就可以确定答案的输出。所以代码如下:

#include<stdio.h>
#include<string.h>
#include<string>
#include <math.h>
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
struct point
{
    double x,y;
};


double xmult(point p1,point p2,point p0)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}


double dmult(point p1,point p2,point p0)
{
    return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);
}


int dots_inline(point p1,point p2,point p3)
{
    return zero(xmult(p1,p2,p3));
}


int dot_online_in(point p,point l1,point l2)
{
    return zero(xmult(p,l1,l2))&&(l1.x-p.x)*(l2.x-p.x)<eps&&(l1.y-p.y)*(l2.y-p.y)<eps;
}


int same_side(point p1,point p2,point l1,point l2)
{
    return xmult(l1,p1,l2)*xmult(l1,p2,l2)>eps;
}


int intersect_in(point u1,point u2,point v1,point v2)
{
    if (!dots_inline(u1,u2,v1)||!dots_inline(u1,u2,v2))
        return !same_side(u1,u2,v1,v2)&&!same_side(v1,v2,u1,u2);
    return dot_online_in(u1,v1,v2)||dot_online_in(u2,v1,v2)||dot_online_in(v1,u1,u2)||dot_online_in(v2,u1,u2);
}
int main()
{
    point x,y;
    point q[10],p,pp;
    int T,i,j,k;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&p.x,&p.y,&pp.x,&pp.y,&q[4].x,&q[4].y,&q[2].x,&q[2].y);
        q[1].x=q[4].x;
        q[1].y=q[2].y;
        q[3].x=q[2].x;
        q[3].y=q[4].y;
        if(((p.x>=q[2].x&&p.x<=q[4].x)||(p.x>=q[4].x&&p.x<=q[2].x)) && ((p.y>=q[2].y&&p.y<=q[4].y)||(p.y>=q[4].y&&p.y<=q[2].y)) )
        {
                printf("T\n");
                continue;


        }
        if(((pp.x>=q[2].x&&pp.x<=q[4].x)||(pp.x>=q[4].x&&pp.x<=q[2].x)) && ((pp.y>=q[2].y&&pp.y<=q[4].y)||(pp.y>=q[4].y&&pp.y<=q[2].y)))
        {
                printf("T\n");
                continue;
        }
        if(intersect_in(p,pp,q[1],q[2])||intersect_in(p,pp,q[3],q[2])||intersect_in(p,pp,q[3],q[4])||intersect_in(p,pp,q[1],q[4]))
        {
            printf("T\n");
        }
        else
        {
            printf("F\n");
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值