大意:给你一条线段,给你一个矩形,问是否相交。
相交:线段全然在矩形内部算相交;线段与矩形随意一条边不规范相交算相交。
思路:知道详细的相交规则之后题事实上是不难的,可是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,须要又一次推断一下...真坑。
struct Point { double x, y; } A, B, C, D; struct Line { Point a, b; } L; int n; double xmult(Point p1, Point p2, Point p) { return (p1.x-p.x)*(p2.y-p.y)-(p2.x-p.x)*(p1.y-p.y); } ///若共线,返回1;不共线,返回0。 int dot_inLine(Point p1, Point p2, Point p3){ return zero(xmult(p1, p2, p3)); } ///判两点在线段同側,点在线段上返回0 int same_side(Point p1, Point p2, Line l){ return xmult(l.a, p1, l.b)*xmult(l.a, p2, l.b) > eps; } ///判点是否在线段上,包含端点 int dot_onLine_in(Point p, Line l){ return zero(xmult(p, l.a, l.b)) && (l.a.x-p.x)*(l.b.x-p.x) < eps && (l.a.y-p.y)*(l.b.y-p.y) < eps; } int intersect_in(Line u, Line v){ if (!dot_inLine(u.a, u.b, v.a) || !dot_inLine(u.a, u.b, v.b)) return !same_side(u.a, u.b,v) && !same_side(v.a, v.b,u); return dot_onLine_in(u.a, v) || dot_onLine_in(u.b, v) || dot_onLine_in(v.a, u) || dot_onLine_in(v.b, u); } bool is_Inter(Point A, Point B, Point C, Point D, Point t) { if(xmult(t, A, B) > eps && xmult(t, B, C) > eps && xmult(t, C, D) > eps && xmult(t, D, A) > eps) return true; if(xmult(t, A, B) < eps && xmult(t, B, C) < eps && xmult(t, C, D) < eps && xmult(t, D, A) < eps) return true; if(t.x >= A.x && t.x <= B.x && t.y >= C.y && t.y <= B.y && (zero(xmult(t, A, B)) || zero(xmult(t, B, C)) || zero(xmult(t, C, D)) || zero(xmult(t, D, A)))) return true; return false; } int T; void Solve() { scanf("%d", &T); while(T--) { scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &L.a.x, &L.a.y, &L.b.x, &L.b.y, &C.x, &C.y, &D.x, &D.y); A.x = min(C.x, D.x); A.y = max(C.y, D.y); C.x = max(C.x, D.x); C.y = min(C.y, D.y); B.x = C.x, B.y = A.y; D.x = A.x, D.y = C.y; if(is_Inter(A, B, C, D, L.a) && is_Inter(A, B, C, D, L.b)) { printf("T "); } else if(intersect_in((Line){A, B}, L) || intersect_in((Line){B, C,}, L) || intersect_in((Line){C, D}, L) || intersect_in((Line){D, A}, L)) { printf("T "); } else { printf("F "); } } }