Struct Rect
pub struct Rect { /* private fields */ }
dep_sdl2
only.Expand description
A (non-empty) rectangle.
The width and height of a Rect
must always be strictly positive (never
zero). In cases where empty rects may need to represented, it is
recommended to use Option<Rect>
, with None
representing an empty
rectangle (see, for example, the output of the
intersection
method).
Implementations§
§impl Rect
impl Rect
pub fn new(x: i32, y: i32, width: u32, height: u32) -> Rect
pub fn new(x: i32, y: i32, width: u32, height: u32) -> Rect
Creates a new rectangle from the given values.
The width and height are clamped to ensure that the right and bottom sides of the rectangle does not exceed i32::MAX (the value 2147483647, the maximal positive size of an i32). This means that the rect size will behave oddly if you move it very far to the right or downwards on the screen.
Rect
s must always be non-empty, so a width
and/or height
argument
of 0 will be replaced with 1.
pub fn from_center<P>(center: P, width: u32, height: u32) -> Rect
pub fn from_center<P>(center: P, width: u32, height: u32) -> Rect
Creates a new rectangle centered on the given position.
The width and height are clamped to ensure that the right and bottom sides of the rectangle does not exceed i32::MAX (the value 2147483647, the maximal positive size of an i32). This means that the rect size will behave oddly if you move it very far to the right or downwards on the screen.
Rect
s must always be non-empty, so a width
and/or height
argument
of 0 will be replaced with 1.
pub fn set_x(&mut self, x: i32)
pub fn set_x(&mut self, x: i32)
Sets the horizontal position of this rectangle to the given value, clamped to be less than or equal to i32::MAX / 2.
pub fn set_y(&mut self, y: i32)
pub fn set_y(&mut self, y: i32)
Sets the vertical position of this rectangle to the given value, clamped to be less than or equal to i32::MAX / 2.
pub fn set_width(&mut self, width: u32)
pub fn set_width(&mut self, width: u32)
Sets the width of this rectangle to the given value, clamped to be less than or equal to i32::MAX / 2.
Rect
s must always be non-empty, so a width
argument of 0 will be
replaced with 1.
pub fn set_height(&mut self, height: u32)
pub fn set_height(&mut self, height: u32)
Sets the height of this rectangle to the given value, clamped to be less than or equal to i32::MAX / 2.
Rect
s must always be non-empty, so a height
argument of 0 will be
replaced with 1.
pub fn left_shifted(self, offset: i32) -> Rect
pub fn left_shifted(self, offset: i32) -> Rect
Shifts this rectangle to the left by offset
.
§Example
use sdl2::rect::Rect;
assert_eq!(Rect::new(5, 5, 10, 10).left_shifted(5), Rect::new(0, 5, 10, 10));
pub fn right_shifted(self, offset: i32) -> Rect
pub fn right_shifted(self, offset: i32) -> Rect
Shifts this rectangle to the right by offset
.
§Example
use sdl2::rect::Rect;
assert_eq!(Rect::new(5, 5, 10, 10).right_shifted(5), Rect::new(10, 5, 10, 10));
pub fn top_shifted(self, offset: i32) -> Rect
pub fn top_shifted(self, offset: i32) -> Rect
Shifts this rectangle to the top by offset
.
§Example
use sdl2::rect::Rect;
assert_eq!(Rect::new(5, 5, 10, 10).top_shifted(5), Rect::new(5, 0, 10, 10));
pub fn bottom_shifted(self, offset: i32) -> Rect
pub fn bottom_shifted(self, offset: i32) -> Rect
Shifts this rectangle to the bottom by offset
.
§Example
use sdl2::rect::Rect;
assert_eq!(Rect::new(5, 5, 10, 10).bottom_shifted(5), Rect::new(5, 10, 10, 10));
pub fn center(&self) -> Point
pub fn center(&self) -> Point
Returns the center position of this rectangle.
Note that if the width or height is not a multiple of two, the center will be rounded down.
§Example
use sdl2::rect::{Rect,Point};
let rect = Rect::new(1,0,2,3);
assert_eq!(Point::new(2,1),rect.center());
pub fn top_left(&self) -> Point
pub fn top_left(&self) -> Point
Returns the top-left corner of this rectangle.
§Example
use sdl2::rect::{Rect, Point};
let rect = Rect::new(1, 0, 2, 3);
assert_eq!(Point::new(1, 0), rect.top_left());
pub fn top_right(&self) -> Point
pub fn top_right(&self) -> Point
Returns the top-right corner of this rectangle.
§Example
use sdl2::rect::{Rect, Point};
let rect = Rect::new(1, 0, 2, 3);
assert_eq!(Point::new(3, 0), rect.top_right());
pub fn bottom_left(&self) -> Point
pub fn bottom_left(&self) -> Point
Returns the bottom-left corner of this rectangle.
§Example
use sdl2::rect::{Rect, Point};
let rect = Rect::new(1, 0, 2, 3);
assert_eq!(Point::new(1, 3), rect.bottom_left());
pub fn bottom_right(&self) -> Point
pub fn bottom_right(&self) -> Point
Returns the bottom-right corner of this rectangle.
§Example
use sdl2::rect::{Rect, Point};
let rect = Rect::new(1, 0, 2, 3);
assert_eq!(Point::new(3, 3), rect.bottom_right());
pub fn set_right(&mut self, right: i32)
pub fn set_right(&mut self, right: i32)
Sets the position of the right side of this rectangle to the given value, clamped to be less than or equal to i32::MAX / 2.
pub fn set_bottom(&mut self, bottom: i32)
pub fn set_bottom(&mut self, bottom: i32)
Sets the position of the bottom side of this rectangle to the given value, clamped to be less than or equal to i32::MAX / 2.
pub fn center_on<P>(&mut self, point: P)
pub fn center_on<P>(&mut self, point: P)
Centers the rectangle on the given point (in place).
pub fn centered_on<P>(self, point: P) -> Rect
pub fn centered_on<P>(self, point: P) -> Rect
Centers the rectangle on the given point.
pub fn offset(&mut self, x: i32, y: i32)
pub fn offset(&mut self, x: i32, y: i32)
Move this rect and clamp the positions to prevent over/underflow. This also clamps the size to prevent overflow.
pub fn reposition<P>(&mut self, point: P)
pub fn reposition<P>(&mut self, point: P)
Moves this rect to the given position after clamping the values.
pub fn resize(&mut self, width: u32, height: u32)
pub fn resize(&mut self, width: u32, height: u32)
Resizes this rect to the given size after clamping the values.
pub fn contains_point<P>(&self, point: P) -> bool
pub fn contains_point<P>(&self, point: P) -> bool
Checks whether this rectangle contains a given point.
Points along the right and bottom edges are not considered to be inside
the rectangle; this way, a 1-by-1 rectangle contains only a single
point. Another way to look at it is that this method returns true if
and only if the given point would be painted by a call to
Renderer::fill_rect
.
§Examples
use sdl2::rect::{Rect, Point};
let rect = Rect::new(1, 2, 3, 4);
assert!(rect.contains_point(Point::new(1, 2)));
assert!(!rect.contains_point(Point::new(0, 1)));
assert!(rect.contains_point(Point::new(3, 5)));
assert!(!rect.contains_point(Point::new(4, 6)));
pub fn contains_rect(&self, other: Rect) -> bool
pub fn contains_rect(&self, other: Rect) -> bool
Checks whether this rectangle completely contains another rectangle.
This method returns true if and only if every point contained by
other
is also contained by self
; in other words, if the
intersection of self
and other
is equal to other
.
§Examples
use sdl2::rect::Rect;
let rect = Rect::new(1, 2, 3, 4);
assert!(rect.contains_rect(rect));
assert!(rect.contains_rect(Rect::new(3, 3, 1, 1)));
assert!(!rect.contains_rect(Rect::new(2, 1, 1, 1)));
assert!(!rect.contains_rect(Rect::new(3, 3, 2, 1)));
pub fn raw_mut(&mut self) -> *mut SDL_Rect
pub fn raw_slice(slice: &[Rect]) -> *const SDL_Rect
pub fn from_ll(raw: SDL_Rect) -> Rect
pub fn from_enclose_points<R>(
points: &[Point],
clipping_rect: R,
) -> Option<Rect> ⓘ
pub fn from_enclose_points<R>( points: &[Point], clipping_rect: R, ) -> Option<Rect> ⓘ
Calculate a minimal rectangle enclosing a set of points. If a clipping rectangle is given, only points that are within it will be considered.
pub fn has_intersection(&self, other: Rect) -> bool
pub fn has_intersection(&self, other: Rect) -> bool
Determines whether two rectangles intersect.
Rectangles that share an edge but don’t actually overlap are not considered to intersect.
§Examples
use sdl2::rect::Rect;
let rect = Rect::new(0, 0, 5, 5);
assert!(rect.has_intersection(rect));
assert!(rect.has_intersection(Rect::new(2, 2, 5, 5)));
assert!(!rect.has_intersection(Rect::new(5, 0, 5, 5)));
pub fn intersection(&self, other: Rect) -> Option<Rect> ⓘ
pub fn intersection(&self, other: Rect) -> Option<Rect> ⓘ
Calculates the intersection of two rectangles.
Returns None
if the two rectangles don’t intersect. Rectangles that
share an edge but don’t actually overlap are not considered to
intersect.
The bitwise AND operator &
can also be used.
§Examples
use sdl2::rect::Rect;
let rect = Rect::new(0, 0, 5, 5);
assert_eq!(rect.intersection(rect), Some(rect));
assert_eq!(rect.intersection(Rect::new(2, 2, 5, 5)),
Some(Rect::new(2, 2, 3, 3)));
assert_eq!(rect.intersection(Rect::new(5, 0, 5, 5)), None);
pub fn union(&self, other: Rect) -> Rect
pub fn union(&self, other: Rect) -> Rect
Calculates the union of two rectangles (i.e. the smallest rectangle that contains both).
The bitwise OR operator |
can also be used.
§Examples
use sdl2::rect::Rect;
let rect = Rect::new(0, 0, 5, 5);
assert_eq!(rect.union(rect), rect);
assert_eq!(rect.union(Rect::new(2, 2, 5, 5)), Rect::new(0, 0, 7, 7));
assert_eq!(rect.union(Rect::new(5, 0, 5, 5)), Rect::new(0, 0, 10, 5));
Trait Implementations§
impl Copy for Rect
impl Eq 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
Source§impl<T> ByteSized for T
impl<T> ByteSized for T
Source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Source§fn byte_align(&self) -> usize
fn byte_align(&self) -> usize
Source§fn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Source§impl<T, R> Chain<R> for Twhere
T: ?Sized,
impl<T, R> Chain<R> for Twhere
T: ?Sized,
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> ExtAny for T
impl<T> ExtAny for T
Source§fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
TypeId
of Self
using a custom hasher.Source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Source§impl<T> ExtMem for Twhere
T: ?Sized,
impl<T> ExtMem for Twhere
T: ?Sized,
Source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Source§fn mem_align_of<T>() -> usize
fn mem_align_of<T>() -> usize
Source§fn mem_align_of_val(&self) -> usize
fn mem_align_of_val(&self) -> usize
Source§fn mem_size_of<T>() -> usize
fn mem_size_of<T>() -> usize
Source§fn mem_size_of_val(&self) -> usize
fn mem_size_of_val(&self) -> usize
Source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true
if dropping values of this type matters. Read moreSource§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self
without running its destructor. Read moreSource§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Source§unsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§fn mem_as_bytes(&self) -> &[u8] ⓘ
fn mem_as_bytes(&self) -> &[u8] ⓘ
unsafe_slice
only.§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Hook for T
impl<T> Hook for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more