计算几何入门
判断线段是否与一个矩形相交
包含在内部也算
那么判断是否在矩形内部,只需叉积即可
判相交就判断跟矩形的四条线段相交即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <map>
#include <sstream>
#include <queue>
#include <vector>
#define MAXN 100005
#define MAXM 211111
#define eps 1e-8
#define INF 500000001
using namespace std;
inline int dblcmp(double d)
{if(fabs(d) < eps) return 0;return d > eps ? 1 : -1;
}
struct point
{double x, y;point(){}point(double _x, double _y): x(_x), y(_y) {}void input(){scanf("%lf%lf", &x, &y);}bool operator ==(point a)const{return dblcmp(a.x - x) == 0 && dblcmp(a.y - y) == 0;}point sub(point p){return point(x - p.x, y - p.y);}double dot(point p){return x * p.x + y * p.y;}double det(point p){return x * p.y - y * p.x;}double distance(point p){return hypot(x - p.x, y - p.y);}
}lft, rht;
struct line
{point a, b;line(){}line(point _a, point _b){ a = _a; b = _b;}void input(){a.input();b.input();}int segcrossseg(line v){int d1 = dblcmp(b.sub(a).det(v.a.sub(a)));int d2 = dblcmp(b.sub(a).det(v.b.sub(a)));int d3 = dblcmp(v.b.sub(v.a).det(a.sub(v.a)));int d4 = dblcmp(v.b.sub(v.a).det(b.sub(v.a)));if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2)return 2;return (d1 == 0 && dblcmp(v.a.sub(a).dot(v.a.sub(b))) <= 0||d2 == 0 && dblcmp(v.b.sub(a).dot(v.b.sub(b))) <= 0||d3 == 0 && dblcmp(a.sub(v.a).dot(a.sub(v.b))) <= 0||d4 == 0 && dblcmp(b.sub(v.a).dot(b.sub(v.b))) <= 0);}}seg;
int main()
{int T;scanf("%d", &T);while(T--){seg.input();lft.input();rht.input();if(lft.x > rht.x) swap(lft.x, rht.x);if(lft.y < rht.y) swap(lft.y, rht.y);int flag = 0;if(dblcmp(seg.a.x - lft.x) >= 0 && dblcmp(seg.a.x - rht.x) <= 0 && dblcmp(seg.a.y - rht.y) >= 0 && dblcmp(seg.a.y - lft.y) <= 0) flag = 1;if(dblcmp(seg.b.x - lft.x) >= 0 && dblcmp(seg.b.x - rht.x) <= 0 && dblcmp(seg.b.y - rht.y) >= 0 && dblcmp(seg.b.y - lft.y) <= 0) flag = 1;line a = line(lft, point(lft.x, rht.y));line b = line(lft, point(rht.x, lft.y));line c = line(rht, point(rht.x, lft.y));line d = line(rht, point(lft.x, rht.y));if(flag || a.segcrossseg(seg) || b.segcrossseg(seg) || c.segcrossseg(seg) || d.segcrossseg(seg)) puts("T");else puts("F");}return 0;
}