Pick-up sticks(判断两条线段是否相交)
2013-10-20 20:06
495 查看
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 8351 | Accepted: 3068 |
Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.
Input
Input consists of a number of cases. The data for each case start with 1 <= n <= 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.
Output
For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.
The picture to the right below illustrates the first case from input.
#include<stdio.h> #include<algorithm> #include<math.h> using namespace std; const int maxn = 100010; struct point { double x,y; point(){} point(double a,double b):x(a),y(b) {} friend point operator - (const point &a,const point &b) { return point(a.x-b.x,a.y-b.y); } friend double operator ^(const point &a,const point &b) { return a.x*b.y-a.y*b.x; } friend double operator *(const point &a, const point &b) { return a.x*b.x + a.y*b.y; } }; struct line { point a; point b; int flag; line (){} line (point x, point y):a(x),b(y) {} }L[maxn]; const double eps = 1e-8; int cmp(double x) { if(fabs(x) < eps) return 0; if(x > 0) return 1; return -1; } bool inter(line L1, line L2)//判断两条线段是否相交 { return //快速排斥实验 max(L1.a.x,L1.b.x) >= min(L2.a.x,L2.b.x) && max(L2.a.x,L2.b.x) >= min(L1.a.x,L1.b.x) && max(L1.a.y,L1.b.y) >= min(L2.a.y,L2.b.y) && max(L2.a.y,L2.b.y) >= min(L1.a.y,L1.b.y) && //跨立实验 cmp((L2.a-L1.a)^(L1.b-L1.a))*cmp((L2.b-L1.a)^(L1.b-L1.a)) <= 0 && cmp((L1.a-L2.a)^(L2.b-L2.a))*cmp((L1.b-L2.a)^(L2.b-L2.a)) <= 0; } int main() { int n; while(~scanf("%d",&n) && n) { double x1,y1,x2,y2; struct point p1,p2; for(int i = 0; i < n; i++) { scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2); p1.x = x1; p1.y = y1; p2.x = x2; p2.y = y2; L[i].a = p1; L[i].b = p2; L[i].flag = 1; } for(int i = 0; i < n-1; i++) { for(int j = i+1; j < n; j++) { if(inter(L[i],L[j]))//如果第i条线段与第j条线段(i<j)相交,第i条线段显然不可能是最上边的; { L[i].flag = 0; break; } } } printf("Top sticks:"); int ok = 0; for(int i = 0; i < n; i++) { if(L[i].flag == 1) { if(ok == 0) { printf(" %d",i+1); ok = 1; } else printf(", %d",i+1); } } printf(".\n"); } return 0; }
View Code
判断两条线段是否相交问题:
(1) 快速排斥试验
设以线段 P1P2 为对角线的矩形为 R , 设以线段 Q1Q2 为对角线的矩形为 T ,如果 R 和 T
不相交,显然两线段不会相交。
(2) 跨立试验
如果两线段相交,则两线段必然相互跨立对方。若 P1P2 跨立 Q1Q2 ,则矢量 ( P1 - Q1 ) 和
( P2 - Q1 ) 位于矢量 ( Q2 - Q1 ) 的两侧,
即 ( P1 - Q1 ) × ( Q2 - Q1 ) * ( P2 - Q1 ) × ( Q2 - Q1 ) < 0 。
上式可改写成 ( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) > 0 。
当 ( P1 - Q1 ) × ( Q2 - Q1 ) = 0 时,说明 ( P1 - Q1 ) 和 ( Q2 - Q1 ) 共线,
但是因为已经通过快速排斥试验,所以 P1 一定在线段 Q1Q2 上;
同理, ( Q2 - Q1 ) ×(P2 - Q1 ) = 0 说明 P2 一定在线段 Q1Q2 上。
所以判断 P1P2 跨立 Q1Q2 的依据是:
( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) >= 0 。
同理判断 Q1Q2 跨立 P1P2 的依据是:
( Q1 - P1 ) × ( P2 - P1 ) * ( P2 - P1 ) × ( Q2 - P1 ) >= 0 。
当快速排斥实验和跨立实验都满足时,才说明两天线段相交;
相关文章推荐
- POJ 2653:Pick-up sticks _判断两线段是否相交
- poj 2653 Pick-up sticks(判断线段是否相交)
- Pick-up sticks(判断线段是否相交)
- POJ 2653 Pick-up sticks 枚举判断线段是否相交
- poj2653——Pick-up sticks(判断线段是否相交)
- Pick-up sticks--poj2653(判断两线段是否相交)
- (计算几何step8.1.2.1)POJ 2653 Pick-up sticks(判断一根木棒的上面时候还有其他木棒——判断两条直线是否相交)
- 判断两条线段是否相交(三种算法)
- POJ 2653 Pick-up sticks 判断线段相交
- 判断两条线段是否相交(三种算法)
- hdu acm 1147 Pick-up sticks(判断线段相交)
- 判断两条线段是否相交
- HDU 1147 Pick-up sticks (叉乘判断线段相交)
- poj 2653 Pick-up sticks 线段相交判断
- 判断两条线段是否相交
- poj 2653 Pick-up sticks(判断线段相交)
- POJ 2653 - Pick-up sticks(判断两条直线相交)
- hdu 1147:Pick-up sticks(基本题,判断两线段相交)
- POJ 2653 Pick-up sticks (判断线段相交)
- 判断两条线段是否相交