# Find the convex hull of a set of points in C# This example shows how to find the convex hull for a set of points. The details are fairly complicated so I’m not going to show them all here, but the basic ideas are relatively straightforward.

A convex hull is a smallest convex polygon that surrounds a set of points. If you imagine the points as pegs sticking up in a board, then you can think of a convex hull as the shape made by a rubber band wrapped around them all. Some of the points may lie inside the polygon. They are not part of the convex hull.

This program demonstrates several useful techniques for finding convex hulls.

First, the program culls some points out to make finding the convex hull faster. To do this, it finds the points that most closely represent the set’s upper left, upper right, lower left, and lower right corners. It finds the largest rectangle parallel to the X and Y axes that fits within the quadrilateral defined by those points. This rectangle is guaranteed to lie within the convex hull so any point that lies entirely within the rectangle cannot be on the convex hull. That lets the program remove those points from consideration quickly.

Similarly the program finds the quadrilateral defined by those four points and culls any points that lie inside it. It’s harder to determine whether a point lies inside a quadrilateral than inside a rectangle parallel to the X and Y axes, so the program performs the rectangle culling first.

After it has finished culling, the program uses an AngleValue method to order the points. Suppose dx and dy represent the difference between two points. The AngleValue method uses the numeric value dy / (dy + dx) to order the angle from one point to another with respect to the horizontal. This number is not the angle itself, but it has the property that AngleValue(x) < AngleValue(y) if Angle(x) < Angle(y).

The program starts with a point guaranteed to be on the convex hull. For example, the point with the smallest X coordinate or the smallest Y coordinate. Then it begins sweeping around to find the point with the smallest AngleValue. It adds that point to the hull and repeats the sweep from the new point starting with the sweep angle it used in the last test. This process continues until the program finds the start point again. (Basically this lets the program work its way around the convex hull, adding the next point along the polygon at each step.)

To use the program, click several times to select points. Each time you select a point, the program draws the points, highlights those that are culled, draws the culling rectangle and quadrilateral, and draws the convex hull.   This entry was posted in algorithms, geometry, graphics and tagged , , , , , , , , , , . Bookmark the permalink.

### 10 Responses to Find the convex hull of a set of points in C#

1. BLOG.CSHARPHELPER.COM says:

Find a minimal bounding circle of a set of points in C#

The example Find the convex hull of a set of points in C# finds the convex hull of a set of points. A convex hull is the smallest polygon that encloses the points. This example extends that result to find a minimal circle enclosing the points. The key is to note that a minimal bounding circle passes through two or three of the convex hull’s points. The following picture shows the two possible scenarios. In the pictures, blue shows the convex hull, red shows a culling trapezoid, and orange shows a culling rectangle, all as …

2. mm says:

can I ask what method you are exactly using? is it quickhull?

• RodStephens says:

It’s not quickhull. I don’t know if it has a name.

• peSHIr says:

Sounds like the Gift Wrapping algorithm to me (after the point culling).

3. solomon borodkin says:

I would appreciate any reference to programs in C#, C++, C for getting edge points of multi-dimensional convex hull of finite set of points. I see only planar and 3-dimential cases, but need general n-dimensial case (even not optimal algorithm – any one working).

• RodStephens says:

Sorry but I don’t have any examples like that. The best I could do is search the internet.

4. Eric Ouellet says:

Hi Rod,

I used your Smallest Enclosing Circle code in my article about Convex Hull (https://www.codeproject.com/Articles/1210225/Fast-and-improved-D-Convex-Hull-algorithm-and-its). I would like to add your Convex Hull code as a reference. But I would like to know if you authorize me to do so and what algorithm did you used (Graham Scan, Monotone Chain, …) ?

Thanks,
Eric

• RodStephens says:

Yes, feel free to refer to these examples.

This example uses a method somewhat similar to Graham scan but a bit slower and simpler. At each step, Graham scan pick a point to visit next and then later may undo that decision and replace it with some other point. The method used here examines all remaining points and picks the one that makes the next smallest angle. That means Graham scan has runtime O(n log n) but the method used here is O(N2).

It works well in practice if you don’t have too many points, and that’s true if the culling steps work well. For a random selection of points in an area, that should be true. It would have problems if the points lie near the edge of a circle so most of the points were in the convex hull.

• Eric Ouellet says:

Thanks. I already included it as a new algorithm in my benchmark. It is nice because it should help to see the difference in performance related to complexity. My code is actually broken because I’m in a middle of something. I will let you know when I can test and update my article. Just FYI, I’m now using Point from WPF (instead of Winform Point) and I modified Sytem.Drawing.Rectangle to use double instead of int. Also, I use double instead of float and convert List to IList parameters in order to be compatible with my code. I’m sure it will work great. Very big thanks for this code and the Smallest Enclosing Circle which is pretty much the only code I found.

This site uses Akismet to reduce spam. Learn how your comment data is processed.