Given a group of points on a plane. The Convex Hull algorithm calculates the shape (made up from the points itself) containing all these points. It can also be used on a collection of points of different dimensions. This implementation however covers points on a plane. It essentially calculates the lines between points which together contain all points. In comparing different solutions to this problem we can describe each algorithm in terms of it's big-O time complexity.
There are multiple Convex Hull algorithms but this solution is called Quickhull, is comes from the work of both W. Eddy in 1977 and also separately A. Bykat in 1978, this algorithm has an expected time complexity of O(n log n), but it's worst-case time-complexity can be O(n^2) . With average conditions the algorithm has ok efficiency, but it's time-complexity can start to head become more exponential in cases of high symmetry or where there are points lying on the circumference of a circle for example.
The quickhull algorithm works as follows:
Our functioni will have the following defininition:
findHull(points: [CGPoint], p1: CGPoint, p2: CGPoint)
findHull(S1, A, B)
findHull(S2, B, A)
What this function does is the following:
points
is empty we return as there are no points to the right of our line to add to our hull.p1
to p2
.points
that is furthest away from this line. (maxPoint
)maxPoint
to the hull right after p1
.line1
) from p1
to maxPoint
.line2
) from maxPoint
to p2
. (These lines now form a triangle)points
are to the right of line1
these are grouped in an array s1
.line2
are grouped in an array s2
. Note that there are no points that are both to the right of line1
and line2
as then maxPoint
wouldn't be the point furthest away from our initial line between p1
and p2
.findHull(_, _, _)
again on our new groups of points to find more hull points.findHull(s1, p1, maxPoint)
findHull(s2, maxPoint, p2)
This eventually leaves us with an array of points describing the convex hull.
Written for the Swift Algorithm Club by Jaap Wijnen.