Struct sdl2::rect::Rect [] [src]

pub struct Rect { /* fields omitted */ }

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

Methods

impl Rect
[src]

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.

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.

The horizontal position of this rectangle.

The vertical position of this rectangle.

The width of this rectangle.

The height of this rectangle.

Returns the width and height of this rectangle.

Sets the horizontal position of this rectangle to the given value, clamped to be less than or equal to i32::max_value() / 2.

Sets the vertical position of this rectangle to the given value, clamped to be less than or equal to i32::max_value() / 2.

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.

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.

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

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

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

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

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

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

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

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

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

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.

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.

Centers the rectangle on the given point.

Move this rect and clamp the positions to prevent over/underflow. This also clamps the size to prevent overflow.

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

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

Deprecated since 0.30.0

: use contains_point instead

Checks whether this rect contains a given point, or touches it on the right and/or bottom edges. This method is deprecated in favor of Rect::contains_point.

For historical reasons, this method differs in behavior from SDL_PointInRect by including points along the bottom and right edges of the rectangle, so that a 1-by-1 rectangle actually covers an area of four points, not one.

Examples

use sdl2::rect::{Rect, Point};
let rect = Rect::new(1, 2, 3, 4);
assert!(rect.contains(Point::new(1, 2)));
assert!(!rect.contains(Point::new(0, 1)));
assert!(rect.contains(Point::new(3, 5)));
assert!(rect.contains(Point::new(4, 6)));  // N.B.
assert!(!rect.contains(Point::new(5, 7)));

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

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

Returns the underlying C 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.

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

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

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

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

Trait Implementations

impl Debug for Rect
[src]

Formats the value using the given formatter.

impl Clone for Rect
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Copy for Rect
[src]

impl PartialEq for Rect
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Eq for Rect
[src]

impl Hash for Rect
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl Deref for Rect
[src]

The resulting type after dereferencing

Example

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

impl DerefMut for Rect
[src]

Example

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

impl Into<SDL_Rect> for Rect
[src]

Performs the conversion.

impl Into<(i32, i32, u32, u32)> for Rect
[src]

Performs the conversion.

impl From<SDL_Rect> for Rect
[src]

Performs the conversion.

impl From<(i32, i32, u32, u32)> for Rect
[src]

Performs the conversion.

impl AsRef<SDL_Rect> for Rect
[src]

Performs the conversion.

impl AsMut<SDL_Rect> for Rect
[src]

Performs the conversion.

impl BitAnd<Rect> for Rect
[src]

The resulting type after applying the & operator

The method for the & operator

impl BitOr<Rect> for Rect
[src]

The resulting type after applying the | operator

The method for the | operator