Intersection
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10675 | Accepted: 2807 |
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
Source
判断矩形和线段是否相交,没什么说的,只是写的好别扭……
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define eps 1e-6
struct Point
{
double x,y;
Point(double xx,double yy)
{
x=xx;y=yy;
}
Point(){}
};
struct Obj
{
Point begin,end;
Obj(Point begin1,Point end1)
{
begin=begin1;
end=end1;
}
Obj(){}
};
bool Check(Point p,Obj rec)
{
if (p.x<=max(rec.begin.x,rec.end.x) && p.x>=min(rec.begin.x,rec.end.x) && p.y<=max(rec.begin.y,rec.end.y) && p.y>=min(rec.begin.y,rec.end.y)) return true;
return false;
}
double mul(Obj line1,Obj line2)
{
return (line1.end.x-line1.begin.x)*(line2.end.y-line2.begin.y)-(line2.end.x-line2.begin.x)*(line1.end.y-line2.begin.y);
}
bool cross(Obj line1,Obj line2)
{
if (min(line1.begin.x,line1.end.x)>max(line2.begin.x,line2.end.x)) return false;
if (min(line2.begin.x,line2.end.x)>max(line1.begin.x,line1.end.x)) return false;
if (min(line1.begin.y,line1.end.y)>max(line2.begin.y,line2.end.y)) return false;
if (min(line2.begin.y,line2.end.y)>max(line1.begin.y,line1.end.y)) return false;
Obj tmp1(line1.begin,line2.begin),tmp2(line1.begin,line2.end);
// printf("%f~~\n",mul(line1,tmp1)*mul(line1,tmp2));
if (mul(line1,tmp1)*mul(line1,tmp2)>eps) return false;
Obj tmp3(line2.begin,line1.begin),tmp4(line2.begin,line1.end);
if (mul(line2,tmp3)*mul(line2,tmp4)>eps) return false;
return true;
}
bool Inter(Obj line,Obj rec)
{
Point p1(rec.begin.x,rec.begin.y),p2(rec.begin.x,rec.end.y),p3(rec.end.x,rec.end.y),p4(rec.end.x,rec.begin.y);
Obj line1(p1,p2),line2(p2,p3),line3(p3,p4),line4(p4,p1);
if (cross(line,line1) || cross(line,line2) || cross(line,line3) || cross(line,line4)) return true;
return false;
}
int main()
{
int i,j,n,T;
Obj rec,line;
scanf("%d",&T);
while(T--)
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&line.begin.x,&line.begin.y,&line.end.x,&line.end.y,&rec.begin.x,&rec.begin.y,&rec.end.x,&rec.end.y);
bool x=Check(line.begin,rec);
bool y=Check(line.end,rec);
// printf("%d %d\n",x,y);
if (x || y)
{
printf("T\n");
continue;
}
if (Inter(line,rec))
{
printf("T\n");
}
else
{
printf("F\n");
}
}
return 0;
}