描述
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.
输入
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.
输出
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.
样例输入
1
4 9 11 2 1 5 7 1
样例输出
F
题目来源
http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1879
注意点:
1.线段在矩形内算作与矩形相交
2.给出的矩形坐标不一定为先左上后右下,这需要自己改下(x1<x2,y1>y2)
思路:
1.判断线段所在直线是否经过矩形,这个可以判断矩形的各个点是否在线段所在直线的同一边,如果在同一边那么不相交.
2.判断线段的端点是否经过矩形的边
3.答案出来了
#include <iostream>
using namespace std;
struct Node
{
double x,y;
};
double f(Node o,Node a ,Node b)
{
return (a.x-o.x)*(o.y-b.y)-(o.x-b.x)*(a.y-o.y);
}
bool tong(Node *d , Node *x)
{
int ans = 0;
for(int i = 0 ; i < 4 ; i++)
{
if(f(d[i],x[0],x[1])>-0.000001 &&f(d[i],x[0],x[1])<0.00001)
return false;
else if(f(d[i],x[0],x[1])>0)
ans++;
}
if(ans==4 || ans==0)
return true;
return false;
}
int main()
{
int n;
Node d[4],x[2];
cin>>n;
while(n--)
{
double ax,ay,bx,by;
cin>>x[0].x>>x[0].y>>x[1].x>>x[1].y>>ax>>ay>>bx>>by;
d[0].x = min(ax,bx); d[0].y = max(ay,by);
d[1].x = max(ax,bx); d[1].y = min(ay,by);
d[2].x = d[0].x ; d[2].y = d[1].y;
d[3].x = d[1].x ; d[3].y = d[0].y;
if(tong(d,x))
{
cout<<"F"<<endl;
}
else if(max(x[0].x,x[1].x)<min(d[0].x,d[1].x) ||
min(x[0].x,x[1].x)>max(d[0].x,d[1].x) ||
max(x[0].y,x[1].y)<min(d[0].y,d[1].y) ||
min(x[0].y,x[1].y)>max(d[0].y,d[1].y))
{
cout<<"F"<<endl;
}
else
{
cout<<"T"<<endl;
}
}
return 0;
}
附上一些测试数据(来源于网络):
68 4 9 11 2 1 1 7 5 11 2 4 9 1 1 7 5 12 12 24 24 19 5 25 17 4 6 15 9 1 1 11 11 19 5 25 17 12 12 24 24 0 18 8 12 1 1 11 11 2 4 4 2 1 1 11 11 -4 9 -11 2 -1 1 -7 5 -11 2 -4 9 -1 1 -7 5 -12 12 -24 24 -19 5 -25 17 -4 6 -15 9 -1 1 -11 11 -19 5 -25 17 -12 12 -24 24 0 18 -8 12 -1 1 -11 11 -2 4 -4 2 -1 1 -11 11 4 -9 11 -2 1 -1 7 -5 11 -2 4 -9 1 -1 7 -5 12 -12 24 -24 19 -5 25 -17 4 -6 15 -9 1 -1 11 -11 19 -5 25 -17 12 -12 24 -24 0 -18 8 -12 1 -1 11 -11 2 -4 4 -2 1 -1 11 -11 -4 -9 -11 -2 -1 -1 -7 -5 -11 -2 -4 -9 -1 -1 -7 -5 -12 -12 -24 -24 -19 -5 -25 -17 -4 -6 -15 -9 -1 -1 -11 -11 -19 -5 -25 -17 -12 -12 -24 -24 0 -18 -8 -12 -1 -1 -11 -11 -2 -4 -4 -2 -1 -1 -11 -11 9 1 9 2 4 3 9 6 9 2 9 1 4 3 9 6 10 3 13 3 4 3 9 6 13 3 10 3 4 3 9 6 10 6 14 6 4 3 9 6 14 6 10 6 4 3 9 6 9 7 9 10 4 3 9 6 9 10 9 7 4 3 9 6 4 7 4 10 4 3 9 6 4 10 4 7 4 3 9 6 0 6 3 6 4 3 9 6 3 6 0 6 4 3 9 6 1 3 3 3 4 3 9 6 3 3 1 3 4 3 9 6 4 0 4 2 4 3 9 6 4 2 4 0 4 3 9 6 5 3 8 5 4 3 9 6 8 5 5 3 4 3 9 6 5 3 8 3 4 3 9 6 8 3 5 3 4 3 9 6 6 4 6 5 4 3 9 6 6 5 6 4 4 3 9 6 4 3 9 6 4 3 9 6 9 6 4 3 4 3 9 6 4 3 5 4 4 3 9 6 5 4 4 3 4 3 9 6 5 3 8 3 4 3 9 6 8 3 5 3 4 3 9 6 5 3 9 3 4 3 9 6 9 3 5 3 4 3 9 6 4 4 4 5 4 3 9 6 4 5 4 4 4 3 9 6 4 3 4 5 4 3 9 6 4 5 4 3 4 3 9 6 4 3 4 6 4 3 9 6 4 6 4 3 4 3 9 6 9 2 9 5 4 3 9 6 9 5 9 2 4 3 9 6 9 2 9 7 4 3 9 6 9 7 9 2 4 3 9 6
F F F T T F T F F F T T F T F F F T T F T F F F T T F T F F F F F F F F F F F F F F F F T T T T T T T T T T T T T T T T T T T T T T T T