POJ1410_Intersection(几何/线段是否相交/模板)

17 篇文章 0 订阅
8 篇文章 0 订阅

Intersection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11142 Accepted: 2924

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



解题报告

判断线段是否与矩形相交,线段如果在矩形里面也属于相交。

要注意的是给的矩形两点位置。。。

判断线段与线段相交的模板

double cross(point a,point b,point c)
{
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int isIntersected(point s1,point e1,point s2,point e2)
{
    return (max(s1.x,e1.x)>=min(s2.x,e2.x))&&
    (max(s2.x,e2.x)>=min(s1.x,e1.x))&&
    (max(s1.y,e1.y)>=min(s2.y,e2.y))&&
    (max(s2.y,e2.y)>=min(s1.y,e1.y))&&
    (cross(s1,s2,e1)*cross(s1,e1,e2)>=0)&&
    (cross(s2,s1,e2)*cross(s2,e2,e1)>=0);
}

AC代码

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;
struct point
{
    double x,y;
}st,en,le1,le2,r1,r2;
double cross(point a,point b,point c)
{
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int isIntersected(point s1,point e1,point s2,point e2)
{
    return (max(s1.x,e1.x)>=min(s2.x,e2.x))&&
    (max(s2.x,e2.x)>=min(s1.x,e1.x))&&
    (max(s1.y,e1.y)>=min(s2.y,e2.y))&&
    (max(s2.y,e2.y)>=min(s1.y,e1.y))&&
    (cross(s1,s2,e1)*cross(s1,e1,e2)>=0)&&
    (cross(s2,s1,e2)*cross(s2,e2,e1)>=0);
}
int main()
{
    int n;
    //freopen("1.txt","r",stdin);
    cin>>n;
    while(n--)
    {
        cin>>st.x>>st.y>>en.x>>en.y>>le1.x>>le1.y>>r2.x>>r2.y;
        if(le1.x>r2.x)
            swap(le1.x,r2.x);
        if(le1.y>r2.y)
            swap(le1.y,r2.y);
        le2.x=le1.x;
        le2.y=r2.y;
        r1.x=r2.x;
        r1.y=le1.y;
        if(!isIntersected(st,en,le1,le2)&&!isIntersected(st,en,r1,r2)&&!isIntersected(st,en,le1,r1)&&!isIntersected(st,en,r2,le2))
        {
            if(st.x>=le1.x&&st.x<=r1.x&&en.x>=le1.x&&en.x<=r1.x&&st.y>=le1.y&&st.y<=le2.y&&en.y>=le1.y&&en.y<=le2.y)
              cout<<"T"<<endl;
            else cout<<"F"<<endl;
        }
        else cout<<"T"<<endl;
    }
    return 0;
}
测试数据

68
4 9 11 2 1 1 7 5
11 2 4 9 1 1 7 5
12 12 24 24 19 5 25 17
4 6 15 9 1 1 11 11
19 5 25 17 12 12 24 24
0 18 8 12 1 1 11 11
2 4 4 2 1 1 11 11
-4 9 -11 2 -1 1 -7 5
-11 2 -4 9 -1 1 -7 5
-12 12 -24 24 -19 5 -25 17
-4 6 -15 9 -1 1 -11 11
-19 5 -25 17 -12 12 -24 24
0 18 -8 12 -1 1 -11 11
-2 4 -4 2 -1 1 -11 11
4 -9 11 -2 1 -1 7 -5
11 -2 4 -9 1 -1 7 -5
12 -12 24 -24 19 -5 25 -17
4 -6 15 -9 1 -1 11 -11
19 -5 25 -17 12 -12 24 -24
0 -18 8 -12 1 -1 11 -11
2 -4 4 -2 1 -1 11 -11
-4 -9 -11 -2 -1 -1 -7 -5
-11 -2 -4 -9 -1 -1 -7 -5
-12 -12 -24 -24 -19 -5 -25 -17
-4 -6 -15 -9 -1 -1 -11 -11
-19 -5 -25 -17 -12 -12 -24 -24
0 -18 -8 -12 -1 -1 -11 -11
-2 -4 -4 -2 -1 -1 -11 -11
9 1 9 2 4 3 9 6
9 2 9 1 4 3 9 6
10 3 13 3 4 3 9 6
13 3 10 3 4 3 9 6
10 6 14 6 4 3 9 6
14 6 10 6 4 3 9 6
9 7 9 10 4 3 9 6
9 10 9 7 4 3 9 6
4 7 4 10 4 3 9 6
4 10 4 7 4 3 9 6
0 6 3 6 4 3 9 6
3 6 0 6 4 3 9 6
1 3 3 3 4 3 9 6
3 3 1 3 4 3 9 6
4 0 4 2 4 3 9 6
4 2 4 0 4 3 9 6
5 3 8 5 4 3 9 6
8 5 5 3 4 3 9 6
5 3 8 3 4 3 9 6
8 3 5 3 4 3 9 6
6 4 6 5 4 3 9 6
6 5 6 4 4 3 9 6
4 3 9 6 4 3 9 6
9 6 4 3 4 3 9 6
4 3 5 4 4 3 9 6
5 4 4 3 4 3 9 6
5 3 8 3 4 3 9 6
8 3 5 3 4 3 9 6
5 3 9 3 4 3 9 6
9 3 5 3 4 3 9 6
4 4 4 5 4 3 9 6
4 5 4 4 4 3 9 6
4 3 4 5 4 3 9 6
4 5 4 3 4 3 9 6
4 3 4 6 4 3 9 6
4 6 4 3 4 3 9 6
9 2 9 5 4 3 9 6
9 5 9 2 4 3 9 6
9 2 9 7 4 3 9 6
9 7 9 2 4 3 9 6

答案

F
F
F
T
T
F
T
F
F
F
T
T
F
T
F
F
F
T
T
F
T
F
F
F
T
T
F
T
F
F
F
F
F
F
F
F
F
F
F
F
F
F
F
F
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T
T



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值