Struct sdl2::rect::Rect

source ·
pub struct Rect { /* private fields */ }
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§

source§

impl Rect

source

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_value() (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.

Rects must always be non-empty, so a width and/or height argument of 0 will be replaced with 1.

source

pub fn from_center<P>(center: P, width: u32, height: u32) -> Rect
where P: Into<Point>,

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_value() (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.

Rects must always be non-empty, so a width and/or height argument of 0 will be replaced with 1.

source

pub fn x(&self) -> i32

The horizontal position of this rectangle.

source

pub fn y(&self) -> i32

The vertical position of this rectangle.

source

pub fn width(&self) -> u32

The width of this rectangle.

source

pub fn height(&self) -> u32

The height of this rectangle.

source

pub fn size(&self) -> (u32, u32)

Returns the width and height of this rectangle.

source

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_value() / 2.

source

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_value() / 2.

source

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_value() / 2.

Rects must always be non-empty, so a width argument of 0 will be replaced with 1.

source

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_value() / 2.

Rects must always be non-empty, so a height argument of 0 will be replaced with 1.

source

pub fn left(&self) -> i32

Returns the x-position of the left side of this rectangle.

source

pub fn right(&self) -> i32

Returns the x-position of the right side of this rectangle.

source

pub fn top(&self) -> i32

Returns the y-position of the top side of this rectangle.

source

pub fn bottom(&self) -> i32

Returns the y-position of the bottom side of this rectangle.

source

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(0, 0, 10, 10).left_shifted(5), Rect::new(-5, 0, 10, 10));
source

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(0, 0, 10, 10).right_shifted(5), Rect::new(5, 0, 10, 10));
source

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(0, 0, 10, 10).top_shifted(5), Rect::new(0, -5, 10, 10));
source

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(0, 0, 10, 10).bottom_shifted(5), Rect::new(0, 5, 10, 10));
source

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

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

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

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

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

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_value() / 2.

source

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_value() / 2.

source

pub fn center_on<P>(&mut self, point: P)
where P: Into<(i32, i32)>,

Centers the rectangle on the given point (in place).

source

pub fn centered_on<P>(self, point: P) -> Rect
where P: Into<(i32, i32)>,

Centers the rectangle on the given point.

source

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.

source

pub fn reposition<P>(&mut self, point: P)
where P: Into<(i32, i32)>,

Moves this rect to the given position after clamping the values.

source

pub fn resize(&mut self, width: u32, height: u32)

Resizes this rect to the given size after clamping the values.

source

pub fn contains_point<P>(&self, point: P) -> bool
where P: Into<(i32, i32)>,

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

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

pub fn raw(&self) -> *const SDL_Rect

Returns the underlying C Rect.

source

pub fn raw_mut(&mut self) -> *mut SDL_Rect

source

pub fn raw_slice(slice: &[Rect]) -> *const SDL_Rect

source

pub fn from_ll(raw: SDL_Rect) -> Rect

source

pub fn from_enclose_points<R>( points: &[Point], clipping_rect: R ) -> Option<Rect>
where R: Into<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.

source

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

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

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

pub fn intersect_line(&self, start: Point, end: Point) -> Option<(Point, Point)>

Calculates the intersection of a rectangle and a line segment and returns the points of their intersection.

Trait Implementations§

source§

impl AsMut<SDL_Rect> for Rect

source§

fn as_mut(&mut self) -> &mut SDL_Rect

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl AsRef<SDL_Rect> for Rect

source§

fn as_ref(&self) -> &SDL_Rect

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl BitAnd for Rect

§

type Output = Option<Rect>

The resulting type after applying the & operator.
source§

fn bitand(self, rhs: Rect) -> Option<Rect>

Performs the & operation. Read more
source§

impl BitOr for Rect

§

type Output = Rect

The resulting type after applying the | operator.
source§

fn bitor(self, rhs: Rect) -> Rect

Performs the | operation. Read more
source§

impl Clone for Rect

source§

fn clone(&self) -> Rect

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Rect

source§

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

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

impl DerefMut for Rect

source§

fn deref_mut(&mut self) -> &mut SDL_Rect

Example
use sdl2::rect::Rect;
let mut rect = Rect::new(2, 3, 4, 5);
rect.x = 60;
assert_eq!(60, rect.x);
source§

impl From<(i32, i32, u32, u32)> for Rect

source§

fn from((x, y, width, height): (i32, i32, u32, u32)) -> Rect

Converts to this type from the input type.
source§

impl From<SDL_Rect> for Rect

source§

fn from(raw: SDL_Rect) -> Rect

Converts to this type from the input type.
source§

impl Hash for Rect

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Into<(i32, i32, u32, u32)> for Rect

source§

fn into(self) -> (i32, i32, u32, u32)

Converts this type into the (usually inferred) input type.
source§

impl Into<SDL_Rect> for Rect

source§

fn into(self) -> SDL_Rect

Converts this type into the (usually inferred) input type.
source§

impl PartialEq for Rect

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Deref for Rect

source§

fn deref(&self) -> &SDL_Rect

Example
use sdl2::rect::Rect;
let rect = Rect::new(2, 3, 4, 5);
assert_eq!(2, rect.x);
§

type Target = SDL_Rect

The resulting type after dereferencing.
source§

impl Copy for Rect

source§

impl Eq for Rect

Auto Trait Implementations§

§

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> 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> 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> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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.