您的位置:首页 > 其它

poj 1410 Intersection(判断线段是否与实心矩形相交)

2017-10-11 23:30 609 查看
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<iostream>
#include<set>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const double eps=1e-8;
const double INF=0x3f3f3f3f;

int dcmp(double x)
{
if(fabs(x)<eps) 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(y3<y4)
{
swap(y3,y4);
}

Point p1=Point(x3,y3);
Point p2=Point(x4,y3);
Point p3=Point(x3,y4);
Point p4=Point(x4,y4);
Line L1=Line(p1,p2);
Line L2=Line(p1,p3);
Line L3=Line(p2,p4);
Line L4=Line(p3,p4);
if(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: