1410 Intersection 判断线段和矩形是否相交 转换为判断和矩形四条边是否相交以及线段是否在矩形内部 包含端点

Intersection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5184 Accepted: 1284

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>
using namespace std;
#define Min(x,y) (x<y?x:y)
#define Max(x,y) (x<y?y:x)
struct Node
{
    int x,y;
};
int CrossMutiply(Node& p1,Node& p2,Node& p3)//叉积
{
    return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
int PointMutiply(Node& p1,Node& p2,Node& p3)//点积
{
    return (p2.x-p1.x)*(p3.x-p1.x)+(p2.y-p1.y)*(p3.y-p1.y);
}
bool Segments_Intersect(Node& p1_start,Node& p1_end,Node& p2_start,Node& p2_end)//判断两线段是否相交
{
    int d1=CrossMutiply(p1_start,p1_end,p2_start);
    int d2=CrossMutiply(p1_start,p1_end,p2_end);
    int d3=CrossMutiply(p2_start,p2_end,p1_start);
    int d4=CrossMutiply(p2_start,p2_end,p1_end);
    if((d1*d2<0)&&(d3*d4<0)) return true;
    else if(d1==0&&PointMutiply(p2_start,p1_start,p1_end)<=0) return true;
    else if(d2==0&&PointMutiply(p2_end,p1_start,p1_end)<=0) return true;
    else if(d3==0&&PointMutiply(p1_start,p2_start,p2_end)<=0) return true;
    else if(d4==0&&PointMutiply(p1_end,p2_start,p2_end)<=0) return true;
    else return false;
}
int main()
{
    int n;
    Node rectangle[5],pStart,pEnd;
    bool flag;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d%d%d%d%d%d%d%d",&pStart.x,&pStart.y,&pEnd.x,&pEnd.y,&rectangle[0].x,&rectangle[0].y,&rectangle[2].x,&rectangle[2].y);
        int min_x=Min(rectangle[0].x,rectangle[2].x);
        int min_y=Min(rectangle[0].y,rectangle[2].y);
        int max_x=Max(rectangle[0].x,rectangle[2].x);
        int max_y=Max(rectangle[0].y,rectangle[2].y);
        if(pStart.x>=min_x&&pStart.y>=min_y&&pStart.x<=max_x&&pStart.y<=max_y)
        {
            if(pEnd.x>=min_x&&pEnd.y>=min_y&&pEnd.x<=max_x&&pEnd.y<=max_y)
            {
                printf("T/n");
                continue;
            }
        }
        rectangle[4]=rectangle[0];
        rectangle[1].y=rectangle[0].y;
        rectangle[1].x=rectangle[2].x;
        rectangle[3].y=rectangle[2].y;
        rectangle[3].x=rectangle[0].x;
        flag=false;
        for(int i=0;i<4;i++)
        {
            if(Segments_Intersect(pStart,pEnd,rectangle[i],rectangle[i+1]))
            {
                flag=true;
                break;
            }
        }
        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、付费专栏及课程。

余额充值