pub struct Rect {
pub x: u32,
pub y: u32,
pub width: u32,
pub height: u32,
}Expand description
An axis-aligned rectangle with u32 coordinates.
Uses u32 rather than u16 to avoid overflow bugs that affect other TUI
libraries on large terminals. All coordinates are in terminal columns and
rows, with (0, 0) at the top-left.
Note: Rect::right and Rect::bottom return exclusive bounds
(one past the last column/row), consistent with Rust range conventions.
Fields§
§x: u32Left edge column, inclusive.
y: u32Top edge row, inclusive.
width: u32Width in terminal columns.
height: u32Height in terminal rows.
Implementations§
Source§impl Rect
impl Rect
Sourcepub const fn new(x: u32, y: u32, width: u32, height: u32) -> Self
pub const fn new(x: u32, y: u32, width: u32, height: u32) -> Self
Create a new rectangle from position and size.
Sourcepub const fn right(&self) -> u32
pub const fn right(&self) -> u32
Exclusive right edge (x + width).
This is one column past the last column in the rectangle.
Sourcepub const fn bottom(&self) -> u32
pub const fn bottom(&self) -> u32
Exclusive bottom edge (y + height).
This is one row past the last row in the rectangle.
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true if the rectangle has zero area (width or height is zero).
Sourcepub fn centered(&self, inner_w: u32, inner_h: u32) -> Rect
pub fn centered(&self, inner_w: u32, inner_h: u32) -> Rect
Returns a smaller Rect centered within self.
If the inner dimensions exceed self’s dimensions, they are clamped to self’s size. The returned rectangle is positioned such that it is centered both horizontally and vertically within self.
§Example
use slt::Rect;
let outer = Rect::new(0, 0, 10, 10);
let inner = outer.centered(4, 4);
assert_eq!(inner, Rect::new(3, 3, 4, 4));Sourcepub fn union(&self, other: Rect) -> Rect
pub fn union(&self, other: Rect) -> Rect
Returns the smallest Rect containing both self and other.
The union encompasses all cells in both rectangles. If either rectangle is empty,
the result may have unexpected dimensions; use is_empty() to check.
§Example
use slt::Rect;
let r1 = Rect::new(0, 0, 5, 5);
let r2 = Rect::new(3, 3, 5, 5);
let union = r1.union(r2);
assert_eq!(union, Rect::new(0, 0, 8, 8));Sourcepub fn intersection(&self, other: Rect) -> Option<Rect>
pub fn intersection(&self, other: Rect) -> Option<Rect>
Returns the overlapping region between self and other, or None if they don’t overlap.
Two rectangles overlap if they share at least one cell. Adjacent rectangles (touching at an edge but not overlapping) return None.
§Example
use slt::Rect;
let r1 = Rect::new(0, 0, 5, 5);
let r2 = Rect::new(3, 3, 5, 5);
let overlap = r1.intersection(r2);
assert_eq!(overlap, Some(Rect::new(3, 3, 2, 2)));Sourcepub fn contains(&self, x: u32, y: u32) -> bool
pub fn contains(&self, x: u32, y: u32) -> bool
Returns true if the point (x, y) is inside the rectangle.
A point is considered inside if it is within the inclusive left/top bounds and exclusive right/bottom bounds (consistent with Rust range conventions).
§Example
use slt::Rect;
let r = Rect::new(5, 5, 10, 10);
assert!(r.contains(5, 5)); // top-left corner
assert!(r.contains(14, 14)); // inside
assert!(!r.contains(15, 15)); // outside (exclusive right/bottom)Sourcepub fn rows(&self) -> impl Iterator<Item = u32>
pub fn rows(&self) -> impl Iterator<Item = u32>
Returns an iterator over row y-coordinates in this rectangle.
Yields values from self.y to self.bottom() - 1 (inclusive).
§Example
use slt::Rect;
let r = Rect::new(0, 2, 5, 3);
let rows: Vec<u32> = r.rows().collect();
assert_eq!(rows, vec![2, 3, 4]);Sourcepub fn positions(&self) -> impl Iterator<Item = (u32, u32)>
pub fn positions(&self) -> impl Iterator<Item = (u32, u32)>
Returns an iterator over all (x, y) positions in this rectangle, row by row.
Iterates from top-left to bottom-right, filling each row left-to-right before
moving to the next row. Total count is width * height.
§Example
use slt::Rect;
let r = Rect::new(0, 0, 2, 2);
let positions: Vec<(u32, u32)> = r.positions().collect();
assert_eq!(positions, vec![(0, 0), (1, 0), (0, 1), (1, 1)]);