Struct sfml::graphics::Rect

source ·
#[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§

Construct a rectangle from its coordinates.

Usage Example
let rect = Rect::new(10, 10, 10, 10);

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));

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);

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());

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);

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));

Get the size of the rectangle

Usage Example
let a = Rect::new(1, 2, 3, 4);
assert_eq!(a.size(), Vector2::new(3, 4));

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));

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));

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);

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.