find smallest area that contains all the rectangles

谁都会走 提交于 2019-11-29 04:03:14

There is no absolute solution to this problem, but there are several approximate solutions, you can read about some of them here.

Optimal Rectangle Packing on Non-Square Benchmarks:

Given a set of rectangles, our problem is to find all enclosing rectangles of minimum area that will contain them without overlap. We refer to an enclosing rectangle as a bounding box. The optimization problem is NP-hard, while the problem of deciding whether a set of rectangles can be packed in a given bounding box is NP-complete, via a reduction from bin-packing (Korf 2003).

New Improvements in Optimal Rectangle Packing:

Korf [2003] divided the rectangle packing problem into two subproblems: the minimal bounding box problem and the containment problem. The former finds a bounding box of least area that can contain a given set of rectangles, while the latter tries to pack the given rectangles in a given bounding box. The algorithm that solves the minimal bounding box problem calls the algorithm that solves the containment problem as a subroutine.

Minimal Bounding Box Problem

A simple way to solve the minimal bounding box problem is to find the minimum and maximum areas that describe the set of feasible and potentially optimal bounding boxes. Bounding boxes of all dimensions can be generated with areas within this range, and then tested in non-decreasing order of area until all feasible solutions of smallest area are found. The minimum area is the sum of the areas of the given rectangles. The maximum area is determined by the bounding box of a greedy solution found by setting the bounding box height to that of the tallest rectangle, and then placing the rectangles in the first available position when scanning from left to right, and for each column scanning from bottom to top.

See also Optimal Rectangle Packing: New Results.

First of all you should check, could be enclosing rectangle be rotated or no? Anyway, you could ignore "rectangles" condition and resolve task in points. You have array of points (which are vertexes of rectangles). Your task is to find encosing rectangle with minimum area. If enclosing rectangle could not be rotated then solution is silly and has complexity O(n).

Generated array of rectangles and make array of points, which are vertexes of rectangles. Next is simple:

long n; // Number of vertexes
point arr[SIZE]; //Array of vertexes
long minX = MAXNUMBER, minY = MAXNUMBER, maxX = -MAXNUMER, maxY = -MAXNUMBER;
for (long i = 0; i < 4 * n; i++)
{
    minX = MIN(minX, arr[i].x);
    minY = MIN(minY, arr[i].y);
    maxX = MIN(maxX, arr[i].x);
    maxY = MIN(maxY, arr[i].y);
}
long width = maxX - minX, height = maxY - minY;
printf("%ddX%ld", width, height);

Another task if rectangle could be rotated. Then you should first:

  1. Build minimum convex polygon of all the points in rectangle. You can use any of existing algorythms. Complexity O(n log n). As exmaple "Graham's Scan" : http://en.wikipedia.org/wiki/Graham%27s_scan
  2. Use simple algorithm for convex polygon. Link: http://cgm.cs.mcgill.ca/~orm/maer.html

Link for your task in wiki: http://en.wikipedia.org/wiki/Minimum_bounding_rectangle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!