POJ-1410

Intersection

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12817   Accepted: 3343

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

题意:给一条线段,然后是一个矩形,问线段是否与矩形相交

    kuangbin模版

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
#define eps 1e-8
#define maxn 100
using namespace std;
int sgn(double x)
{
    if(abs(x) < eps) return 0;
    if(x<0) return -1;
    else return 1;
}
struct Point
{
    double x;
    double y;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x;
        y = _y;
    }
    Point operator -(const Point &a) const
    {
        return Point(x-a.x,y-a.y);
    }
    Point operator + (const Point &a) const
    {
        return Point(x+a.x,y+a.y);
    }
    double  operator *(const Point &a) const
    {
        return x*a.x+y*a.y;
    }
    double  operator ^(const Point &a) const
    {
        return x*a.y-y*a.x;
    }
};
struct Line
{
    Point s;
    Point 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) &&
    sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0&&
    sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <=0;
}
///判断直线和线段是否相交
bool seg_inter_line(Line l1,Line l2)
{
    return sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s,l1.e)) <=0;
}
bool Onseg(Point p,Line L)
{
    return
    sgn((L.s-p)^(L.e-p)) == 0 &&
    sgn((p.x-L.s.x)*(p.x-L.e.x)) <= 0 &&
    sgn((p.y-L.s.y)*(p.y-L.e.y)) <= 0;
}
int inConvexpoly(Point a,Point p[],int n)
{
    for(int i=0;i<n;i++)
    {
        if(sgn((p[i]-a)^(p[(i+1)%n]-a)) < 0) return -1;
        else if(Onseg(a,Line(p[i],p[(i+1)%n]))) return 0;
    }
    return 1;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        Point s;
        Point e;
        double  x1,y1,x2,y2;
        scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&s.x,&s.y,&e.x,&e.y,&x1,&y1,&x2,&y2);
        if(x1 > x2) swap(x1,x2);
        if(y1 > y2) swap(y1,y2);
        Point p[10];
        Line L = Line(s,e);
        p[0] = Point(x1,y1);
        p[1] = Point(x2,y1);
        p[2] = Point(x2,y2);
        p[3] = Point(x1,y2);
        if(inter(L,Line(p[0],p[1])))
        {
            printf("T\n");
            continue;
        }
        else if(inter(L,Line(p[1],p[2])))
        {
            printf("T\n");
            continue;
        }
        else if(inter(L,Line(p[2],p[3])))
        {
            printf("T\n");
            continue;
        }
        else if(inter(L,Line(p[3],p[0])))
        {
            printf("T\n");
            continue;
        }
        else if(inConvexpoly(L.s,p,4)>=0 || inConvexpoly(L.e,p,4)>=0)
        {
             printf("T\n");
            continue;
        }
        else
            printf("F\n");
    }
    return 0;
}
时间: 2024-12-16 12:04:20

POJ-1410的相关文章

poj 1410 线段相交判断

http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11329   Accepted: 2978 Description You are to write a program that has to decide whether a given line segment intersects a given rectangle. An ex

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 计算几何

1 /** 2 注意: 千万得小心..就因为一个分号,调了一个晚上... 3 **/ 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 struct point { 8 int x,y; 9 }; 10 11 struct line{ 12 point a; 13 point b; 14 }; 15 line l,lt;//l 直线 lt 矩形的边 16 int x1,x2,y1,y2; 17

线段和矩形相交 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(线段相交&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 Intersection(计算几何)

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

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

这题我就是用最原始的思考方法,其中有许多细节要注意.主体思想就是四条边分别和线段比较. 线段在矩形内要考虑. 我的代码有点乱有点长,其中有的部分可以写成函数. #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 (判断线段与矩形相交 判线段相交)

题目链接 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 数据错误

题目要求判断一条线段和一个矩形是否相交,或者是否在矩形里面(题目好像没说?) 思路就是直接暴力判断和矩形四条边是否相交,和线段的坐标是否在矩形的坐标范围即可. 然后题目的数据,(xleft,ytop) 和 (xright,ybottom)不是按顺序给出的,需要自己判断下顺序. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algori