poj1410计算几何线段相交

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到不省人事 ..题意没有理解,(为啥总是不能把题意说清楚点呢!!!!)线段在矩形里也算T(这一点害我wa了9次)
google翻译是线段和矩形至少有一个公共点,md理解成线段和矩形的边至少一个公共点了
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007

using namespace std;

const double eps=1e-8;
const int N=1005,maxn=100005,inf=0x3f3f3f3f;

struct point{
    int x,y;
};
struct line{
   point a,b;
}l[N];

int mul(point p,point u,point v)
{
    return (u.x-v.x)*(p.y-u.y)-(u.y-v.y)*(p.x-u.x);
}
bool acoss(line u,line v)
{
    if(mul(u.a,v.a,v.b)*mul(u.b,v.a,v.b)<0&&mul(v.a,u.a,u.b)*mul(v.b,u.a,u.b)<0)return 1;
    if(mul(u.a,v.a,v.b)==0&&(u.a.x-v.a.x)*(u.a.x-v.b.x)<=0&&(u.a.y-v.a.y)*(u.a.y-v.b.y)<=0)return 1;
    if(mul(u.b,v.a,v.b)==0&&(u.b.x-v.a.x)*(u.b.x-v.b.x)<=0&&(u.b.y-v.a.y)*(u.b.y-v.b.y)<=0)return 1;
    if(mul(v.a,u.a,u.b)==0&&(v.a.x-u.a.x)*(v.a.x-u.b.x)<=0&&(v.a.y-u.a.y)*(v.a.y-u.b.y)<=0)return 1;
    if(mul(v.b,u.a,u.b)==0&&(v.b.x-u.a.x)*(v.b.x-u.b.x)<=0&&(v.b.y-u.a.y)*(v.b.y-u.b.y)<=0)return 1;
    return 0;
}
int main()
{
    int n;
    cin>>n;
    while(n--){
        int x1,y1,x2,y2;
        cin>>l[0].a.x>>l[0].a.y>>l[0].b.x>>l[0].b.y>>x1>>y1>>x2>>y2;
        if(x1>x2)swap(x1,x2);
        if(y1<y2)swap(y1,y2);
        if(x1<=l[0].a.x&&l[0].a.x<=x2
           &&x1<=l[0].b.x&&l[0].b.x<=x2
           &&y2<=l[0].a.y&&l[0].a.y<=y1
           &&y2<=l[0].b.y&&l[0].b.y<=y1)
        {
            cout<<"T"<<endl;
            continue;
        }
        l[1].a={x1,y1},l[1].b={x2,y1};
        l[2].a={x1,y1},l[2].b={x1,y2};
        l[3].a={x1,y2},l[3].b={x2,y2};
        l[4].a={x2,y1},l[4].b={x2,y2};
        int flag=0;
        for(int i=1;i<=4;i++)
            if(acoss(l[0],l[i]))
               flag=1;
        if(flag)cout<<"T"<<endl;
        else cout<<"F"<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/acjiumeng/p/6689077.html