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
Source
Southwestern European Regional Contest 1995
除了线段完全在矩形内和线段所在直线斜率不存在两种特殊情况外,相交的情况只有以下四种及其组合:
线段与x=xl、x=xr、y=yb、y=yt分别相交,讨论即可。
/*The rectangle consists of four straight lines and the area in between.注意矩形是实心的*/
#include<iostream>
using namespace std;
int xs, ys, xe, ye, xl, yt, xr, yb;
void swap(int &a, int &b)
{
if (a > b)
{
int t = a;
a = b;
b = t;
}
}
bool f1(int x)
{
return x >= xl&&x <= xr;
}
bool f2(int y)
{
return y >= yb&&y <= yt;
}
bool judge(int x)
{
return (x >= xs&&x <= xe) || (x >= xe&&x <= xs);
}
bool judge1(int y)
{
return (y >= ys&&y <= ye) || (y >= ye&&y <= ys);
}
bool judge_x(double x)
{
return x >= (double)xl&&x <= (double)xr;
}
bool judge_y(double y)
{
return y >= (double)yb&&y <= (double)yt;
}
int main()
{
int n;
cin >> n;
while(n--)
{
cin >> xs >> ys >> xe >> ye >> xl >> yt >> xr >> yb;
swap(xl, xr); //The terms top left and bottom right do not imply any ordering of coordinates.注意矩形端点大小,并非按照左上、右下的顺序给出
swap(yb, yt);
int b1 = ys, b2 = ye;
swap(b1, b2);
if (f1(xs) && f1(xe) && f2(ys) && f2(ye)) //线段在矩形内
{
cout << 'T' << endl;
continue;
}
if (xs == xe) //斜率不存在
{
if (f1(xs))
if (f2(ys) || f2(ye) || (b1 <= yb&&b2 >= yt))
{
cout << 'T' << endl;
continue;
}
}
double slope = 1.0*(ys - ye) / (xs - xe);
double b = ys - slope*xs;
if (judge(xl))
{
double y = slope*xl + b;
if (judge_y(y))
{
cout << 'T' << endl;
continue;
}
}
if (judge(xr))
{
double y = slope*xr + b;
if (judge_y(y))
{
cout << 'T' << endl;
continue;
}
}
if (judge1(yb))
{
double x = (yb - b) / slope;
if (judge_x(x))
{
cout << 'T' << endl;
continue;
}
}
if (judge1(yt))
{
double x = (yt - b) / slope;
if (judge_x(x))
{
cout << 'T' << endl;
continue;
}
}
cout << 'F' << endl;
}
return 0;
}
本文介绍了一个程序设计问题,即如何判断一条线段是否与一个矩形相交。通过具体的输入输出示例和C++代码实现,详细解释了判断逻辑,包括特殊情况处理。
614

被折叠的 条评论
为什么被折叠?



