Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13094 | Accepted: 3409 |
Description
Input
Output
Sample Input
1 4 9 11 2 1 5 7 1
Sample Output
F
题意:给你一个线段和一个矩形,问你线段是否与矩形面相交。
思路:先判断线段的两个端点是否在矩形里面,然后判断线段与矩形4条边是否相交。
坑死啊,康总模板。。。少个if WA到死!
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <map> #include <vector> #include <string> #define INF 0x3f3f3f3f #define eps 1e-6 #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 #define debug printf("\n"); #define MAXN 1000000+1 #define MAXM 100000 #define LL long long using namespace std; struct Point{ double x, y; Point(double x = 0, double y = 0) : x(x), y(y){} }; Point operator - (Point A, Point B){ return Point(A.x-B.x, A.y-B.y); } double Cross(Point A, Point B){ return A.x * B.y - A.y * B.x; } int dcmp(double x){ if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } bool judge(Point a1, Point a2, Point b1, Point b2) { if(min(a1.x, a2.x) > max(b1.x, b2.x) || min(a1.y, a2.y) > max(b1.y, b2.y) || min(b1.x, b2.x) > max(a1.x, a2.x) || min(b1.y, b2.y) > max(a1.y, a2.y)) return false; double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1); double c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1); return dcmp(c1) * dcmp(c2) < eps && dcmp(c3) * dcmp(c4) < eps; } int main() { int t; scanf("%d", &t); while(t--) { Point l1, l2; double x1, y1, x2, y2; scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &l1.x, &l1.y, &l2.x, &l2.y, &x1, &y1, &x2, &y2); if(x1 > x2) swap(x1, x2); if(y1 < y2) swap(y1, y2); if((l1.x > x1 && l1.x < x2 && l1.y < y1 && l1.y > y2) || (l2.x > x1 && l2.x < x2 && l2.y < y1 && l2.y > y2)) { printf("T\n"); continue; } Point b, c, d, e; b.x = x1, b.y = y1; c.x = x2, c.y = y1; d.x = x1, d.y = y2; e.x = x2, e.y = y2; if(judge(l1, l2, b, c) || judge(l1, l2, b, d) || judge(l1, l2, d, e) || judge(l1, l2, c, e)) printf("T\n"); else printf("F\n"); } return 0; }