Skip to main content

minimum_bounding_rectangle

Function minimum_bounding_rectangle 

Source
pub fn minimum_bounding_rectangle(
    points: &ArrayView2<'_, f64>,
) -> SpatialResult<OrientedBoundingRect>
Expand description

Compute the minimum area bounding rectangle of a set of 2D points using the rotating calipers algorithm on the convex hull.

The minimum bounding rectangle is the rectangle with the smallest area that contains all the input points. One edge of this rectangle is always collinear with an edge of the convex hull.

§Algorithm

  1. Compute the convex hull of the points
  2. For each edge of the hull, compute the bounding rectangle aligned to that edge
  3. Return the rectangle with minimum area

Time complexity: O(n log n) for hull computation + O(h) for rotating calipers, where h is the number of hull vertices.

§Arguments

  • points - A 2D array of points (n x 2)

§Returns

  • SpatialResult<OrientedBoundingRect> - The minimum area bounding rectangle

§Examples

use scirs2_spatial::computational_geometry::bounding::minimum_bounding_rectangle;
use scirs2_core::ndarray::array;

// A rotated square
let points = array![
    [1.0, 0.0],
    [2.0, 1.0],
    [1.0, 2.0],
    [0.0, 1.0],
];

let mbr = minimum_bounding_rectangle(&points.view()).expect("Operation failed");
// Area should be 2.0 (side = sqrt(2), area = 2)
assert!((mbr.area() - 2.0).abs() < 0.1);