输入格式:
xstart ystart xend yend xleft ytop xright ybottom
Note: The terms top left and bottom right do not imply any ordering of coordinates.
计算几何题对我来说,光是写对就要花很久,而代码还要做到既简洁又易懂真是难上加难……
注意点在于:线段与矩形不相交,这意味着线段不仅可以在矩形外,还可以在矩形内。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t, x1, y1, x2, y2, xl, yt, xr, yb;
int a, b, c, f1, f2, f3, f4;
cin >> t;
while (t--)
{
scanf("%d%d%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &xl, &yt, &xr, &yb);
if (xl > xr) swap(xl, xr);
if (yt < yb) swap(yt, yb);
a = y1 - y2;
b = x2 - x1;
c = x1 * y2 - y1 * x2;
f1 = a * xl + b * yb + c;
f2 = a * xl + b * yt + c;
f3 = a * xr + b * yb + c;
f4 = a * xr + b * yt + c;
if ((f1>0 && f2>0 && f3>0 && f4>0) || (f1<0 && f2<0 && f3<0 && f4<0))
printf("F\n");
else if ((x1 > xr && x2 > xr) || (x1 < xl && x2 < xl))
printf("F\n");
else if ((y1 > yt && y2 > yt) || (y1 < yb && y2 < yb))
printf("F\n");
else
printf("T\n");
}
return 0;
}