Problem: Find the 2d convex hull of a planar set of points
A 2d convex hull encloses a planar point set with minimal perimeter. The following example generates random points in a rectangle and computes the 2d convex hull of these points.
The imput points may be 2d or 3d. However, the z-coordinate is ignored when computing the 2d convex hull.
Solution using G#
- public static void ConvexHullExample()
- {
- // Generate some points in a square
- PointSet ps = SamplePoints.RandomInRectangle(1000, 1, 1);
- // Instantiate a 2d convex hull
- // Assign the point set
- ch.InitialPoints = ps;
- // Compute the 2d convex hull in O(n*log(n)) time
- ch.ComputeHull();
- // Get the edges forming the hull. The edges
- // are oriented clockwise
- List<Edge> edges = ch.Edges;
- // visualize: ----------------------------------------------------
- // Make a scene
- // Set the point style
- sc.PointDisplayMode = 3;
- sc.PointSize = 0.001;
- // Make a layer for the edges of the hull
- // Make a layer for the initial points
- // Make a layer for the vertices on the hull
- // Add the hull to the scene
- sc.Add(edges, eLayer);
- sc.Add(ch.InitialPoints, ipLayer);
- sc.Add(ch.Vertices, hpLayer);
- // Output the scene as dxf
- sc.WriteDxf(@"c:\hull.dxf");
- }

