POJ 1410 Intersection

Intersection

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 15451 Accepted: 4037

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


题意:
判直线与矩形交。

思路:
大家一起学英语好吧。

The rectangle consists of four straight lines and the area in between.
The terms top left and bottom right do not imply any ordering of coordinates.

代码:

#include<cstdio>
#include<cmath>
#include<iostream>
#define exp 1e-10
using namespace std;

struct point
{
    double x,y;
};

struct segment
{
    point p1,p2;
}s,ss[5];

double cx(point a,point b)
{
    return a.x*b.y-b.x*a.y;
}

point xlj(point a,point b)
{
    point res;
    res.x=a.x-b.x;
    res.y=a.y-b.y;
    return res;
}

int judge(segment a,segment b)
{
    point xx,yy,zz;
    xx=xlj(a.p1,b.p1);
    yy=xlj(b.p2,b.p1);
    zz=xlj(a.p2,b.p1);
    if(cx(xx,yy)*cx(yy,zz)>=0)
    {
        xx=xlj(b.p1,a.p1);
        yy=xlj(a.p2,a.p1);
        zz=xlj(b.p2,a.p1);
        if(cx(xx,yy)*cx(yy,zz)>=exp) return 1;
        return 0;
    }
    return 0;
}

int n;

int main()
{
    int ccc=0;
    while(~scanf("%d",&n))
    {
        while(n--)
        {
            double a,b,c,d;
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            s.p1.x=a; s.p1.y=b;
            s.p2.x=c; s.p2.y=d;

            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            point p_1,p_2,p_3,p_4;
            p_1.x=a; p_1.y=b;
            p_2.x=a; p_2.y=d;
            p_3.x=c; p_3.y=d;
            p_4.x=c; p_4.y=b;
            ss[0].p1=p_1; ss[0].p2=p_2;
            ss[1].p1=p_2; ss[1].p2=p_3;
            ss[2].p1=p_3; ss[2].p2=p_4;
            ss[3].p1=p_4; ss[3].p2=p_1;

            int i;
            bool flag=0;

            if((s.p1.x>=min(a,c))&&(s.p1.x<=max(a,c))&&(s.p1.y<=max(b,d))&&(s.p1.y>=min(b,d))) flag=1;
            if((s.p2.x>=min(a,c))&&(s.p2.x<=max(a,c))&&(s.p2.y<=max(b,d))&&(s.p2.y>=min(b,d))) flag=1;

            for(i=0;i<4;i++)
            {
                if(judge(ss[i],s))
                    flag=1;
            }
            if(flag) printf("T\n");
            else printf("F\n");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值