实验中遇到的一个值得思考的问题
3.2
题目描述:
Implement convexHull, which computes the convex hull, the smallest convex set that contains all the points in a set of input points. The gift-wrapping algorithm is one simple approach to this problem, and there are other algorithms too. Remember that 6.031 focuses on SFB/ETU/RFC, so your code will not be judged by its performance.
The specification comment above convexHull requires you to return the “minimal subset of the input points that form the vertices of the perimeter of the convex hull.” This statement is well-defined on any set of input points. What should you do for a set of 2 points? 1 point? 0 points? What should you do for a point that lies on an edge of the convex hull, but is not a vertex of that edge?
大体意思就是要求我们完成一个闭包计算的问题,题目暗示我们可以使用gift-wrapping algorithm,经过网上查找该算法的相关资料,知道了计算凸包的几个步骤:
首先要选出最左下角 的点
接着需要选出距离选出的点极角最小的点,如果有角度相同的,那么比较距离,将较远的点加入凸包中
重复上述操作,直到形成凸包
在此问题中我遇到的问题是不会界定终止条件,开始我想的是遍历所有的点,当所有点都被遍历过一次后该算法终止,可是这样的终止条件并不能保证我们得到正确的闭包。经过思考,我选择在开始将选出的左下角的点打上已经被访问过的标记,在选出第二个凸包内的点后将开始左下角点打上的访问过的标记取消,并将终止条件设置为左下角的那个点再次加入凸包。经过这样的修改,程序可以运行产生正确的凸包。时间复杂度为O(n^{2})