Skip to main content

rectangle_union

Function rectangle_union 

Source
pub fn rectangle_union(a: &Rect, b: &Rect) -> Rect
Expand description

Rectangle union (bounding box) calculation.

Computes the smallest rectangle that contains both input rectangles. This is used in region merging to create the combined dirty area.

§Algorithm

  1. Find minimum X and Y coordinates (top-left corner)
  2. Find maximum X+width and Y+height coordinates (bottom-right corner)
  3. Compute width and height from corners

§Complexity

  • Time: O(1) - arithmetic operations only
  • Space: O(1) - no additional memory allocation

§Overflow Handling

Uses saturating arithmetic to handle coordinate overflow gracefully. Large coordinates are clamped to u16::MAX rather than wrapping.

§Examples

use hojicha_rendering::algorithms::rectangle_union;
use ratatui::layout::Rect;

let a = Rect::new(0, 0, 10, 10);
let b = Rect::new(5, 5, 10, 10);
let union = rectangle_union(&a, &b);

assert_eq!(union, Rect::new(0, 0, 15, 15));