#[repr(C)]pub struct Rect<T> {
pub left: T,
pub top: T,
pub width: T,
pub height: T,
}
Expand description
Utility type for manipulating 2D axis-aligned rectangles.
Fields§
§left: T
Left coordinate of the rectangle.
top: T
Top coordinate of the rectangle.
width: T
Width of the rectangle.
height: T
Height of the rectangle.
Implementations§
source§impl<T> Rect<T>
impl<T> Rect<T>
sourcepub fn from_vecs(pos: Vector2<T>, size: Vector2<T>) -> Rect<T>
pub fn from_vecs(pos: Vector2<T>, size: Vector2<T>) -> Rect<T>
Construct a rectangle from its position and size.
Usage Example
let a = Vector2::new(10, 20);
let b = Vector2::new(30, 40);
let rect = Rect::from_vecs(a, b);
assert_eq!(rect, Rect::new(10, 20, 30, 40));
sourcepub fn into_other<U>(self) -> Rect<U>where
T: Into<U>,
pub fn into_other<U>(self) -> Rect<U>where
T: Into<U>,
Lossless conversion into Rect<U>
.
Usage Example
let a = Rect::new(1u8, 2u8, 3u8, 4u8);
let b: Rect<u32> = a.into_other();
assert_eq!(u8::try_from(b.top).unwrap(), a.top);
assert_eq!(u8::try_from(b.height).unwrap(), a.height);
assert_eq!(u8::try_from(b.left).unwrap(), a.left);
assert_eq!(u8::try_from(b.width).unwrap(), a.width);
sourcepub fn try_into_other<U>(self) -> Result<Rect<U>, T::Error>where
T: TryInto<U>,
pub fn try_into_other<U>(self) -> Result<Rect<U>, T::Error>where
T: TryInto<U>,
Fallible conversion into Rect<U>
Usage Example
let a = Rect::new(1i16, 2i16, 3i16, 4i16);
let b: Rect<u8> = a.try_into_other().unwrap();
assert_eq!(i16::from(b.top), a.top);
assert_eq!(i16::from(b.height), a.height);
assert_eq!(i16::from(b.left), a.left);
assert_eq!(i16::from(b.width), a.width);
let a = Rect::new(-1i16, -2i16, -3i16, -4i16);
let b = a.try_into_other::<u8>();
assert!(b.is_err());
sourcepub fn as_other<U: 'static + Copy>(self) -> Rect<U>where
T: AsPrimitive<U>,
pub fn as_other<U: 'static + Copy>(self) -> Rect<U>where
T: AsPrimitive<U>,
Lossy conversion into Rect<U>
Usage Example
let a = Rect::new(2., 32.32, 3.34, 1.443);
let b: Rect<u8> = a.as_other();
assert_eq!(b.top, 32);
assert_eq!(b.left, 2);
assert_eq!(b.width, 3);
assert_eq!(b.height, 1);
sourcepub fn position(self) -> Vector2<T>
pub fn position(self) -> Vector2<T>
Get the position of the rectangle’s top-left corner
Usage Example
let a = Rect::new(1, 2, 3, 4);
assert_eq!(a.position(), Vector2::new(1, 2));
sourcepub fn size(self) -> Vector2<T>
pub fn size(self) -> Vector2<T>
Get the size of the rectangle
Usage Example
let a = Rect::new(1, 2, 3, 4);
assert_eq!(a.size(), Vector2::new(3, 4));
source§impl<T: PartialOrd + Add<Output = T> + Sub<Output = T> + Copy> Rect<T>
impl<T: PartialOrd + Add<Output = T> + Sub<Output = T> + Copy> Rect<T>
sourcepub fn contains(self, point: Vector2<T>) -> bool
pub fn contains(self, point: Vector2<T>) -> bool
Check if a point is inside the rectangle’s area.
Usage Example
// Passing case
let a = Rect::new(0, 0, 4, 4);
let b = Vector2::new(2, 2);
assert!(a.contains(b));
// Failing case
let a = Rect::new(0, 0, 1, 1);
let b = Vector2::new(20, 10);
assert!(!a.contains(b));
sourcepub fn contains2(self, x: T, y: T) -> bool
pub fn contains2(self, x: T, y: T) -> bool
Check if a point is inside the rectangle’s area.
Usage Example
// Passing case
let a = Rect::new(0, 0, 4, 4);
assert!(a.contains2(2, 2));
// Failing case
let a = Rect::new(0, 0, 1, 1);
assert!(!a.contains2(20, 10));
sourcepub fn intersection(self, other: &Rect<T>) -> Option<Rect<T>>
pub fn intersection(self, other: &Rect<T>) -> Option<Rect<T>>
Returns the intersection between two rectangles, if any.
If the rectangles intersect, returns Some filled with the intersection of the two rectangles. Otherwise, returns None.
Usage Example
// Passing case
let a = Rect::new(0, 0, 2, 2);
let b = Rect::new(1, 1, 2, 2);
assert_eq!(a.intersection(&b), Some(Rect::new(1, 1, 1, 1)));
// Failing case
let a = Rect::new(0, 0, 2, 2);
let b = Rect::new(2, 2, 2, 2);
assert_eq!(a.intersection(&b), None);