Problem: Find the minimum area and perimeter enclosing rectangles of a planar set of points
To find the bounding rectangles that enclose a planar point set with minimal area or perimeter, we first compute the 2d convex hull of the point set in O(n·log(n)) time with n being the number of points in the set. Then, the bounding rectangles can be found in O(h) time with h being the number of vertices of the 2d convex hull.
Solution using G#
- public static void BoundingRectangleExample()
- {
- // Generate some points in a square
- PointSet ps = SamplePoints.RandomInRectangle(20, 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();
- // Compute the minimum area enclosing rectangle
- BoundingRectangle ma = ch.MinimumAreaEnclosingRectangle();
- // Compute the minimum perimeter enclosing rectangle
- BoundingRectangle mp = ch.MinimumPerimeterEnclosingRectangle();
- // visualize: ----------------------------------------------------
- // Make a scene
- // Set the point style
- sc.PointDisplayMode = 3;
- sc.PointSize = 0.003;
- // Make a layer for the 2d convex hull
- // Make a layer for the initial points
- // Make a layer for the minimum area enclosing rectangle
- // Make a layer for the minimum perimeter enclosing rectangle
- // Add the hull to the scene
- sc.Add(ch.Edges, eLayer);
- sc.Add(ch.InitialPoints, ipLayer);
- sc.Add(ma.GetEdges(), maLayer);
- sc.Add(mp.GetEdges(), mpLayer);
- // Output the scene as dxf
- sc.WriteDxf(@"c:\bounding_rectangles.dxf");
- }

