pub struct FRect { /* private fields */ }
Expand description
A (non-empty) rectangle with float precision.
The width and height of a FRect
must always be strictly positive (never
zero). In cases where empty rects may need to be represented, it is
recommended to use Option<FRect>
, with None
representing an empty
rectangle (see, for example, the output of the
intersection
method).
Implementations§
Source§impl FRect
impl FRect
Sourcepub fn new(x: f32, y: f32, width: f32, height: f32) -> FRect
pub fn new(x: f32, y: f32, width: f32, height: f32) -> FRect
Creates a new rectangle with float precision from the given values.
FRect
s must always be non-empty, so a width
and/or height
argument
of 0 or less will be replaced with 1.
Sourcepub fn from_center<P>(center: P, width: f32, height: f32) -> FRect
pub fn from_center<P>(center: P, width: f32, height: f32) -> FRect
Creates a new rectangle with float precision centered on the given position.
FRect
s must always be non-empty, so a width
and/or height
argument
of 0 or less will be replaced with 1.
Sourcepub fn set_x(&mut self, x: f32)
pub fn set_x(&mut self, x: f32)
Sets the horizontal position of this rectangle to the given value, clamped to be less than or equal to i32::max_value() / 2.
Sourcepub fn set_y(&mut self, y: f32)
pub fn set_y(&mut self, y: f32)
Sets the vertical position of this rectangle to the given value, clamped to be less than or equal to i32::max_value() / 2.
Sourcepub fn set_width(&mut self, width: f32)
pub fn set_width(&mut self, width: f32)
Sets the width of this rectangle to the given value, clamped to be less than or equal to i32::max_value() / 2.
FRect
s must always be non-empty, so a width
argument of 0 will be
replaced with 1.
Sourcepub fn set_height(&mut self, height: f32)
pub fn set_height(&mut self, height: f32)
Sets the height of this rectangle to the given value, clamped to be less than or equal to i32::max_value() / 2.
FRect
s must always be non-empty, so a height
argument of 0 will be
replaced with 1.
Sourcepub fn left_shifted(self, offset: f32) -> FRect
pub fn left_shifted(self, offset: f32) -> FRect
Shifts this rectangle to the left by offset
.
§Example
use sdl2::rect::FRect;
assert_eq!(FRect::new(0.0, 0.0, 10.0, 10.0).left_shifted(5.0), FRect::new(-5.0, 0.0, 10.0, 10.0));
Sourcepub fn right_shifted(self, offset: f32) -> FRect
pub fn right_shifted(self, offset: f32) -> FRect
Shifts this rectangle to the right by offset
.
§Example
use sdl2::rect::FRect;
assert_eq!(FRect::new(0.0, 0.0, 10.0, 10.0).right_shifted(5.0), FRect::new(5.0, 0.0, 10.0, 10.0));
Sourcepub fn top_shifted(self, offset: f32) -> FRect
pub fn top_shifted(self, offset: f32) -> FRect
Shifts this rectangle to the top by offset
.
§Example
use sdl2::rect::FRect;
assert_eq!(FRect::new(0.0, 0.0, 10.0, 10.0).top_shifted(5.00), FRect::new(0.0, -5.0, 10.0, 10.0));
Sourcepub fn bottom_shifted(self, offset: f32) -> FRect
pub fn bottom_shifted(self, offset: f32) -> FRect
Shifts this rectangle to the bottom by offset
.
§Example
use sdl2::rect::FRect;
assert_eq!(FRect::new(0.0, 0.0, 10.0, 10.0).bottom_shifted(5.0), FRect::new(0.0, 5.0, 10.0, 10.0));
Sourcepub fn center(&self) -> FPoint
pub fn center(&self) -> FPoint
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::{FRect, FPoint};
let rect = FRect::new(1.0, 0.0, 2.0, 3.0);
assert_eq!(FPoint::new(2.0, 1.5), rect.center());
Sourcepub fn top_left(&self) -> FPoint
pub fn top_left(&self) -> FPoint
Returns the top-left corner of this rectangle.
§Example
use sdl2::rect::{FRect, FPoint};
let rect = FRect::new(1.0, 0.0, 2.0, 3.0);
assert_eq!(FPoint::new(1.0, 0.0), rect.top_left());
Sourcepub fn top_right(&self) -> FPoint
pub fn top_right(&self) -> FPoint
Returns the top-right corner of this rectangle.
§Example
use sdl2::rect::{FRect, FPoint};
let rect = FRect::new(1.0, 0.0, 2.0, 3.0);
assert_eq!(FPoint::new(3.0, 0.0), rect.top_right());
Sourcepub fn bottom_left(&self) -> FPoint
pub fn bottom_left(&self) -> FPoint
Returns the bottom-left corner of this rectangle.
§Example
use sdl2::rect::{FRect, FPoint};
let rect = FRect::new(1.0, 0.0, 2.0, 3.0);
assert_eq!(FPoint::new(1.0, 3.0), rect.bottom_left());
Sourcepub fn bottom_right(&self) -> FPoint
pub fn bottom_right(&self) -> FPoint
Returns the bottom-right corner of this rectangle.
§Example
use sdl2::rect::{FRect, FPoint};
let rect = FRect::new(1.0, 0.0, 2.0, 3.0);
assert_eq!(FPoint::new(3.0, 3.0), rect.bottom_right());
Sourcepub fn set_right(&mut self, right: f32)
pub fn set_right(&mut self, right: f32)
Sets the position of the right side of this rectangle to the given value, clamped to be greater than 0.
Sourcepub fn set_bottom(&mut self, bottom: f32)
pub fn set_bottom(&mut self, bottom: f32)
Sets the position of the bottom side of this rectangle to the given value, clamped to be greater than 0.
Sourcepub fn centered_on<P>(self, point: P) -> FRect
pub fn centered_on<P>(self, point: P) -> FRect
Centers the rectangle on the given point.
Sourcepub fn reposition<P>(&mut self, point: P)
pub fn reposition<P>(&mut self, point: P)
Moves this rect to the given position.
Sourcepub fn resize(&mut self, width: f32, height: f32)
pub fn resize(&mut self, width: f32, height: f32)
Resizes this rect to the given size after clamping the values.
Sourcepub 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. 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_frect
.
§Examples
use sdl2::rect::{FRect, FPoint};
let rect = FRect::new(1.0, 2.0, 3.0, 4.0);
assert!(rect.contains_point(FPoint::new(1.0, 2.0)));
assert!(!rect.contains_point(FPoint::new(0.0, 1.0)));
assert!(rect.contains_point(FPoint::new(3.0, 5.0)));
assert!(!rect.contains_point(FPoint::new(4.0, 6.0)));
Sourcepub fn contains_rect(&self, other: FRect) -> bool
pub fn contains_rect(&self, other: FRect) -> 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::FRect;
let rect = FRect::new(1.0, 2.0, 3.0, 4.0);
assert!(rect.contains_rect(rect));
assert!(rect.contains_rect(FRect::new(3.0, 3.0, 1.0, 1.0)));
assert!(!rect.contains_rect(FRect::new(2.0, 1.0, 1.0, 1.0)));
assert!(!rect.contains_rect(FRect::new(3.0, 3.0, 2.0, 1.0)));
pub fn raw_mut(&mut self) -> *mut SDL_FRect
pub fn raw_slice(slice: &[FRect]) -> *const SDL_FRect
pub fn from_ll(raw: SDL_FRect) -> FRect
Sourcepub fn from_enclose_points<R>(
points: &[FPoint],
clipping_rect: R,
) -> Option<FRect>
pub fn from_enclose_points<R>( points: &[FPoint], clipping_rect: R, ) -> Option<FRect>
Calculate a minimal rectangle enclosing a set of points. If a clipping rectangle is given, only points that are within it will be considered.
Sourcepub fn has_intersection(&self, other: FRect) -> bool
pub fn has_intersection(&self, other: FRect) -> 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::FRect;
let rect = FRect::new(0.0, 0.0, 5.0, 5.0);
assert!(rect.has_intersection(rect));
assert!(rect.has_intersection(FRect::new(2.0, 2.0, 5.0, 5.0)));
assert!(!rect.has_intersection(FRect::new(5.0, 0.0, 5.0, 5.0)));
Sourcepub fn intersection(&self, other: FRect) -> Option<FRect>
pub fn intersection(&self, other: FRect) -> Option<FRect>
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::FRect;
let rect = FRect::new(0.0, 0.0, 5.0, 5.0);
assert_eq!(rect.intersection(rect), Some(rect));
assert_eq!(rect.intersection(FRect::new(2.0, 2.0, 5.0, 5.0)),
Some(FRect::new(2.0, 2.0, 3.0, 3.0)));
assert_eq!(rect.intersection(FRect::new(5.0, 0.0, 5.0, 5.0)), None);
Sourcepub fn union(&self, other: FRect) -> FRect
pub fn union(&self, other: FRect) -> FRect
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::FRect;
let rect = FRect::new(0.0, 0.0, 5.0, 5.0);
assert_eq!(rect.union(rect), rect);
assert_eq!(rect.union(FRect::new(2.0, 2.0, 5.0, 5.0)), FRect::new(0.0, 0.0, 7.0, 7.0));
assert_eq!(rect.union(FRect::new(5.0, 0.0, 5.0, 5.0)), FRect::new(0.0, 0.0, 10.0, 5.0));