Intersection
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11953 | Accepted: 3096 |
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.
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.
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
题意:判断线段是否跟矩形相交。线段只要在矩形内也算相交。然后给出矩形的两个端点,左上和右下也不是确定x,y大小,需要进行判断,然后判断线段相交这里,如果在是同一条线段。-eps,一个eps。然后再判断下端点是否在矩形内。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
using namespace std;
struct point {
double x,y;
point (){}
point (double x,double y):x(x),y(y){}
};
struct line {
point a,b;
line(){}
line(point a,point b):a(a),b(b){}
};
point p1,p2,p3,p4;
int in()
{
if (p1.x>=p3.x && p1.x<=p4.x && p1.y>=p4.y && p1.y<=p3.y)
return 1;
if (p2.x>=p3.x && p2.x<=p4.x && p2.y>=p4.y && p2.y<=p3.y)
return 1;
return 0;
}
point operator -(point a,point b)
{
return point(a.x-b.x,a.y-b.y);
}
double cross(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
int pd(point a,point b,point c,point d)
{
double k1=cross(b-a,c-a),k2=cross(b-a,d-a);
double k3=cross(d-c,a-c),k4=cross(d-c,b-c);
if (k1*k2<eps && k3*k4<-eps) return 1;
return 0;
}
int main()
{
int T;
double x1,x2,x3,x4,y1,y2,y3,y4;
//freopen("out.txt","w",stdout);
scanf("%d",&T);
while (T--)
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
if (x3>x4)
{
x3=x3+x4;
x4=x3-x4;
x3=x3-x4;
}
if (y3<y4)
{
y3=y3+y4;
y4=y3-y4;
y3=y3-y4;
}
p1=point(x1,y1);
p2=point(x2,y2);
p3=point(x3,y3);
p4=point(x4,y4);
line l1=line(p1,p2);
line l[5];
l[1].a=p3;l[1].b=point(x4,y3);
l[2].a=p3;l[2].b=point(x3,y4);
l[3].a=point(x3,y4);l[3].b=p4;
l[4].a=p4;l[4].b=point(x4,y3);
if (in())
{
cout<<"T"<<endl;
continue;
}
int flag=1;
for (int i=1;i<=4;i++)
if (pd(p1,p2,l[i].a,l[i].b))
{
cout<<"T"<<endl;
flag=0;
break;
}
if (flag) cout<<"F"<<endl;
}
return 0;
}