Skip to main content

rectangles_adjacent

Function rectangles_adjacent 

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

Check if two rectangles are adjacent (touching but not overlapping).

Two rectangles are adjacent if they share a border but have no overlapping area. This is used to determine if regions can be merged even when they don’t overlap.

§Algorithm

Checks for adjacency in both directions:

  1. Horizontal adjacency: Right edge of A touches left edge of B (or vice versa) AND they overlap on the Y-axis
  2. Vertical adjacency: Bottom edge of A touches top edge of B (or vice versa) AND they overlap on the X-axis

§Complexity

  • Time: O(1) - up to 8 comparisons
  • Space: O(1) - no additional memory

§Edge Cases

  • Handles zero-width or zero-height rectangles gracefully
  • Uses saturating arithmetic for overflow protection
  • Returns false for overlapping rectangles (use intersection test instead)

§Examples

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

// Horizontally adjacent
let a = Rect::new(0, 0, 10, 10);
let b = Rect::new(10, 0, 10, 10);
assert!(rectangles_adjacent(&a, &b));

// Vertically adjacent  
let c = Rect::new(0, 10, 10, 10);
assert!(rectangles_adjacent(&a, &c));

// Not adjacent (gap between)
let d = Rect::new(12, 0, 10, 10);
assert!(!rectangles_adjacent(&a, &d));