POJ 1410 Intersection(判断线段交和点在矩形内)

Intersection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14773 Accepted: 3872

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>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define ms(a,b)memset(a,b,sizeof(a))
#define eps 1e-10
#define inf 1e8

int n,m,cntl,cntp;

double add(double a,double b)
{
    if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
    return a+b;
}

struct P
{
    double x,y;
    P(){}
    P(double x,double y): x(x),y(y){}
    P operator + (P p)
    {
        return P(add(x,p.x),add(y,p.y));
    }
    P operator - (P p)
    {
        return P(add(x,-p.x),add(y,-p.y));
    }
    P operator *(double d)
    {
        return P(x*d,y*d);
    }
    double dot (P p)
    {
        return add(x*p.x,y*p.y);
    }
    double det(P p)
    {
        return add(x*p.y,-y*p.x);
    }
}p[100005],q[100005];

bool on_seg(P p1,P p2,P q)
{
    return (p1-q).det(p2-q)==0&&(p1-q).dot(p2-q)<=0;
}

P intersection(P p1,P p2,P q1,P q2)
{
    return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
}

bool xiangjiao(P p1,P p2,P p3,P p4)
{
    if((p1-p2).det(p3-p4)==0)
    {
        if(on_seg(p1,p2,p3)||on_seg(p1,p2,p4)||on_seg(p3,p4,p1)||on_seg(p3,p4,p2))
        {
            return true;
        }
        return false;
    }
    P r=intersection(p1,p2,p3,p4);
    if(on_seg(p1,p2,r)&&on_seg(p3,p4,r))
        return true;
    return false;
}


int main()
{
    cin>>n;
    while(n--)
    {
        double xs,xe,ys,ye,xl,yt,xr,yb;
        double x1,x2,y1,y2;
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&xs,&ys,&xe,&ye,&x1,&y1,&x2,&y2);
        xl=min(x1,x2);
        xr=max(x1,x2);
        yb=min(y1,y2);
        yt=max(y1,y2);
        if(xiangjiao(P(xs,ys),P(xe,ye),P(xl,yt),P(xr,yt))||xiangjiao(P(xs,ys),P(xe,ye),P(xl,yb),P(xr,yb))||xiangjiao(P(xs,ys),P(xe,ye),P(xl,yb),P(xl,yt))||xiangjiao(P(xs,ys),P(xe,ye),P(xr,yb),P(xr,yt)))
        {
            cout<<"T"<<endl;
        }
        else if(max(xs, xe) < xr && max(ys, ye) < yt && min(xs, xe) > xl && min(ys, ye) > yb)
        {
            cout<<"T"<<endl;
        }
        else cout<<"F"<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值