POJ 1410 Intersection (线段和矩形相交)

题目:

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

题意:给出一条线段和一个矩形 判断两者是否相交思路:就直接暴力判断 但是要考虑一些边界情况 曾经在判断线段是否在矩形内的时候莫名其妙wa 

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const double eps=1e-10;
int n;
double x,y,xx,yy,tx,ty,txx,tyy;

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

struct Point{
    double x,y;
    Point(){}
    Point(double _x,double _y){
        x=_x,y=_y;
    }
    Point operator + (const Point &b) const{
        return Point(x+b.x,y+b.y);
    }
    Point operator - (const Point &b) const{
        return Point(x-b.x,y-b.y);
    }
    double operator * (const Point &b) const{
        return x*b.x+y*b.y;
    }
    double operator ^ (const Point &b) const{
        return x*b.y-y*b.x;
    }
};

struct Line{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e){
        s=_s,e=_e;
    }
};

bool inter(Line l1,Line l2){
    return
        max(l1.s.x,l1.e.x)>=min(l2.s.x,l2.e.x) &&
        max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x) &&
        max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y) &&
        max(l2.s.y,l2.e.y)>=min(l1.s.y,l1.e.y) &&
        dcmp((l2.s-l1.e)^(l1.s-l1.e))*dcmp((l2.e-l1.e)^(l1.s-l1.e))<=0 &&
        dcmp((l1.s-l2.e)^(l2.s-l2.e))*dcmp((l1.e-l2.e)^(l2.s-l2.e))<=0;
}

int main(){
    scanf("%d",&n);
    while(n--){
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x,&y,&xx,&yy,&tx,&ty,&txx,&tyy);
        double xl=min(tx,txx);
        double xr=max(tx,txx);
        double ydown=min(ty,tyy);
        double yup=max(ty,tyy);
        Line line=Line(Point(x,y),Point(xx,yy));
        Line line1=Line(Point(tx,ty),Point(tx,tyy));
        Line line2=Line(Point(tx,ty),Point(txx,ty));
        Line line3=Line(Point(txx,ty),Point(txx,tyy));
        Line line4=Line(Point(txx,tyy),Point(tx,tyy));
        if(inter(line,line1) || inter(line,line2) || inter(line,line3) || inter(line,line4) || (max(x,xx)<xr && max(y,yy)<yup && min(x,xx)>xl && min(y,yy)>ydown)) printf("T\n");
        else printf("F\n");
    }
    return 0;
}

 

原文地址:https://www.cnblogs.com/whdsunny/p/9870694.html

时间: 2024-10-13 22:31:05

POJ 1410 Intersection (线段和矩形相交)的相关文章

POJ 1410 判断线段与矩形交点或在矩形内

这个题目要注意的是:给出的矩形坐标不一定是按照左上,右下这个顺序的 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define eps 1e-8 #define INF 1e9 using namespace std; const int maxn=100; typedef struct Point {

POJ 1410 Intersection(线段相交&amp;&amp;判断点在矩形内&amp;&amp;坑爹)

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段完全在矩形内部算相交:线段与矩形任意一条边不规范相交算相交. 思路:知道具体的相交规则之后题其实是不难的,但是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,需要重新判断一下...真坑. 1 struct Point 2 { 3 double x, y; 4 } A, B, C, D; 5 struct Line 6 { 7 Point a, b; 8 } L; 9 10 int n; 11

POJ 1410 Intersection(线段相交&amp;amp;&amp;amp;推断点在矩形内&amp;amp;&amp;amp;坑爹)

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段全然在矩形内部算相交:线段与矩形随意一条边不规范相交算相交. 思路:知道详细的相交规则之后题事实上是不难的,可是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,须要又一次推断一下...真坑. struct Point { double x, y; } A, B, C, D; struct Line { Point a, b; } L; int n; double xmult(Point p1

线段和矩形相交 POJ 1410

1 // 线段和矩形相交 POJ 1410 2 3 // #include <bits/stdc++.h> 4 #include <iostream> 5 #include <cstdio> 6 #include <cstdlib> 7 #include <algorithm> 8 #include <vector> 9 #include <math.h> 10 using namespace std; 11 #defin

poj 1410 Intersection (判断线段与矩形相交 判线段相交)

题目链接 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12040   Accepted: 3125 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:

POJ 1410 Intersection --几何,线段相交

题意: 给一条线段,和一个矩形,问线段是否与矩形相交或在矩形内. 解法: 判断是否在矩形内,如果不在,判断与四条边是否相交即可.这题让我发现自己的线段相交函数有错误的地方,原来我写的线段相交函数就是单纯做了两次跨立实验,在下图这种情况是错误的: 这样的话线段与右边界的两次跨立实验(叉积<=0)都会通过,但是并不相交. 所以要加快速排斥. 还有就是这题题目说给出的不一定是左上角,右下角依次的顺序.所以干脆重新自己定义左上角,右下角. 代码: #include <iostream> #inc

POJ 1410 Intersection(计算几何)

题目链接:id=1410">Intersection 推断线段与矩形的关系.与矩形相交打印T,否则打印F. 坑题,精度. . .. 思路就是,先推断 线段是否在矩形里面,再推断线段和两条对角线的关系,利用叉积模板就可以 測试数据有个坑,就是 左上角的坐标并不一定比右下角的小. ..这根本不符合题意嘛 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring&

POJ 1410 Intersection

这题我就是用最原始的思考方法,其中有许多细节要注意.主体思想就是四条边分别和线段比较. 线段在矩形内要考虑. 我的代码有点乱有点长,其中有的部分可以写成函数. #include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; int x_s,y_s,x_e,y_e,x_l,y_t,x_r,y_b; bool f1(int x) { retu

简单几何(线段相交) POJ 1410 Intersection

题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /************************************************ * Author :Running_Time * Created Time :2015/10/27 星期二 13:17:49 * File Name :POJ_1410.cpp ******************************************