Introduction
In my previous article I presented the details of a Divide and conquer algorithm that solves the convex hull problem, along with the challenges I encountered during the implementation. Here I will address these issues and present potential solutions and the results I had with them. Let’s jump right into it!
Problem #1
One of the issues I had was that the algorithm needed to perform a sorting of the points each time before applying the “two-finger” algorithm, since this needs to start from the right/left-most points of the left and right hull respectively.
Although this extra sorting doesn’t affect the overall complexity of the convex hull algorithm, it does add some constant factor to it and I wanted to make my code faster and see how much improvement would the avoiding of this extra step bring.
The solution is quite simple of course: as already suggested in the previous post, we can store the left- and rightmost points of the polygons we generate during the algorithm, so when we are merging two polygons, we don’t need to iterate through all the points again in order to determine them. And since we anyways need to iterate through all the points when finishing the merge phase (remember: after the “two-finger” algorithm determines the upper/lower bounds, we need to “walk” through in clockwise order through all the points to determine all the points that will form the resulting polygon), we can use this step to also determine which are the points on the left and right extremes. Let’s see how our code gets modified:
Previously, our Polygon type was really just an array of points, where we stored the points in clockwise order:
type Polygon struct {
points []Point
}
Now we need to add two fields which will store the index of the left and rightmost points. Note that we don’t need to actually store Point objects here - they are part of the points array, so storing the indexes will do it.
type Polygon struct {
points []Point
left_index int
right_index int
}
We can remove the sorting part from the beginning of the merge step, and slightly modify the part where we walk through the points. So this code:
result := []Point{left_poly[upper_left], right_poly[upper_right]}
//....
for upper_right != lower_right {
upper_right = (upper_right + 1) % len(right_poly)
result = append(result, right_poly[upper_right])
}
//...
will become this:
result := []Point{left_poly[upper_left], right_poly[upper_right]}
//...
rightmost := right_poly[upper_right]
rightmost_index := 1
for upper_right != lower_right {
upper_right = (upper_right + 1) % len(right_poly)
result = append(result, right_poly[upper_right])
if right_poly[upper_right].X > rightmost.X {
rightmost_index = len(result) - 1
rightmost = right_poly[upper_right]
}
}
//...
For simplicity these code snippets only show the right side of this walkthrough, but the left side is symmetrical, so you get the idea.
Results
In order to check how much this small change has improved the performance of the algorithm, I measured the execution time when running the algorithm on sets of points with different sizes, from small ones containing only 10-20 points to very large ones, containing as much as 800.000 points.
Before the improvements, the algorithm would process about 1.200 points in one millisecond. The changes improve this number to around 1.300 points / millisecond, which confirms what I was expecting - not a huge improvement, but getting rid of the extra sorting definitely has some results, in this case around 8%.
Problem #2
Let’s jump over now to the second issue I noticed, namely that having points with the same X-coordinate in the initial set of points can break the algorithm and the resulting polygon would be far from a convex one. Before showing a possible (although not perfect) solution, I will explain why the “two-finger” algorithm doesn’t work in some special cases.

Consider the example from the diagram above. After partitioning the points into the left and right sides, points A and B, having the same X coordinate, end up on different sides and in this case the “two-finger” algorithm will not determine the correct upper and lower tangents when trying to merge the two sides. The reason for this is that A and B are lying on the imaginary vertical line used to determine the best candidates for the tangents. It’s easy to imagine why the algorithm wouldn’t perform correctly: if we choose A from the right side and start walking through the points on the left side to see which is the best upper tangent, the intersection between these possible tangents and the vertical line will always be exactly in A. Therefore the algorithm will stop after just a few steps, seeing that there is no improvement, and most likely it will determine an incorrect tangent as result.
So what can we do in such cases? I spent quite a lot of time thinking about it, and here are a few ideas I had:
- Modify the partitioning algorithm We could partition the input points making sure that points with the same X-coordinate end up on the same side. This would solve the problem indeed, but it would slow down the algorithm, because we would need to search for a pair of consecutive points in the input array that have a horizontal distance between them. I didn’t want to decrease the performance of the algorithm, so I decided to think about a better approach.
- Don’t always use a vertical delimiter Instead of using a vertical imaginary line between the two sides, we could use a slightly sloped one when a problematic case appears, so that none of the points lie on the delimiter. Again, this sounds good, but we can’t guarantee that by slightly rotating the delimiter there will be no other points lying on it. And to actually make sure that no points lie on it, we would need to do an additional search again, which slows down the whole process.
- Introduce an imaginary gap between the two sides
The idea is to shift the right side just a tiny little bit to the right. This way we can draw the delimiter between the two planes and the algorithm will perform correctly. Well, not really, at least not all the time. Just look at the diagram below and imagine what would happen if we started increasing
dX. In its current state, the convex hull isAFEBCD. Increasing the gap somewhat would removeDfrom the hull, and increasing it even more would removeEas well at some point.

Obviously, this can occur when the gap is small, too. However I chose to go with this solution, because of two reasons. First, by adding a really small gap, the chances that the resulting polygon will be incorrect are very low. Second, even if this happens, the algorithm will determine a convex polygon, one that is very close to the correct one. This is still a big improvement compared to the first implementation, where in such cases the resulting polygon could be completely different than the expected result.
Wrap up
We managed to make some nice improvements on the initial implementation of this convex hull algorithm. As always, I’m happy to receive feedback about the article. Feel free to send me an email or suggest changes on Github.
In my next post I’m planning to discover another algorithm from the world of computational geometry. Stay tuned!