-
Notifications
You must be signed in to change notification settings - Fork 0
Foundations
Background on the Largest Inscribed Rectangle problem and the geometric principles underlying LIRiAP algorithms.
Given a simple polygon
- Axis-aligned: Rectangle sides parallel to coordinate axes
- Rotated: Rectangle may have any orientation
- With constraints: Maximum aspect ratio, minimum area, etc.
For axis-aligned rectangles, Daniels et al. (1997) proved that the optimal rectangle always has at least two sides determined by vertex coordinates of the polygon. This enables efficient exact solvers using vertex-coordinate grids.
The general (rotated) case is NP-hard to solve exactly. Approaches include:
- Heuristic search over orientation space
- Grid-based approximation
- Boundary-coordinate raster methods (BCRS)
The classic stack-based algorithm for finding the largest rectangle in a histogram. Runs in O(n) time.
Reference: Klingel, E. (1986). Finding the largest rectangle in a polygon. Algorithmica.
For convex polygons, the optimal axis-aligned rectangle has its top and bottom edges tangent at polygon vertices. This enables O(n²) exact solving via vertex-pair enumeration.
References:
- Alt, H., Hagerup, T., Mehlhorn, K., Preparata, F.P. (1987). Deterministic simulation of idealized parallel computers. Information and Computation.
- Amenta, N. (1994). A short proof of an interesting heuristic result. Proc. 5th ACM-SIAM SODA.
Core Theorem: The largest axis-aligned rectangle in a simple polygon always has at least two sides determined by vertex x- or y-coordinates. This is the foundation for the exact axis-aligned solvers.
Reference: Daniels, K., Milenkovic, V., Roth, D. (1997). Finding the largest axis-aligned rectangle in a polygon. Proc. 13th Canadian Conf. Computational Geometry.
Bounded scalar optimization for angle refinement. Used for local search around candidate orientations.
Reference: Brent, R.P. (1973). Algorithms for Finding Zeros and Extrema of Functions Without Calculating Derivatives. Prentice-Hall.
References:
- [1] Bentley, J.L. (1977). Programming Pearls: Fast Algorithms for Polygon Containment. Communications of the ACM, 20(11), 887-891. https://dl.acm.org/doi/10.1145/1283920.3283334
- [2] Preparata, F.P., & Shamos, M.I. (1985). Computational Geometry: An Introduction. Springer-Verlag.
The BCRS algorithm combines vertex-coordinate grid construction with a variable-pitch variant of the Largest Rectangle in Histogram algorithm to guarantee finding the global optimum for rotated rectangles.
Let
The resulting grid has dimensions
If
Define the occupancy mask
For each row
rather than uniform grid spacing. The depth is:
The area of each candidate rectangle is
Theorem (Daniels et al., 1997): The largest axis-aligned rectangle in a simple polygon has at least two sides aligned with vertex x- or y-coordinates.
Corollary: Searching the vertex-coordinate grid
After obtaining an initial axis-aligned rectangle seed, SDF-guided expansion tightens the rectangle against the polygon boundary by computing the exact distance to the boundary at each side.
Define the signed distance field
where
The SDF provides the exact distance to the polygon boundary, independent of vertex coordinates.
Consider expanding the left side of rectangle
Evaluate
-
If
$s < 0$ (midpoint is inside), the expansion is bounded by both the polygon edge and the bounding box:$$d_{\text{max}} = \min(x_0 - \text{minx}, |s|)$$ -
If
$s \geq 0$ (midpoint is outside), expansion is bounded only by the bounding box:$$d_{\text{max}} = x_0 - \text{minx}$$
This bound uses the actual distance to the boundary
To find the maximum expansion distance maintaining containment, perform binary search over
The binary search evaluates containment at each step using the polygon boundary representation (10 iterations provides sufficient precision).
Repeat this process for all four sides (left, right, bottom, top). Run three iterations of coordinate ascent to jointly optimize all sides simultaneously, as expanding one side may enable further expansion on others.
After expansion, verify strict containment using the SDF.
Compute the maximum SDF value over the rectangle interior:
Since the SDF is continuous and the rectangle is convex, the maximum occurs at one of the boundary points. We sample the four corners and the midpoints of each edge (eight samples total):
This is O(1) with respect to polygon size.
-
If
$\phi_{\text{max}} \leq \epsilon$ (where$\epsilon = 10^{-7}$ ), the rectangle is certified as strictly contained. -
Otherwise, apply symmetric shrinkage by
$\delta = \phi_{\text{max}} + 2\epsilon$ :$$R' = R \oplus (-\delta)$$ where
$\oplus$ denotes Minkowski subtraction (shrink by$\delta$ on all sides). -
If
$\delta > 0.2 \times \min(\text{width}, \text{height}) / 2$ , reject the rectangle (excessive shrinkage). -
Otherwise, re-verify
$\phi_{\text{max}} \leq \epsilon$ on$R'$ . If it passes, return the shrunk rectangle; otherwise, reject.
The shrink distance
Navigation
- Home | Installation | Algorithms | Approximation | Skeleton | BCRS | Axis-Aligned | Performance | Complexity | Parameters | Folder Layout | Usage