POJ-1410——Intersection(简单计算几何,坑题)

该博客讨论的是如何判断一条线段是否与一个矩形相交的问题,其中矩形是实心的,线段在矩形内也视为相交。题目来自Southwestern European Regional Contest 1995。博客提到了判断过程中的注意事项,包括处理矩形点的输入顺序,并提供了相应的解决方案。

Intersection

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 17761 Accepted: 4556

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

Southwestern European Regional Contest 1995

题目大意一眼就懂:就是判断线段与矩形的关系,但是坑点有点多:

一:矩形是实心的,也就是说线段在矩形内也算相交

二:矩形的点不是按照左上右下输入的,还要再判断一下

然后就是判断一下就OK了

代码:

/*
给出线段的四个点坐标,与一个矩形的左上和右下点坐标,判断线段和矩形是否相交
思路:把矩形的每个边与线段比较,判断是否相交(线段在矩形内也算相交)
*/
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define ul unsigned long long
#define bug printf("mmp\n");
#define inf 0x3f3f3f3f
#define esp 1e-6
#define mm(a,b) memset(a,b,sizeof(a))
#define T()int test,q=1,tt;scanf("%d",&test),tt=test;while(test--)
const int N=1e3+10;
const int maxn=1e6+100;
struct point
{
    double x,y;
};
struct line
{
    point st,ed;///开始和结束
};
double mul(point a,point b,point c)
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
bool in(line l1,line l2)
{
    if(max(l1.st.x,l1.ed.x)<min(l2.st.x,l2.ed.x)||max(l2.st.x,l2.ed.x)<min(l1.st.x,l1.ed.x))
        return false;
    if(max(l1.st.y,l1.ed.y)<min(l2.st.y,l2.ed.y)||max(l2.st.y,l2.ed.y)<min(l1.st.y,l1.ed.y))
        return false;
    if(mul(l1.st,l2.ed,l2.st)*mul(l2.ed,l1.ed,l2.st)<0)
        return false;
    if(mul(l2.st,l1.ed,l1.st)*mul(l1.ed,l2.ed,l1.st)<0)
        return false;
    return true;
}
bool judege(point a,point b,point c,point d,line l1)
{
    line l2;
    l2.st=a;
    l2.ed=b;
    if(in(l1,l2))
        return true;
    l2.st=b;
    l2.ed=c;
    if(in(l1,l2))
        return true;
    l2.st=c;
    l2.ed=d;
    if(in(l1,l2))
        return true;
    l2.st=d;
    l2.ed=a;
    if(in(l1,l2))
        return true;
    return false;
}
int main()
{
    T()
    {
        point a,b,c,d;///代表a,b,c,d四点
        line l;
        cin>>l.st.x>>l.st.y>>l.ed.x>>l.ed.y;
        cin>>a.x>>a.y>>c.x>>c.y;
        if(a.x>c.x)swap(a.x,c.x);
        if(a.y<c.y)swap(a.y,c.y);
        int flag=0;
        b.x=a.x,b.y=c.y;
        d.x=c.x,d.y=a.y;
        if(judege(a,b,c,d,l))
            flag=1;
        ///判断是否在矩形内
        if((a.x<=l.st.x&&l.st.x<=c.x&&c.y<=l.st.y&&l.st.y<=a.y)||(a.x<=l.ed.x&&l.ed.x<=c.x&&c.y<=l.ed.y&&l.ed.y<=a.y))
            flag=1;
        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、付费专栏及课程。

余额充值