#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#include<cmath>
#pragma warning(disable:4996)
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
class Point
{
public:
long double x, y;
Point() {}
Point(const long double &X, const long double &Y)
{
x = X;
y = Y;
}
};
class Line
{
public:
long double a, b, c;//ax+by=c
long double x_min, x_max, y_min, y_max;//直線的值域
Line() {}
Line(const Point &first, const Point &second)
{
x_min = std::min(first.x, second.x);
x_max = std::max(first.x, second.x);
y_min = std::min(first.y, second.y);
y_max = std::max(first.y, second.y);
if (first.x != second.x)//斜率式可行
{
b = 1;
a = -(first.y - second.y) / (first.x - second.x);
c = first.y + a*first.x;
}
else//k->無窮
{
b = 0;
a = 1;
c = first.x;
}
}
bool lineIntersected(const Line &line)//直線相交的判定
{
auto D = a*line.b - line.a*b;
if (D)
{
return true;
}
return false;
}
bool lineParallel(const Line&line)//判斷兩直線是否平行
{
auto D = a*line.b - line.a*b;
if (!D)
{
return true;
}
return false;
}
bool lineOverlapped(const Line&line)//判斷兩直線是否重合(平行的特例)
{
auto D = a*line.b - line.a*b;
auto Dx = c*line.b - line.c*b;
auto Dy = a*line.c - line.a*c;
if (!D&&!Dx&&!Dy)
{
return true;
}
return false;
}
long double fixed(long double value)
{
value *= (long double)1000000000000.0;
value += 0.5;
value = floor(value);
value /= (long double)1000000000000.0;
return value;
}
Point getIntersection(const Line&line)//行列式求兩直線交點,要修正誤差
{
auto D = a*line.b - line.a*b;
auto Dx = c*line.b - line.c*b;
auto Dy = a*line.c - line.a*c;
return{ fixed(Dx / D),fixed(Dy / D) };
}
bool segmentIntersected(const Line &line)
{
if (lineIntersected(line))
{
auto point = getIntersection(line);
if (point.x >= x_min&&point.x <= x_max
&&point.y >= y_min&&point.y <= y_max
&&point.x >= line.x_min&&point.x <= line.x_max
&&point.y >= line.y_min&&point.y <= line.y_max
)//交點在兩線段的值域內
{
return true;
}
}
return false;
}
bool segmentOverlapped(const Line &line)
{
if (lineOverlapped(line))
{
if ((x_min <= line.x_max&&x_min >= line.x_min) || (x_max <= line.x_max&&x_max >= line.x_min))
{
return true;
}
}
return false;
}
};
class Rectangle
{
public:
long double left, right, top, bottom;
bool inRectangle(const Point &point)
{
if (point.x >= left&&point.x <= right&&point.y >= bottom&&point.y <= top)
{
return true;
}
return false;
}
};
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n;
while (cin >> n)
{
for (int i = 0; i < n; i++)
{
long double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
Rectangle rectangle;
cin >> rectangle.left >> rectangle.top >> rectangle.right >> rectangle.bottom;
if (rectangle.left > rectangle.right)
{
std::swap(rectangle.left, rectangle.right);
}
if (rectangle.bottom > rectangle.top)
{
std::swap(rectangle.bottom, rectangle.top);
}
if (rectangle.inRectangle({ x1,y1 }) || rectangle.inRectangle({ x2,y2 }))
{
//只要一個端點在矩形內,就有交點
cout << 'T' << endl;
}
else
{
Line line({ x1,y1 }, { x2,y2 });
vector<long double>segment(4);
segment[0] = rectangle.top;
segment[1] = rectangle.right;
segment[2] = rectangle.bottom;
segment[3] = rectangle.left;
bool flag = false;
for (int i = 0; i < 4; i++)
{
Line another({ segment[i],segment[(i + 1) % 4] }, { segment[(i + 1) % 4],segment[(i + 2) % 4] });
if (line.segmentIntersected(another)||line.lineOverlapped(another))
{
flag = true;
break;
}
}
if (flag)
{
cout << 'T' << endl;
}
else
{
cout << 'F' << endl;
}
}
}
}
return 0;
}
<pre name="code" class="cpp">#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#include<cmath>
#pragma warning(disable:4996)
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
using std::swap;
using std::min;
using std::max;
class Point
{
public:
double x,y;
};
const double eps = 1e-20;
bool isEqual(const double &a,const double &b)
{
return (abs(a - b) < eps);
}
//判斷兩點是否相等
bool operator==(const Point &p1, const Point &p2)
{
return (isEqual(p1.x, p2.x) && isEqual(p1.y, p2.y));
}
//比較兩點座標大小,先比較x座標,若相同則比較y座標
bool operator>(const Point &p1, const Point &p2)
{
return (p1.x > p2.x || (isEqual(p1.x, p2.x) && p1.y > p2.y));
}
//計算兩向量外積(叉乘)
double operator^(const Point &p1, const Point &p2)
{
return (p1.x * p2.y - p1.y * p2.x);
}
//判定兩線段位置關係,並求出交點(如果存在)。返回值列舉如下:
//[有重合] 完全重合(6),1個端點重合且共線(5),部分重合(4)
//[無重合] 兩端點相交(3),交於線上(2),正交(1),無交(0),參數錯誤(-1)
int Intersection(Point p1, Point p2, Point p3, Point p4, Point &point)
{
//保證參數p1!=p2,p3!=p4
if (p1 == p2 || p3 == p4)
{
return -1; //返回-1代表至少有一條線段首尾重合,不能構成線段
}
//爲方便運算,保證各線段的起點在前,終點在後。
if (p1 > p2)
{
swap(p1, p2);
}
if (p3 > p4)
{
swap(p3, p4);
}
//判定兩線段是否完全重合
if (p1 == p3 && p2 == p4)
{
return 6;
}
//求出兩線段構成的向量
Point v1 = { p2.x - p1.x, p2.y - p1.y }, v2 = { p4.x - p3.x, p4.y - p3.y };
//求兩向量外積,平行時外積爲0
double Cross = v1 ^ v2;
//如果起點重合
if (p1 == p3)
{
point = p1;
//起點重合且共線(平行)返回5;不平行則交於端點,返回3
return (isEqual(Cross, 0) ? 5 : 3);
}
//如果終點重合
if (p2 == p4)
{
point = p2;
//終點重合且共線(平行)返回5;不平行則交於端點,返回3
return (isEqual(Cross, 0) ? 5 : 3);
}
//如果兩線端首尾相連
if (p1 == p4)
{
point = p1;
return 3;
}
if (p2 == p3) {
point = p2;
return 3;
}
//經過以上判斷,首尾點相重的情況都被排除了
//將線段按起點座標排序。若線段1的起點較大,則將兩線段交換
if (p1 > p3) {
swap(p1, p3);
swap(p2, p4);
//更新原先計算的向量及其外積
swap(v1, v2);
Cross = v1 ^ v2;
}
//處理兩線段平行的情況
if (isEqual(Cross, 0))
{
//做向量v1(p1, p2)和vs(p1,p3)的外積,判定是否共線
Point vs = { p3.x - p1.x, p3.y - p1.y };
//外積爲0則兩平行線段共線,下面判定是否有重合部分
if (isEqual(v1 ^ vs, 0)) {
//前一條線的終點大於後一條線的起點,則判定存在重合
if (p2 > p3) {
point = p3;
return 4; //返回值4代表線段部分重合
}
}//若三點不共線,則這兩條平行線段必不共線。
//不共線或共線但無重合的平行線均無交點
return 0;
} //以下爲不平行的情況,先進行快速排斥試驗
//x座標已有序,可直接比較。y座標要先求兩線段的最大和最小值
double ymax1 = p1.y, ymin1 = p2.y, ymax2 = p3.y, ymin2 = p4.y;
if (ymax1 < ymin1) {
swap(ymax1, ymin1);
}
if (ymax2 < ymin2) {
swap(ymax2, ymin2);
}
//如果以兩線段爲對角線的矩形不相交,則無交點
if (p1.x > p4.x || p2.x < p3.x || ymax1 < ymin2 || ymin1 > ymax2) {
return 0;
}//下面進行跨立試驗
Point vs1 = { p1.x - p3.x, p1.y - p3.y }, vs2 = { p2.x - p3.x, p2.y - p3.y };
Point vt1 = { p3.x - p1.x, p3.y - p1.y }, vt2 = { p4.x - p1.x, p4.y - p1.y };
double s1v2, s2v2, t1v1, t2v1;
//根據外積結果判定否交於線上
if (isEqual(s1v2 = vs1 ^ v2, 0) && p4 > p1 && p1 > p3) {
point = p1;
return 2;
}
if (isEqual(s2v2 = vs2 ^ v2, 0) && p4 > p2 && p2 > p3) {
point = p2;
return 2;
}
if (isEqual(t1v1 = vt1 ^ v1, 0) && p2 > p3 && p3 > p1) {
point = p3;
return 2;
}
if (isEqual(t2v1 = vt2 ^ v1, 0) && p2 > p4 && p4 > p1) {
point = p4;
return 2;
} //未交於線上,則判定是否相交
if (s1v2 * s2v2 > 0 || t1v1 * t2v1 > 0) {
return 0;
} //以下爲相交的情況,算法詳見文檔
//計算二階行列式的兩個常數項
double ConA = p1.x * v1.y - p1.y * v1.x;
double ConB = p3.x * v2.y - p3.y * v2.x;
//計算行列式D1和D2的值,除以係數行列式的值,得到交點座標
point.x = (ConB * v1.x - ConA * v2.x) / Cross;
point.y = (ConB * v1.y - ConA * v2.y) / Cross;
//正交返回1
return 1;
}
bool in(const double &value,const double &lower_bound,const double&higher_bound)
{
if (value-lower_bound>eps&&higher_bound-value>eps)
{
return true;
}
return false;
}
//主函數
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n;
while (cin >> n)
{
while (n--)
{
double x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
double left, top, right, bottom; cin >> left >> top >> right >> bottom;
if (left > right) swap(left, right);
if (bottom > top) swap(bottom, top);
if ((in(x1,left,right)&&in(y1,bottom,top))||(in(y2,bottom,top) && in(x2, left, right)))
{
cout << 'T' << endl;
continue;
}
else
{
vector<double>rectangle;
rectangle.push_back(left);
rectangle.push_back(top);
rectangle.push_back(right);
rectangle.push_back(bottom);
bool flag = false;
for (int i = 0; i < 4; i++)
{
Point point;
int type = Intersection({ x1,y1 }, { x2,y2 }, { rectangle[i % 4],rectangle[(i + 1) % 4] }, { rectangle[(i + 1) % 4] ,rectangle[(i + 2) % 4] }, point);
if (type>0)
{
cout << 'T' << endl;
flag = true;
break;
}
}
if (!flag)
{
cout << 'F' << endl;
}
}
}
}
return 0;
}