Problem: Test if a 2d point set fits into a 2d container
In contrast to to the 3d container loading problem, the 2d container loading problem can be solved exactly in O(n·log(n)) time. To test if a 2d point set fits into a 2d container, 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 test if the convex hull fits into a given bounding rectangle can be done in O(h) time with h being the number of vertices of the 2d convex hull.
Solution using G#
- public static void ContainerLoadingExample()
- {
- // A container with dimension 3 x 1. Center point and
- // rotation angle are arbitrary
- // Generate some points in a rectangle. This is the item to load
- PointSet ps = SamplePoints.RandomInRectangle(100, 3, 1);
- // Instantiate a 2d convex hull
- // Assign the item to load
- ch.InitialPoints = ps;
- // Compute the 2d convex hull of the item to load
- ch.ComputeHull();
- // Test if the item fits into the container
- BoundingRectangle br = ch.FitsInto(container);
- if ( br == null )
- {
- Console.WriteLine("Item does not fit into container!");
- return;
- }
- // Since it fits into the container, we can visualize the
- // bounding box
- // 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 container
- // Make a layer for the bounding rectangle
- // Transform the container so that we can actually see that
- // the item fits into the container
- container.Center = br.Center;
- container.RotationAngle = br.RotationAngle;
- // Add the hull to the scene
- sc.Add(ch.Edges, iLayer);
- sc.Add(ch.InitialPoints, pLayer);
- sc.Add(container.GetEdges(), cLayer);
- sc.Add(br.GetEdges(), bLayer);
- // Output the scene as dxf
- sc.WriteDxf(@"c:\container_loading_2d.dxf");
- }

