Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9536 | Accepted: 2518 |
Description
Input
Output
Sample Input
1 4 9 11 2 1 5 7 1
Sample Output
F
思路:判断线段与矩形相交--也就是判断线段与矩形的两条对角线是否有相交,只要有一条相交则说明相交。另外,此题说明了线段在矩形内部也算是相交,并且给出的矩形的两个点可能是左上,右下和或下,左上。线段相交需要满足的条件:(设两线段的端点分别为p1(x1,y1),p2(x2,y2) 和 p3(x3,y3),p4(x4,y4)
(1)快速排斥:min(x1,x2)<=max(x3,x4) && min(x3,x4)<=max(x1,x2) && min(y1,y2)<=max(y3,y4) && min(y3,y4)<=max(y1,y2);
(2)相互跨立:既满足(p1p3 x p1p2)*(p1p2 x p1p4)>=0 && (p3p2 x p3p4)*(p3p4 x p3p1)>=0 (点的顺序不唯一,可画个图,根据叉乘的右手法则看出,其实也就是判定p3,p4在p1p2的两端,和p2,p1在p3p4的两端)
1 #include2 #include 3 #include 4 #include 5 6 using namespace std; 7 8 struct node 9 { 10 int x,y; 11 }; 12 13 node seg1,seg2,rec1,rec2,rec3,rec4; 14 15 bool inRec(node s,int x1,int x2,int y1,int y2) 16 { 17 if(s.x > max(x1,x2))return false; 18 if(s.x < min(x1,x2))return false; 19 if(s.y > max(y1,y2))return false; 20 if(s.y < min(y1,y2))return false; 21 return true; 22 } 23 24 int check(node p2,node p1,node p0) 25 { 26 return ((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y)); 27 } 28 29 bool isIntersect(node s1,node s2,node r1,node r2) 30 { 31 if(min(s1.x,s2.x)<=max(r1.x,r2.x) && 32 min(r1.x,r2.x)<=max(s1.x,s2.x) && 33 min(s1.y,s2.y)<=max(r1.y,r2.y) && 34 min(r1.y,r2.y)<=max(s1.y,s2.y) && 35 check(r1,s2,s1)*check(s2,r2,s1)>=0 && 36 check(s1,r2,r1)*check(r2,s2,r1)>=0) 37 return true; 38 return false; 39 } 40 41 int main() 42 { 43 int n; 44 scanf("%d",&n); 45 while(n--) 46 { 47 scanf("%d%d%d%d",&seg1.x,&seg1.y,&seg2.x,&seg2.y); 48 int x1,y1,x2,y2; 49 scanf("%d%d%d%d",&x1,&y1,&x2,&y2); 50 if(inRec(seg1,x1,x2,y1,y2) || inRec(seg2,x1,x2,y1,y2)) //判断是否在矩形内 51 { 52 printf("T\n"); 53 continue; 54 } 55 rec1.x = min(x1,x2); rec1.y = max(y1,y2); 56 rec2.x = max(x1,x2); rec2.y = min(y1,y2); 57 rec3.x = min(x1,x2); rec3.y = min(y1,y2); 58 rec4.x = max(x2,x2); rec4.y = max(y1,y2); 59 bool flag = false; 60 if(isIntersect(seg1,seg2,rec1,rec2)) 61 { 62 flag = true; 63 } 64 if(isIntersect(seg1,seg2,rec3,rec4)) 65 { 66 flag = true; 67 } 68 if(flag) 69 printf("T\n"); 70 else 71 printf("F\n"); 72 } 73 return 0; 74 }