Pick-up sticks——计算几何 两线段相交

Pick-up sticks

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.html

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.ios

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.c++

The picture to the right below illustrates the first case from input.
在这里插入图片描述
Sample Inputweb

5
1 1 4 2
2 3 3 1
1 -2.0 8 4
1 4 8 2
3 3 6 -2.0
3
0 0 1 1
1 0 2 1
2 0 3 1
0

Sample Output算法

Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.

Hint
Huge input,scanf is recommended.app

题意:
给定一个指定数目的棍子(数量是n),每次扔出去一根(从1号扔到n号),直到最后一根扔完。求在扔出去的这些棍子中有哪些棍子是在这堆棍子的顶端(即该号棍子上方没有棍子覆盖了)。dom

c++ AC 代码ide

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define EPS 1e-10
const int MAX = 100010;
bool ans[MAX];
int n;

double add(double a,double b)
{
	if(fabs(a+b) < EPS * (fabs(a) + fabs(b)) return 0;
	return a + b;
}

struct P
{
	double x,y;
	P() {}
	P(double x,double y) : x{x],y(y) {}
	P operator + (P p)
	{
		return P(add(x,p.x),add(y,p.y));
	}
	P operator - (P p)
	{
		return P(add(x,-p,x),add(y,-p.y));
	}
	P operator * (double d)
	{
		return P(x*d,y*d);
	}
	double dot (P p)
	{
		return add(x*p.x,y*p.y);
	}
	double det (P p)
	{
		return add(x*p.y,-y*p.x);
	}
};
P pset[MAX][2];

double ccw(P s,P e,P p) // s表明起点 e表明终点 p是须要判断的点
{
	P a = e - s,b = p - s;
	if(a.det(b) > 0) return 1;		// a、b叉积为正说明p在线段的逆时针方向
	else if(a.det(b) < 0) return -1;	// 叉积为负 p 在线段的逆时针方向
	// 如下三种状况点在线段所在的直线上
	else if(a.dot(b) < 0) return 2;		// 点积小于0 两向量反向
	else if(a.dot(a) < b.dot(b)) return -2;	  // a的长度小于b的长度 a在b上
	return 0;   // 点在线上(b的长度小于a的长度)
}

bool interesect(P p0,P p1,P p2,P p3,P p4)
{
	return (ccw(p0,p1,p2)*ccw(p0,p1,p3)<=0 && ccw(p2,p3,p0)*ccw(p2,p3,p1)<=0);
}


int main()
{
	while(~scanf("%d",&n) && n)
	{
		scanf("%lf%lf%lf%lf",&pset[0][0].x,&pset[0][0].y,&pset[0][1].x,&pset[0][1],y);
		for(int i=1;i<n;i++)
		{
			scanf("%lf%lf%lf%lf",&pset[i][0].x,&pset[i][0].y,&pset[i][1].x,&pset[i][1],y);
			ans[i] = interesect(pset[i-1][0],pset[i-1][1],pset[i][0],pset[i][1]); // 先进行一轮相邻的放的棍子的判断,减轻后面判断的压力(由于算法不够优秀,只能这样)
		}
		printf("Top sticks: ");
		for(int i=0;i<n;i++)
		{
			if(!ans[i+1]) // 判断不是相邻的放的棍子是否与当前棍子相交,一有相交就说明这根棍子不是最顶上的,当即退出
				fo(int j=i+1;j<n;j++)
				{
					ans[i+1] = interesect(pset[i][0],pset[i][1],pset[j][0],pset[j][1]);
					if(ans[i+1) break;  // 这里至关于一个“剪枝”的步骤 不加这个会超时
				}
			if(!ans[i+1] && i!=n-1)
				printf("%d, ",i+1);
		}
		printf("%d.\n",n);
	}
	return 0;
}

 

对ccw函数的一些解释

  • 首先介绍一些一些叉积的知识
    在这里插入图片描述
    咱们看到上图,P2在P1的顺时针方向,这时这两个向量的叉积为负数(感兴趣的能够本身假定两个向量去试一试);相反当P2在P1逆时针方向时两个向量的叉积为正;而共线的时候两个的叉积为0。
  • 如今咱们利用上面的性质就能够来判断两线段是否相交了。
    在这里插入图片描述咱们看到上图P0P1与P2P3是存在交点的。咱们拆解来看,看到 P 2 P 0 \overrightarrow{P_2P_0} P 2 P 3 \overrightarrow{P_2P_3} P 2 P 3 \overrightarrow{P_2P_3} , P 2 P 1 \overrightarrow{P_2P_1} ,P0与P1这两个点分别是位于 P 2 P 3 \overrightarrow{P_2P_3} 的顺时针方向与逆时针方向,结合咱们的代码来看,就是如下两句:
if(a.det(b) > 0) return 1;		// a、b叉积为正说明p在线段的逆时针方向
else if(a.det(b) < 0) return -1; // 叉积为负 p 在线段的逆时针方向

当咱们分别给ccw函数传递ccw(P2,P3,P0)ccw(P2,P3,P1)时咱们获得的返回结果是一正一负,经过这两个结果相乘咱们勉强能够认为这两个线段是相交的,为何是勉强?由于还有下面的状况:
在这里插入图片描述
这种状况也是会产生一正一负的状况,可是两线段并不相交。因此咱们有了interesect(P0,P1,P2,P3)函数来进行下一步的判断。我svg

(ccw(p0,p1,p2)*ccw(p0,p1,p3)<=0 && ccw(p2,p3,p0)*ccw(p2,p3,p1)<=0);

们只有用时保证P2、P3也是别落在 P 0 P 1 \overrightarrow{P_0P_1} ,的两侧(顺时针和逆时针方向)才能判定两直线是相交的。函数

  • 至此,代码解释的差很少了,还有剩下的两线段共线以及一条线段的一端落在另一条线段上的状况结合代码注释以及本身动手画画图就能很好的理解了。