标签:des style blog http io ar color os 使用
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 12040 | Accepted: 3125 |
Description
Input
Output
Sample Input
1 4 9 11 2 1 5 7 1
Sample Output
F
题意:
有一个矩阵是实心的,给出一条线段,问线段和矩阵是否相交
分析:有很多坑点,
①给出的矩形顶点的坐标需要自己重新排下序再使用,看discuss说所谓的top等并不是严格指上方,只是一个相对的参考,所以要重新排下序再用。
②因为题目里面说了矩形的内部也算矩形的一部分,所以线段在矩形内部是认为和矩形相交的。
③在判断线段与矩形四个边是否有交点时,要注意对非规范相交的判定,当线段和边共线且不相交时叉积也为0。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <algorithm> 7 #define LL __int64 8 const int maxn = 1e2 + 10; 9 const double eps = 1e-8; 10 using namespace std; 11 12 struct node 13 { 14 double x, y; 15 }l1, l2, a, b, c, d; 16 17 double mult(node a, node b, node c) //叉积 18 { 19 return ((b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y)); 20 } 21 bool solve() //判断点在矩形里 22 { 23 if((l1.x>=a.x && l1.x<=b.x && l1.y>=d.y && l1.y<=a.y) || (l2.x>=a.x && l2.x<=b.x && l2.y>=d.y && l2.y<=a.y)) 24 return true; 25 return false; 26 } 27 bool solve2(node a, node b, node c, node d) //判断线段ab与线段cd是否相交,相交返回true,包含线段重合的情况,已测试。 28 { 29 if(max(a.x, b.x)<min(c.x, d.x)) return false; 30 if(max(a.y, b.y)<min(c.y, d.y)) return false; 31 if(max(c.x, d.x)<min(a.x, b.x)) return false; 32 if(max(c.y, d.y)<min(a.y, b.y)) return false; 33 if(mult(c, d, a)*mult(c, d, b)>0) 34 return false; 35 if(mult(a, b, c)*mult(a, b, d)>0) 36 return false; 37 return true; 38 } 39 int main() 40 { 41 int n; 42 scanf("%d", &n); 43 int xx = 1; 44 while(n--) 45 { 46 scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &l1.x, &l1.y, &l2.x, &l2.y, &a.x, &a.y, &c.x, &c.y); 47 if(a.x > c.x) { 48 b.x = a.x; 49 d.x = c.x; 50 } 51 else { 52 b.x = c.x; 53 d.x = a.x; 54 } 55 if(a.y > c.y) { 56 b.y = a.y; 57 d.y = c.y; 58 } 59 else { 60 b.y = c.y; 61 d.y = a.y; 62 } 63 a.x = d.x; a.y = b.y; 64 c.x = b.x; c.y = d.y; 65 if(solve()) 66 cout<<"T"<<endl; 67 else if(solve2(l1, l2, a, b)||solve2(l1, l2, a, d)||solve2(l1, l2, c, b)||solve2(l1, l2, c, d)) 68 cout<<"T"<<endl; 69 else cout<<"F"<<endl; 70 } 71 return 0; 72 }
poj 1410 Intersection (判断线段与矩形相交 判线段相交)
标签:des style blog http io ar color os 使用
原文地址:http://www.cnblogs.com/bfshm/p/4121963.html