# What Is The Pseudocode To Compute 2D Convex Hulls?

Decent Essays

This is the pseudocode for the algorithm I implemented in my program to compute 2D convex hulls. I chose this incremental algorithm, which adds the points one by one and updates the solution after each point added. We must ensure that every turn (walking from vertex to vertex is a turn) is a right turn, so we take the last 3 points at the current point of the solution and determine if they make a right turn. Using the last 3 points p0, p1, and p2, we can determine if they make a right or left turn by calculating the cross product of the two vectors p0-p1 and p2-p1. Since it’s a 2D x-y plane, the z coordinate of the cross product is the only nonzero value. If this component is positive, the second vector is to the left of the first, and if …show more content…

In this case (1, 1) and (3,3) are part of the convex hull, but not (2,2) since it’s along the same line and is in the middle. In this case, the values (1,1), (2,1), and (3,1) all have the same x-coordinate which can cause problems in the algorithm if not handled correctly. A general way to order the convex hull points is simply by x-coordinate, so if two points shared that x-coordinate, the ordering system is defined poorly. This was fixed by ordering the points in a lexicographical way, such that if two points have the same x-coordinate, they will be further sorted by the y coordinate. I’ve done this type of sorting in my code. This is an example with 20 points with the x and y coordinates ranging from 0-20. As you can see from the graph, the convex hull is correctly computed and the output is all the vertices of the hull in a clockwise ordering. This is another regular case example with 10 points, with the x and y coordinates ranging from 0-10. As you can see from the graph to the right, the convex hull is correctly computed with exactly 5 vertices, just like the algorithm’s output with the points listed in a clockwise fashion. I’ve tested my algorithm with various numbers of points (input) to analyze the performance. For inputs with less than 100,000 points, the algorithm only takes about a second to complete. The algorithm starts taking 30 seconds around an input of 1,000,000 points. For