本文是基于 合工大老师徐本柱的教材
什么是凸包:
中间涉及问题
线之间是否相交?
首先使用快速排斥
再判断线段是否跨立
如何用栈简化算法:
会涉及到的知识:
叉积 栈的使用
叉积是有方向的,当叉积为正代表逆时针,为true,
当叉积为正代表顺时针,为false,那么叉积为0的时候也为ture (原因查看sortByPolorAngle函数)
看注释也可得知!
说一下具体思路(对照着代码和注释就很容易理解了):
1.找出集中最下方的点,如果这样的点不止一个,那么用最下面最左边的点。
2.那么把这个点设为P0是凸包子集中的一个点
3.设想P0栓了一根皮筋如上图,放在P0成水平的右侧
4.沿逆时针方向转动,首先碰到P1(一定要逆时针旋转,如果不是逆时针选择,那么果断抛弃找下一个点,例如p1p2到p1p3就不是逆时针旋转了,果断抛弃P2。)
5.以P1为中心做和P0一样的事情,碰P3
6.循环,直到再次碰到P0,循环结束
下面展示一些 代码
。
我觉得注释写的很清楚了 ,看着注释对着思路,基本上没问题的。
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<double,double> POINT;
// function dirction determines the direction that the segment
//p1p turns to p2p with respect to point p
//if return value is positive, means clockwise;
//if return value is negative, menas counter-clockwise;
// naught means on the same line;
double direction(POINT p,POINT p1,POINT p2)
{
POINT v1,v2;
v1.first =p2.first -p.first ;
v1.second=p2.second-p.second;