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
大致题意:告诉你线段的两个端点坐标和矩形的两个端点坐标,问线段是否和矩形相交,矩形是实心的。
思路:判断一下线段是否和矩形的四条边相交,如果没有的话再判断下是否在矩形内部。
注意:题目给你的矩形的那两个坐标不一定就是左上角和右下角,也可能是左下角和右上角,而且顺序也不一定,所以要先自己判断下。(这里被坑哭了。。。。)
代码如下
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const double eps=1e-8;
const double INF=0x3f3f3f3f;
int dcmp(double x)
{
if(fabs(x)return 0;
return x<0?-1: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);
}
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;
}
int operator ==(const Point &b)const {
if(dcmp(x-b.x)==0&&dcmp(y-b.y)==0)//两点相同
return 0;
else return 1;
}
};
struct Line
{
Point a,b;
Line() {}
Line(Point _a,Point _b)
{
a=_a;
b=_b;
}
};
double xmult(Point p0,Point p1,Point p2)
{
return (p1-p0)^(p2-p0);
}
bool inter(Line L1,Line L2)//判断两条线段是否相交,相交返回1
{
return
max(L1.a.x,L1.b.x) >= min(L2.a.x,L2.b.x) &&
max(L2.a.x,L2.b.x) >= min(L1.a.x,L1.b.x) &&
max(L1.a.y,L1.b.y) >= min(L2.a.y,L2.b.y) &&
max(L2.a.y,L2.b.y) >= min(L1.a.y,L1.b.y) &&
dcmp((L2.a-L1.a)^(L1.b-L1.a))*dcmp((L2.b-L1.a)^(L1.b-L1.a)) <= 0 &&
dcmp((L1.a-L2.a)^(L2.b-L2.a))*dcmp((L1.b-L2.a)^(L2.b-L2.a)) <= 0;
}
int main()
{
int n;
double x1,y1,x2,y2,x3,y3,x4,y4;
scanf("%d",&n);
while(n--)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
Line L=Line(Point(x1,y1),Point(x2,y2));
scanf("%lf%lf%lf%lf",&x3,&y3,&x4,&y4);
if(x3>x4)//先自己找出矩形的左上角和右下角坐标
{
swap(x3,x4);
}
if(y3if(x1>=x3&&x2>=x3&&x1<=x4&&x2<=x4&&y1<=y3&&y2<=y3&&y1>=y4&&y2>=y4)
{
printf("T\n");
continue;
}
if(inter(L,L1)||inter(L,L2)||inter(L,L3)||inter(L,L4))
printf("T\n");
else
printf("F\n");
}
return 0;
}