Convex Polygon minimum area external rectangle
Returns a convex polygon.PThe minimum area can be mountedPWhat is the rectangle of (as far as the periphery is concerned? Technically speaking, given a direction, we can calculatePAnd create an external rectangle. But do we need to test each case to obtain each rectangle to calculate the minimum area? Thank God, we don't have to do that.
For PolygonPAn external rectangle of is located with an edge of the original polygon.
The above conclusions effectively limit the possible range of rectangles. We not only do not need to detect all possible directions, but also only need to detect the number of rectangles equal to the number of polygon edges.
As shown in the preceding figure, the four tangent lines (red) overlap with one side of the polygon and determine the external rectangle (blue ).
A simple algorithm is to calculate each edge in sequence as an edge that overlaps with a rectangle. However, this method of constructing a rectangle involves the calculation of each edge endpoint of a polygon.O (N)Time (becauseNSide. The whole algorithm will have a second time complexity.
A more efficient algorithm has been found. Using the rotating jamming case, we can update the data in real time within the constant time, instead of re-calculating the endpoint.
In fact, considering a convex polygon, there are two pairs andXAndYTangent tangent to the last four endpoints. The four lines have determined the external rectangle of a polygon. However, unless a polygon has a horizontal or vertical edge, the area of the rectangle cannot be included in the minimum area.
However, you can rotate the line until the condition is met. This process is the core of subordinate algorithms. Assume that a convex polygon is input clockwise.NVertex.
- Calculate the endpoints of all four polygon.Xminp,Xmaxp,Yminp,Ymaxp.
- Constructed by four pointsP. They identified two "shells.
- If one or two lines overlap with one side, the area of the rectangle determined by the four lines is calculated and saved as the current minimum value. Otherwise, the current minimum value is defined as Infinity.
- Rotate the line clockwise until one of the lines overlaps with one of the sides of the polygon.
- Calculate the area of the new rectangle and compare it with the current minimum value. If it is smaller than the current minimum value, it is updated and information about the rectangle that determines the minimum value is saved.
- Repeat steps 4 and 5 until the angle of line rotation is greater than 90 degrees.
- The minimum area of the output external rectangle.
Because the two pairs of "shells" determine an external rectangle, this algorithm takes into account all the rectangles that may calculate the minimum area. In addition to the initial values, the main cycle of the algorithm only needs to execute the total number of vertices multiple times. Therefore, the algorithm is linear in time complexity.
A similar but little-known problem is the problem of the smallest perimeter external rectangle. Interestingly, these two problems are completely different, because there are (although very few) polygon with no overlap between the smallest area external rectangle and the smallest perimeter external rectangle polygon.
Http://cgm.cs.mcgill.ca /~ Orm/maer.html
Please indicate the source for reprinting. Thank you!