1 /* 2 * 题目要求:判断线段是否跟矩形相交 3 * 注意:线段完全在矩形内也是相交 4 * 解法:分别判断线段是否跟矩形的四条边相交,再判断线段的两个端点是否都在矩形内 5 */ 6 7 #include <cstdio> 8 #include <cstdlib> 9 #include <iostream> 10 11 using namespace std; 12 13 struct point { 14 double x; 15 double y; 16 }A, B, C, D, E, F; 17 18 double crossProd(point A, point B, point C) { 19 return (B.x-A.x)*(C.y-A.y) - (B.y-A.y)*(C.x-A.x); 20 } 21 22 bool segIntersect(point A, point B, point C, point D) {//判断线段是否相交 23 if (max(A.x, B.x) >= min(C.x, D.x) && 24 max(C.x, D.x) >= min(A.x, B.x) && 25 max(A.y, B.y) >= min(C.y, D.y) && 26 max(C.y, D.y) >= min(A.y, B.y) && 27 crossProd(C, D, A)*crossProd(C, B, D)>=0 && 28 crossProd(A, B, C)*crossProd(A, D, B)>=0) return true; 29 return false; 30 } 31 32 bool pointInRectangle(point S, point A, point B) {//判断点是否在矩形内 33 if (max(A.x, B.x) >= S.x && 34 min(A.x, B.x) <= S.x && 35 max(A.y, B.y) >= S.y && 36 min(A.y, B.y) <= S.y) return true; 37 return false; 38 } 39 40 bool segXRectangle() { 41 if (segIntersect(A, B, C, E)) return true;//判断线段是否跟矩形的边相交 42 else if (segIntersect(A, B, C, F)) return true; 43 else if (segIntersect(A, B, D, E)) return true; 44 else if (segIntersect(A, B, D, F)) return true; 45 if (pointInRectangle(A, C, D) && pointInRectangle(B, C, D)) return true;//判断线段的两端点是否都在矩形内 46 return false; 47 } 48 49 int main() { 50 int t; 51 scanf ("%d", &t); 52 while (t--) { 53 scanf ("%lf%lf%lf%lf%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y, &D.x, &D.y); 54 E.x = C.x; 55 E.y = D.y; 56 F.x = D.x; 57 F.y = C.y; 58 bool yes = segXRectangle(); 59 if (yes) printf ("T\n"); 60 else printf ("F\n"); 61 } 62 return 0; 63 }