to_axis_aligned_bounding_box

Function to_axis_aligned_bounding_box 

Source
pub fn to_axis_aligned_bounding_box<T>(vertices: &[Point<T>; 4]) -> Rect
where T: Copy + PartialOrd + Num + ToPrimitive,
Expand description

Calculates the axis-aligned bounding box of a rotated rectangle’s vertices.

This function is designed to work with the output of imageproc::geometry::min_area_rect, which is an array of four Point<T>. It iterates through the points to find the minimum and maximum x and y coordinates, then constructs an image::math::Rect that encloses all four points.

This version is generic over numeric types that implement PartialOrd, making it suitable for both integer and floating-point coordinates.

§Arguments

  • vertices - An array of 4 Point<T> representing the corners of a rectangle. T must be a numeric type that supports partial ordering and arithmetic operations.

§Returns

An image::math::Rect representing the smallest possible axis-aligned rectangle that contains all the input vertices.

§Panics

This function assumes the input array is not empty, which is guaranteed by its type &[Point<T>; 4].

§Examples

use imageproc::point::Point;
use image_debug_utils::rect::to_axis_aligned_bounding_box;

let rotated_rect_vertices = [
    Point { x: 50.0, y: 10.0 },
    Point { x: 90.0, y: 50.0 },
    Point { x: 50.0, y: 90.0 },
    Point { x: 10.0, y: 50.0 },
];

let bounding_box = to_axis_aligned_bounding_box(&rotated_rect_vertices);

assert_eq!(bounding_box.x, 10);
assert_eq!(bounding_box.y, 10);
assert_eq!(bounding_box.width, 80);
assert_eq!(bounding_box.height, 80);