pub struct Rect {
pub left: f32,
pub top: f32,
pub width: f32,
pub height: f32,
}
Expand description
Rectangle area within a grid of rectangular pixels. It consists of the four
values: top, left, width, and height. Both axes of the grid are aligned like
the coordinate system of a web page: x
runs from left to right, y
runs
from top to bottom.
§Examples
let rect = Rect::new(1.0, 2.0, 4.0, 3.0);
assert_eq!(rect.left, 1.0);
assert_eq!(rect.top, 2.0);
assert_eq!(rect.right(), 5.0);
assert_eq!(rect.bottom(), 5.0);
Fields§
§left: f32
§top: f32
§width: f32
§height: f32
Implementations§
source§impl Rect
impl Rect
sourcepub fn new(left: f32, top: f32, width: f32, height: f32) -> Self
pub fn new(left: f32, top: f32, width: f32, height: f32) -> Self
Creates a new rectangle from the left
, top
, width
and height
values. Values for width
or height
are allowed to be negative, the
rectangle will be normalized in that case.
§Examples
let rect = Rect::new(1.0, 2.0, 3.0, -4.0);
assert_eq!(rect.left, 1.0);
assert_eq!(rect.top, -2.0);
assert_eq!(rect.right(), 4.0);
assert_eq!(rect.bottom(), 2.0);
sourcepub fn center(&self) -> Point
pub fn center(&self) -> Point
Center point of the rectangle.
§Examples
let rect = Rect::new(0.0, 1.0, 3.0, 4.0);
assert_eq!(rect.center(), Point(1.5, 3.0));
sourcepub fn top_left(&self) -> Point
pub fn top_left(&self) -> Point
Top-left corner of the rectangle.
§Examples
let rect = Rect::new(0.0, 1.0, 3.0, 4.0);
assert_eq!(rect.top_left(), Point(0.0, 1.0));
assert_eq!(rect.left, 0.0);
assert_eq!(rect.top, 1.0);
sourcepub fn bottom_right(&self) -> Point
pub fn bottom_right(&self) -> Point
Bottom-right corner of the rectangle.
§Examples
let rect = Rect::new(0.0, 1.0, 3.0, 4.0);
assert_eq!(rect.bottom_right(), Point(3.0, 5.0));
assert_eq!(rect.right(), 3.0);
assert_eq!(rect.bottom(), 5.0);
sourcepub fn union(&self, other: &Rect) -> Rect
pub fn union(&self, other: &Rect) -> Rect
Calculates the union of two rectangles, the smallest rectangle that fully contains both source rectangles.
§Examples
let a = Rect::new(1.0, 2.0, 3.0, 4.0);
let b = Rect::new(-3.0, -4.0, 2.0, 1.0);
let rect = a.union(&b);
assert_eq!(rect.left, -3.0);
assert_eq!(rect.top, -4.0);
assert_eq!(rect.width, 7.0);
assert_eq!(rect.height, 10.0);
sourcepub fn with_padding(&self, value: f32) -> Self
pub fn with_padding(&self, value: f32) -> Self
Creates a new rectangle from the existing one by adding a padding border on all four sides.
§Examples
let a = Rect::new(1.0, 2.0, 3.0, 4.0);
let rect = a.with_padding(0.5);
assert_eq!(rect.left, 0.5);
assert_eq!(rect.top, 1.5);
assert_eq!(rect.width, 4.0);
assert_eq!(rect.height, 5.0);
Trait Implementations§
source§impl PartialEq for Rect
impl PartialEq for Rect
impl Copy for Rect
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 UnwindSafe for Rect
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more