POJ1410 Intersection

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9733 Accepted: 2566

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
考虑:线段在矩形内也算相交,开始忘考虑一直情况,两个线段相交,也可能其中一条线段完全覆盖另一个线段,所以判断的时候要相互的判断点时候在线段内。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
const double eps = 1e-8;

struct point {
    int x;
    int y;
};
struct line {
    point start;
    point endx;
};

int dcmp(double x) {
    if(fabs(x)<eps) return 0;
    else return x<0 ? -1 : 1;
}

double det(const point p1, const point p2, const point p0) {
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

bool PointOnSegment(const line a, point p) {
    if((dcmp(det(a.start, a.endx, p))==0) &&
       ((a.start.x-p.x)*(a.endx.x-p.x)+(a.start.y-p.y)*(a.endx.y-p.y))<=0)
     return true;
     return false;
}

bool Point_in_rectangle(int x1, int x2, int y1, int y2, line a) {
    if((a.start.x>=x1 && a.start.x <=x2 && a.start.y>=y1 && a.start.y<=y2)&&
        a.endx.x>=x1 && a.endx.x<=x2 && a.endx.y>=y1 && a.endx.y<=y2)
    return true;
    return false;
}

bool ok(const line a, const line b) {
    int flag1 = 0, flag2 = 0;
    if(max(a.start.x, a.endx.x)>=min(b.start.x, b.endx.x) &&
       max(b.start.x, b.endx.x)>=min(a.start.x, a.endx.x) &&
       max(a.start.y, a.endx.y)>=min(b.start.y, b.endx.y) &&
       max(b.start.y, b.endx.y)>=min(a.start.y, a.endx.y) &&
       det(b.start, a.endx, a.start)*det(a.endx, b.endx,a.start)> 0 &&
       det(a.start, b.endx, b.start)*det(b.endx, a.endx, b.start)> 0
       ) flag1 = 1;
    if(PointOnSegment(a, b.endx)||PointOnSegment(a, b.start)
       ||PointOnSegment(b, a.endx) || PointOnSegment(b, a.start)
       ) flag2 = 1;
    if(flag1 || flag2) return true;
    return false;
}

int main()
{
    //freopen("d:\\in.txt","r",stdin);
    //freopen("d:\\out.txt","w",stdout);
    int n;
    int xstart, ystart, xend, yend;
    int xleft, ytop, xright, ybottom;
    line a, b, c, d;
    line t;
    bool flag = false;
    scanf("%d", &n);
    int tmp1, tmp2, tmp3, tmp4;
    while(n--) {
        flag = false;
        scanf("%d%d%d%d", &xstart, &ystart, &xend, ¥d);
        scanf("%d%d%d%d", &xleft, &ytop, &xright, &ybottom);
        t.start.x = xstart; t.start.y = ystart;
        t.endx.x = xend; t.endx.y = yend;

        tmp1 = min(xleft, xright);
        tmp2 = max(xleft, xright);
        tmp3 = min(ytop, ybottom);
        tmp4 = max(ytop, ybottom);

        a.start.x = a.endx.x = c.start.x = b.endx.x = tmp1; //xleft;
        a.start.y = b.start.y = b.endx.y = d.endx.y = tmp4;//ytop;
        a.endx.y = d.start.y = c.start.y = c.endx.y = tmp3;//ybottom;
        b.start.x = c.endx.x = d.start.x = d.endx.x = tmp2;//xright;
        if(Point_in_rectangle(tmp1, tmp2, tmp3, tmp4, t)) flag = true;
        if(ok(a,t)) flag = true;
        if(ok(b,t)) flag = true;
        if(ok(c,t)) flag = true;
        if(ok(d,t)) flag = true;
        if(flag){
            printf("T\n");
        } else {
            printf("F\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值