poj1410 Intersection

Intersection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10675 Accepted: 2807

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



判断矩形和线段是否相交,没什么说的,只是写的好别扭……

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

#define eps 1e-6

struct Point
{
    double x,y;
    Point(double xx,double yy)
    {
        x=xx;y=yy;
    }
    Point(){}
};

struct Obj
{
    Point begin,end;
    Obj(Point begin1,Point end1)
    {
        begin=begin1;
        end=end1;
    }
    Obj(){}
};

bool Check(Point p,Obj rec)
{
    if (p.x<=max(rec.begin.x,rec.end.x) && p.x>=min(rec.begin.x,rec.end.x) && p.y<=max(rec.begin.y,rec.end.y) && p.y>=min(rec.begin.y,rec.end.y)) return true;
    return false;
}

double mul(Obj line1,Obj line2)
{
    return (line1.end.x-line1.begin.x)*(line2.end.y-line2.begin.y)-(line2.end.x-line2.begin.x)*(line1.end.y-line2.begin.y);
}

bool cross(Obj line1,Obj line2)
{
    if (min(line1.begin.x,line1.end.x)>max(line2.begin.x,line2.end.x)) return false;
    if (min(line2.begin.x,line2.end.x)>max(line1.begin.x,line1.end.x)) return false;
    if (min(line1.begin.y,line1.end.y)>max(line2.begin.y,line2.end.y)) return false;
    if (min(line2.begin.y,line2.end.y)>max(line1.begin.y,line1.end.y)) return false;
    Obj tmp1(line1.begin,line2.begin),tmp2(line1.begin,line2.end);
  //  printf("%f~~\n",mul(line1,tmp1)*mul(line1,tmp2));
    if (mul(line1,tmp1)*mul(line1,tmp2)>eps) return false;
    Obj tmp3(line2.begin,line1.begin),tmp4(line2.begin,line1.end);
    if (mul(line2,tmp3)*mul(line2,tmp4)>eps) return false;
    return true;
}

bool Inter(Obj line,Obj rec)
{
    Point p1(rec.begin.x,rec.begin.y),p2(rec.begin.x,rec.end.y),p3(rec.end.x,rec.end.y),p4(rec.end.x,rec.begin.y);
    Obj line1(p1,p2),line2(p2,p3),line3(p3,p4),line4(p4,p1);
    if (cross(line,line1) || cross(line,line2) || cross(line,line3) || cross(line,line4)) return true;
    return false;
}

int main()
{
    int i,j,n,T;
    Obj rec,line;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&line.begin.x,&line.begin.y,&line.end.x,&line.end.y,&rec.begin.x,&rec.begin.y,&rec.end.x,&rec.end.y);
        bool x=Check(line.begin,rec);
        bool y=Check(line.end,rec);
      //  printf("%d %d\n",x,y);
        if (x || y)
        {
            printf("T\n");
            continue;
        }
        if (Inter(line,rec))
        {
            printf("T\n");
        }
        else
        {
            printf("F\n");
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值