Skip to main content

Rect

Struct Rect 

Source
pub struct Rect {
    pub x: u16,
    pub y: u16,
    pub width: u16,
    pub height: u16,
}
Expand description

A rectangular region defined by position and dimensions.

Used for:

  • Tracking dirty regions that need redrawing
  • Clipping child elements to parent bounds
  • Hit testing for mouse events

§Coordinate System

(0,0) ─────────────▶ x
  │   ┌─────────┐
  │   │ (x,y)   │
  │   │ ┌─────┐ │
  │   │ │     │ │ height
  │   │ └─────┘ │
  │   └─────────┘
  ▼      width
  y

Fields§

§x: u16

X coordinate of the top-left corner

§y: u16

Y coordinate of the top-left corner

§width: u16

Width in terminal columns

§height: u16

Height in terminal rows

Implementations§

Source§

impl Rect

Source

pub fn new(x: u16, y: u16, width: u16, height: u16) -> Self

Creates a new rectangle with the given position and dimensions.

Source

pub fn empty() -> Self

Creates an empty rectangle at origin.

Source

pub fn right(&self) -> u16

Returns the right edge coordinate (exclusive).

Source

pub fn bottom(&self) -> u16

Returns the bottom edge coordinate (exclusive).

Source

pub fn contains_point(&self, x: u16, y: u16) -> bool

Checks if this rectangle contains the given point.

§Example
Rect { x: 10, y: 5, width: 20, height: 10 }
contains(15, 7) → true
contains(5, 7) → false
Source

pub fn is_empty(&self) -> bool

Checks if this rectangle is empty (zero width or height).

Source

pub fn intersection(&self, other: &Rect) -> Rect

Calculates the intersection of two rectangles.

Returns the overlapping region, or an empty rectangle if no overlap.

§Example
┌─────┐
│  A  │──┐
└─────┘  │ intersection
   │  ┌──▼──┐
   └──│     │
      │  B  │
      └─────┘
Source

pub fn intersects(&self, other: &Rect) -> bool

Checks if two rectangles intersect.

Source

pub fn union(&self, other: &Rect) -> Rect

Calculates the union of two rectangles.

Returns the smallest rectangle that contains both rectangles.

§Example
┌─────────────┐ union
│ ┌─────┐     │
│ │  A  │  B  │
│ └─────┘     │
└─────────────┘
Source

pub fn clip_to(&self, bounds: &Rect) -> Rect

Clips this rectangle to fit within the given bounds.

Returns the intersection of this rectangle with the bounds.

Source

pub fn expand(&self, amount: u16) -> Rect

Expands the rectangle by the given amount in all directions.

Useful for adding padding or margins.

Source

pub fn contract(&self, amount: u16) -> Rect

Contracts the rectangle by the given amount in all directions.

Useful for applying padding inward.

Trait Implementations§

Source§

impl Clone for Rect

Source§

fn clone(&self) -> Rect

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Rect

Source§

impl Debug for Rect

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Rect

Source§

impl PartialEq for Rect

Source§

fn eq(&self, other: &Rect) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Rect

Auto Trait Implementations§

§

impl Freeze for Rect

§

impl RefUnwindSafe for Rect

§

impl Send for Rect

§

impl Sync for Rect

§

impl Unpin for Rect

§

impl UnsafeUnpin for Rect

§

impl UnwindSafe for Rect

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Message for T
where T: Any + Clone + Send + 'static,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

fn clone_box(&self) -> Box<dyn Message>

Source§

impl<T> State for T
where T: Any + Clone + Send + 'static,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Source§

fn clone_box(&self) -> Box<dyn State>

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.