pub struct Rect { /* private fields */ }
Expand description
An axis-aligned rectangle, specified by lower-left and upper-right corners.
Implementations§
source§impl Rect
impl Rect
sourcepub fn from_dims(dims: Dims) -> Rect
pub fn from_dims(dims: Dims) -> Rect
Creates a rectangle with corners (0, 0), (dims.w(), dims.h())
.
§Example
let dims = Dims::new(100, 200);
let rect = Rect::from_dims(dims);
assert_eq!(rect.top(), 200);
assert_eq!(rect.bot(), 0);
assert_eq!(rect.left(), 0);
assert_eq!(rect.right(), 100);
sourcepub const fn center(&self) -> Point
pub const fn center(&self) -> Point
Returns the center point of the rectangle.
Note that the center point will be rounded to integer coordinates. The current behavior is to round down, but this is subject to change; users should not rely on this behavior.
§Examples
let rect = Rect::from_sides(0, 0, 200, 100);
assert_eq!(rect.center(), Point::new(100, 50));
Center points are rounded:
let rect = Rect::from_sides(0, 0, 55, 45);
assert_eq!(rect.center(), Point::new(27, 22));
sourcepub const fn from_point(p: Point) -> Rect
pub const fn from_point(p: Point) -> Rect
Creates a zero-area rectangle containing the given point.
§Example
let rect = Rect::from_point(Point::new(25, 60));
assert_eq!(rect.top(), 60);
assert_eq!(rect.bot(), 60);
assert_eq!(rect.left(), 25);
assert_eq!(rect.right(), 25);
sourcepub fn from_sides(left: i64, bot: i64, right: i64, top: i64) -> Rect
pub fn from_sides(left: i64, bot: i64, right: i64, top: i64) -> Rect
Creates a rectangle from all 4 sides (left, bottom, right, top).
§Example
let rect = Rect::from_sides(15, 20, 30, 40);
assert_eq!(rect.left(), 15);
assert_eq!(rect.bot(), 20);
assert_eq!(rect.right(), 30);
assert_eq!(rect.top(), 40);
§Panics
This method panics if left > right
or if bot > top
.
If you want sides to be sorted for you, consider using Rect::new
instead.
sourcepub const unsafe fn from_sides_unchecked(
left: i64,
bot: i64,
right: i64,
top: i64
) -> Rect
pub const unsafe fn from_sides_unchecked( left: i64, bot: i64, right: i64, top: i64 ) -> Rect
Creates a rectangle from all 4 sides (left, bottom, right, top), without checking
that left <= right
and bot <= top
.
§Safety
The caller must ensure that left <= right
and that bot <= top
.
sourcepub fn from_corners_option(ll: Point, ur: Point) -> Option<Rect>
pub fn from_corners_option(ll: Point, ur: Point) -> Option<Rect>
Creates a rectangle from a lower left and upper right corner point,
but returns None
if the given sides would make the rectangle empty.
The rectangle is empty if the left edge is beyond the right edge, or if the bottom edge is above the top edge.
§Example
let rect = Rect::from_corners_option(Point::new(15, 20), Point::new(30, 40));
assert_eq!(rect, Some(Rect::from_sides(15, 20, 30, 40)));
let rect = Rect::from_corners_option(Point::new(10, 20), Point::new(0, 40));
assert_eq!(rect, None);
sourcepub fn from_sides_option(
left: i64,
bot: i64,
right: i64,
top: i64
) -> Option<Rect>
pub fn from_sides_option( left: i64, bot: i64, right: i64, top: i64 ) -> Option<Rect>
Creates a rectangle from all 4 sides (left, bottom, right, top),
but returns None
if the given sides would make the rectangle empty.
The rectangle is empty if the left edge is beyond the right edge, or if the bottom edge is above the top edge.
§Example
let rect = Rect::from_sides_option(15, 20, 30, 40);
assert_eq!(rect, Some(Rect::from_sides(15, 20, 30, 40)));
let rect = Rect::from_sides_option(10, 20, 0, 40);
assert_eq!(rect, None);
sourcepub const fn from_xy(x: i64, y: i64) -> Rect
pub const fn from_xy(x: i64, y: i64) -> Rect
Creates a zero-area empty rectangle containing the given (x, y)
coordinates.
§Example
let rect = Rect::from_xy(25, 60);
assert_eq!(rect.top(), 60);
assert_eq!(rect.bot(), 60);
assert_eq!(rect.left(), 25);
assert_eq!(rect.right(), 25);
sourcepub fn new(lower_left: Point, upper_right: Point) -> Rect
pub fn new(lower_left: Point, upper_right: Point) -> Rect
Creates a new rectangle from the given opposite corner points.
§Examples
Create a rectangle from the lower left and upper right corners:
let rect = Rect::new(Point::new(15, 20), Point::new(30, 40));
assert_eq!(rect.left(), 15);
assert_eq!(rect.bot(), 20);
assert_eq!(rect.right(), 30);
assert_eq!(rect.top(), 40);
Create a rectangle from the lower right and upper left corners:
let rect = Rect::new(Point::new(30, 20), Point::new(15, 40));
assert_eq!(rect.left(), 15);
assert_eq!(rect.bot(), 20);
assert_eq!(rect.right(), 30);
assert_eq!(rect.top(), 40);
sourcepub const unsafe fn new_unchecked(p0: Point, p1: Point) -> Rect
pub const unsafe fn new_unchecked(p0: Point, p1: Point) -> Rect
Creates a new rectangle from a lower-left corner and an upper-right corner, without checking that coordinates are ordered correctly.
§Safety
The caller must ensure that p0
is the lower-left corner and that
p1
is the upper-right corner. In other words, you must ensure that
p0.x <= p1.x
and p0.y <= p1.y
.
sourcepub const fn from_spans(h: Span, v: Span) -> Rect
pub const fn from_spans(h: Span, v: Span) -> Rect
sourcepub const fn bot(&self) -> i64
pub const fn bot(&self) -> i64
Returns the bottom y-coordinate of the rectangle.
§Example
let rect = Rect::from_sides(10, 20, 30, 40);
assert_eq!(rect.bot(), 20);
sourcepub const fn top(&self) -> i64
pub const fn top(&self) -> i64
Returns the top y-coordinate of the rectangle.
§Example
let rect = Rect::from_sides(10, 20, 30, 40);
assert_eq!(rect.top(), 40);
sourcepub const fn left(&self) -> i64
pub const fn left(&self) -> i64
Returns the left x-coordinate of the rectangle.
§Example
let rect = Rect::from_sides(10, 20, 30, 40);
assert_eq!(rect.left(), 10);
sourcepub const fn right(&self) -> i64
pub const fn right(&self) -> i64
Returns the right x-coordinate of the rectangle.
§Example
let rect = Rect::from_sides(10, 20, 30, 40);
assert_eq!(rect.right(), 30);
sourcepub const fn vspan(&self) -> Span
pub const fn vspan(&self) -> Span
Returns the vertical span of the rectangle.
§Example
let rect = Rect::from_sides(10, 20, 30, 40);
assert_eq!(rect.vspan(), Span::new(20, 40));
sourcepub fn with_hspan(self, hspan: Span) -> Rect
pub fn with_hspan(self, hspan: Span) -> Rect
sourcepub fn with_vspan(self, vspan: Span) -> Rect
pub fn with_vspan(self, vspan: Span) -> Rect
sourcepub const fn width(&self) -> i64
pub const fn width(&self) -> i64
Returns the horizontal width of the rectangle.
§Example
let rect = Rect::from_sides(10, 20, 30, 50);
assert_eq!(rect.width(), 20);
sourcepub const fn height(&self) -> i64
pub const fn height(&self) -> i64
Returns the vertical height of the rectangle.
§Example
let rect = Rect::from_sides(10, 20, 30, 50);
assert_eq!(rect.height(), 30);
sourcepub const fn area(&self) -> i64
pub const fn area(&self) -> i64
Returns the area of the rectangle.
§Example
let rect = Rect::from_sides(10, 20, 30, 50);
assert_eq!(rect.area(), 600);
sourcepub const fn lower_coord(&self, dir: Dir) -> i64
pub const fn lower_coord(&self, dir: Dir) -> i64
Returns the lowest/leftmost coordinate of the rectangle in the given direction.
For Dir::Horiz
, this is the left edge’s x-coordinate.
For Dir::Vert
, this is the bottom edge’s y-coordinate.
§Example
let rect = Rect::from_sides(10, 20, 30, 50);
assert_eq!(rect.lower_coord(Dir::Vert), 20);
assert_eq!(rect.lower_coord(Dir::Horiz), 10);
sourcepub const fn upper_coord(&self, dir: Dir) -> i64
pub const fn upper_coord(&self, dir: Dir) -> i64
Returns the highest/rightmost coordinate of the rectangle in the given direction.
For Dir::Horiz
, this is the right edge’s x-coordinate.
For Dir::Vert
, this is the top edge’s y-coordinate.
§Example
let rect = Rect::from_sides(10, 20, 30, 50);
assert_eq!(rect.upper_coord(Dir::Vert), 50);
assert_eq!(rect.upper_coord(Dir::Horiz), 30);
sourcepub const fn span(&self, dir: Dir) -> Span
pub const fn span(&self, dir: Dir) -> Span
Returns the span of the rectangle in the given direction.
§Example
let rect = Rect::from_sides(10, 20, 30, 50);
assert_eq!(rect.span(Dir::Vert), Span::new(20, 50));
assert_eq!(rect.span(Dir::Horiz), Span::new(10, 30));
sourcepub fn inner_span(&self, other: Rect, dir: Dir) -> Span
pub fn inner_span(&self, other: Rect, dir: Dir) -> Span
Returns the span between the inner two edges of two rectangles along the given direction.
§Example
let r1 = Rect::from_sides(10, 25, 30, 50);
let r2 = Rect::from_sides(20, 15, 70, 35);
assert_eq!(r1.inner_span(r2, Dir::Horiz), Span::new(20, 30));
assert_eq!(r1.inner_span(r2, Dir::Vert), Span::new(25, 35));
The “order” of r1
and r2
does not matter:
let r1 = Rect::from_sides(10, 25, 30, 50);
let r2 = Rect::from_sides(20, 15, 70, 35);
assert_eq!(r2.inner_span(r1, Dir::Horiz), Span::new(20, 30));
assert_eq!(r2.inner_span(r1, Dir::Vert), Span::new(25, 35));
sourcepub fn outer_span(&self, other: Rect, dir: Dir) -> Span
pub fn outer_span(&self, other: Rect, dir: Dir) -> Span
Returns the span between the outer two edges of two rectangles along the given direction.
§Example
let r1 = Rect::from_sides(10, 25, 30, 50);
let r2 = Rect::from_sides(20, 15, 70, 35);
assert_eq!(r1.outer_span(r2, Dir::Horiz), Span::new(10, 70));
assert_eq!(r1.outer_span(r2, Dir::Vert), Span::new(15, 50));
The “order” of r1
and r2
does not matter:
let r1 = Rect::from_sides(10, 25, 30, 50);
let r2 = Rect::from_sides(20, 15, 70, 35);
assert_eq!(r2.outer_span(r1, Dir::Horiz), Span::new(10, 70));
assert_eq!(r2.outer_span(r1, Dir::Vert), Span::new(15, 50));
sourcepub fn edge_closer_to(&self, x: i64, dir: Dir) -> i64
pub fn edge_closer_to(&self, x: i64, dir: Dir) -> i64
Returns the edge of a rectangle closest to the coordinate x
along a given direction.
§Example
let rect = Rect::from_sides(10, 25, 30, 50);
assert_eq!(rect.edge_closer_to(14, Dir::Horiz), 10);
assert_eq!(rect.edge_closer_to(22, Dir::Horiz), 30);
assert_eq!(rect.edge_closer_to(23, Dir::Vert), 25);
assert_eq!(rect.edge_closer_to(37, Dir::Vert), 25);
assert_eq!(rect.edge_closer_to(38, Dir::Vert), 50);
assert_eq!(rect.edge_closer_to(59, Dir::Vert), 50);
sourcepub fn edge_farther_from(&self, x: i64, dir: Dir) -> i64
pub fn edge_farther_from(&self, x: i64, dir: Dir) -> i64
Returns the edge of a rectangle farthest from the coordinate x
along a given direction.
§Example
let rect = Rect::from_sides(10, 25, 30, 50);
assert_eq!(rect.edge_farther_from(14, Dir::Horiz), 30);
assert_eq!(rect.edge_farther_from(22, Dir::Horiz), 10);
assert_eq!(rect.edge_farther_from(23, Dir::Vert), 50);
assert_eq!(rect.edge_farther_from(37, Dir::Vert), 50);
assert_eq!(rect.edge_farther_from(38, Dir::Vert), 25);
assert_eq!(rect.edge_farther_from(59, Dir::Vert), 25);
sourcepub fn from_dir_spans(dir: Dir, parallel_span: Span, perp_span: Span) -> Rect
pub fn from_dir_spans(dir: Dir, parallel_span: Span, perp_span: Span) -> Rect
Creates a rectangle from two Span
s, where the first is parallel to dir
,
and the second is perpendicular.
§Example
let span1 = Span::new(10, 30);
let span2 = Span::new(25, 50);
let rect = Rect::from_dir_spans(Dir::Horiz, span1, span2);
assert_eq!(rect, Rect::from_sides(10, 25, 30, 50));
let rect = Rect::from_dir_spans(Dir::Vert, span1, span2);
assert_eq!(rect, Rect::from_sides(25, 10, 50, 30));
sourcepub const fn length(&self, dir: Dir) -> i64
pub const fn length(&self, dir: Dir) -> i64
Returns the length of this rectangle in the given direction.
§Example
let rect = Rect::from_sides(0, 0, 200, 100);
assert_eq!(rect.length(Dir::Horiz), 200);
assert_eq!(rect.length(Dir::Vert), 100);
sourcepub const fn longer_dir(&self) -> Dir
pub const fn longer_dir(&self) -> Dir
Returns the direction in which the rectangle is longer, choosing Dir::Horiz
if the sides
are equal.
§Example
let rect = Rect::from_sides(0, 0, 100, 200);
assert_eq!(rect.longer_dir(), Dir::Vert);
let rect = Rect::from_sides(0, 0, 200, 100);
assert_eq!(rect.longer_dir(), Dir::Horiz);
let rect = Rect::from_sides(0, 0, 100, 100);
assert_eq!(rect.longer_dir(), Dir::Horiz);
sourcepub fn longer_dir_strict(&self) -> Option<Dir>
pub fn longer_dir_strict(&self) -> Option<Dir>
Returns the direction in which the rectangle is longer, returning None
if the sides
are equal.
§Example
let rect = Rect::from_sides(0, 0, 100, 200);
assert_eq!(rect.longer_dir_strict(), Some(Dir::Vert));
let rect = Rect::from_sides(0, 0, 200, 100);
assert_eq!(rect.longer_dir_strict(), Some(Dir::Horiz));
let rect = Rect::from_sides(0, 0, 100, 100);
assert_eq!(rect.longer_dir_strict(), None);
sourcepub const fn shorter_dir(&self) -> Dir
pub const fn shorter_dir(&self) -> Dir
Returns the direction in which the rectangle is shorter, choosing Dir::Vert
if the sides
are equal.
This always returns the opposite of Rect::longer_dir
.
§Example
let rect = Rect::from_sides(0, 0, 100, 200);
assert_eq!(rect.shorter_dir(), Dir::Horiz);
let rect = Rect::from_sides(0, 0, 200, 100);
assert_eq!(rect.shorter_dir(), Dir::Vert);
let rect = Rect::from_sides(0, 0, 100, 100);
assert_eq!(rect.shorter_dir(), Dir::Vert);
sourcepub fn shorter_dir_strict(&self) -> Option<Dir>
pub fn shorter_dir_strict(&self) -> Option<Dir>
Returns the direction in which the rectangle is shorter, choosing None
if the sides
are equal.
This always returns the opposite of Rect::longer_dir_strict
.
§Example
let rect = Rect::from_sides(0, 0, 100, 200);
assert_eq!(rect.shorter_dir_strict(), Some(Dir::Horiz));
let rect = Rect::from_sides(0, 0, 200, 100);
assert_eq!(rect.shorter_dir_strict(), Some(Dir::Vert));
let rect = Rect::from_sides(0, 0, 100, 100);
assert_eq!(rect.shorter_dir_strict(), None);
sourcepub fn union(self, other: Rect) -> Rect
pub fn union(self, other: Rect) -> Rect
Computes the rectangular union of this Rect
with another Rect
.
§Example
let r1 = Rect::from_sides(0, 0, 100, 200);
let r2 = Rect::from_sides(-50, 20, 120, 160);
assert_eq!(r1.union(r2), Rect::from_sides(-50, 0, 120, 200));
sourcepub fn union_all<T>(rects: impl Iterator<Item = T>) -> Rect
pub fn union_all<T>(rects: impl Iterator<Item = T>) -> Rect
Calculates the rectangular union of all rectangles provided.
§Example
let rects = vec![
Rect::from_sides(10, 20, 30, 40),
Rect::from_sides(-10, 25, 20, 35),
Rect::from_sides(15, 20, 25, 60),
];
assert_eq!(Rect::union_all(rects.into_iter()), Rect::from_sides(-10, 20, 30, 60));
§Panics
This function panics if the provided iterator has no elements.
If your iterator may be empty, consider using Rect::union_all_option
.
sourcepub fn union_all_option<T>(rects: impl Iterator<Item = T>) -> Option<Rect>
pub fn union_all_option<T>(rects: impl Iterator<Item = T>) -> Option<Rect>
Calculates the rectangular union of all Option<Rect>
s provided.
All None
elements in the iterator are ignored.
If the iterator has no Some(_)
elements, this function returns None
.
§Example
let rects = vec![
Some(Rect::from_sides(10, 20, 30, 40)),
Some(Rect::from_sides(-10, 25, 20, 35)),
None,
Some(Rect::from_sides(15, 20, 25, 60)),
];
assert_eq!(Rect::union_all_option(rects.into_iter()), Some(Rect::from_sides(-10, 20, 30, 60)));
sourcepub fn intersection(self, other: Rect) -> Option<Rect>
pub fn intersection(self, other: Rect) -> Option<Rect>
Computes the rectangular intersection of this Rect
with another Rect
.
Returns None
if the intersection is empty.
§Example
let r1 = Rect::from_sides(0, 0, 100, 200);
let r2 = Rect::from_sides(-50, 20, 120, 160);
assert_eq!(r1.intersection(r2), Some(Rect::from_sides(0, 20, 100, 160)));
let r1 = Rect::from_sides(0, 0, 100, 200);
let r2 = Rect::from_sides(120, -60, 240, 800);
assert_eq!(r1.intersection(r2), None);
sourcepub fn expand_all(&self, amount: i64) -> Rect
pub fn expand_all(&self, amount: i64) -> Rect
Expands the rectangle by amount
on all sides.
§Example
let rect = Rect::from_sides(0, 0, 100, 200);
assert_eq!(rect.expand_all(20), Rect::from_sides(-20, -20, 120, 220));
sourcepub fn expand_dir(&self, dir: Dir, amount: i64) -> Rect
pub fn expand_dir(&self, dir: Dir, amount: i64) -> Rect
Expands the rectangle by amount
on both sides associated with the direction dir
.
§Example
let rect = Rect::from_sides(0, 0, 100, 200);
assert_eq!(rect.expand_dir(Dir::Horiz, 20), Rect::from_sides(-20, 0, 120, 200));
assert_eq!(rect.expand_dir(Dir::Vert, 20), Rect::from_sides(0, -20, 100, 220));
sourcepub fn expand_side(&self, side: Side, amount: i64) -> Rect
pub fn expand_side(&self, side: Side, amount: i64) -> Rect
Expands the rectangle by amount
on the given side.
§Example
let rect = Rect::from_sides(0, 0, 100, 200);
assert_eq!(rect.expand_side(Side::Top, 20), Rect::from_sides(0, 0, 100, 220));
assert_eq!(rect.expand_side(Side::Bot, 20), Rect::from_sides(0, -20, 100, 200));
assert_eq!(rect.expand_side(Side::Left, 20), Rect::from_sides(-20, 0, 100, 200));
assert_eq!(rect.expand_side(Side::Right, 20), Rect::from_sides(0, 0, 120, 200));
sourcepub fn expand_corner(self, corner: Corner, amount: i64) -> Rect
pub fn expand_corner(self, corner: Corner, amount: i64) -> Rect
Expands the rectangle by amount
at the given corner.
§Example
let rect = Rect::from_sides(0, 0, 100, 200);
assert_eq!(rect.expand_corner(Corner::LowerLeft, 20), Rect::from_sides(-20, -20, 100, 200));
assert_eq!(rect.expand_corner(Corner::LowerRight, 20), Rect::from_sides(0, -20, 120, 200));
assert_eq!(rect.expand_corner(Corner::UpperLeft, 20), Rect::from_sides(-20, 0, 100, 220));
assert_eq!(rect.expand_corner(Corner::UpperRight, 20), Rect::from_sides(0, 0, 120, 220));
sourcepub fn expand_sides(&self, sides: Sides<i64>) -> Rect
pub fn expand_sides(&self, sides: Sides<i64>) -> Rect
Expands the rectangle by some (possibly different) amount on each side.
§Example
let rect = Rect::from_sides(0, 0, 100, 200);
let sides = Sides::new(10, 20, 30, 40);
assert_eq!(rect.expand_sides(sides), Rect::from_sides(-10, -20, 130, 240));
sourcepub fn shrink_all(&self, amount: i64) -> Option<Rect>
pub fn shrink_all(&self, amount: i64) -> Option<Rect>
sourcepub fn shrink_dir(&self, dir: Dir, amount: i64) -> Option<Rect>
pub fn shrink_dir(&self, dir: Dir, amount: i64) -> Option<Rect>
Shrinks the rectangle by amount
on both sides associated with the direction dir
.
Returns None
if shrinking would make the rectangle invalid.
§Example
let rect = Rect::from_sides(0, 0, 100, 200);
assert_eq!(rect.shrink_dir(Dir::Horiz, 20), Some(Rect::from_sides(20, 0, 80, 200)));
assert_eq!(rect.shrink_dir(Dir::Vert, 20), Some(Rect::from_sides(0, 20, 100, 180)));
assert_eq!(rect.shrink_dir(Dir::Vert, 120), None);
sourcepub fn shrink_side(&self, side: Side, amount: i64) -> Option<Rect>
pub fn shrink_side(&self, side: Side, amount: i64) -> Option<Rect>
Shrinks the rectangle by amount
on the given side.
Returns None
if shrinking would make the rectangle invalid.
§Example
let rect = Rect::from_sides(0, 0, 100, 200);
assert_eq!(rect.shrink_side(Side::Top, 20), Some(Rect::from_sides(0, 0, 100, 180)));
assert_eq!(rect.shrink_side(Side::Bot, 20), Some(Rect::from_sides(0, 20, 100, 200)));
assert_eq!(rect.shrink_side(Side::Left, 20), Some(Rect::from_sides(20, 0, 100, 200)));
assert_eq!(rect.shrink_side(Side::Right, 20), Some(Rect::from_sides(0, 0, 80, 200)));
assert_eq!(rect.shrink_side(Side::Right, 210), None);
sourcepub fn shrink_sides(&self, sides: Sides<i64>) -> Option<Rect>
pub fn shrink_sides(&self, sides: Sides<i64>) -> Option<Rect>
Shrinks the rectangle by some (possibly different) amount on each side.
Returns None
if shrinking would make the rectangle invalid.
§Example
let rect = Rect::from_sides(0, 0, 100, 200);
let sides = Sides::new(10, 20, 30, 40);
assert_eq!(rect.shrink_sides(sides), Some(Rect::from_sides(10, 20, 70, 160)));
sourcepub fn shrink_corner(self, corner: Corner, amount: i64) -> Option<Rect>
pub fn shrink_corner(self, corner: Corner, amount: i64) -> Option<Rect>
Shrinks the rectangle by amount
at the given corner.
Returns None
if shrinking would make the rectangle invalid.
§Example
let rect = Rect::from_sides(0, 0, 100, 200);
assert_eq!(rect.shrink_corner(Corner::LowerLeft, 20), Some(Rect::from_sides(20, 20, 100, 200)));
assert_eq!(rect.shrink_corner(Corner::LowerRight, 20), Some(Rect::from_sides(0, 20, 80, 200)));
assert_eq!(rect.shrink_corner(Corner::UpperLeft, 20), Some(Rect::from_sides(20, 0, 100, 180)));
assert_eq!(rect.shrink_corner(Corner::UpperRight, 20), Some(Rect::from_sides(0, 0, 80, 180)));
assert_eq!(rect.shrink_corner(Corner::UpperRight, 110), None);
sourcepub fn dims(&self) -> Dims
pub fn dims(&self) -> Dims
Returns the dimensions of the rectangle.
§Example
let rect = Rect::from_sides(20, 20, 100, 200);
assert_eq!(rect.dims(), Dims::new(80, 180));
sourcepub fn lower_left(&self) -> Point
pub fn lower_left(&self) -> Point
The lower left corner of the rectangle.
§Example
let rect = Rect::from_sides(20, 20, 100, 200);
assert_eq!(rect.lower_left(), Point::new(20, 20));
sourcepub fn lower_right(&self) -> Point
pub fn lower_right(&self) -> Point
The lower right corner of the rectangle.
§Example
let rect = Rect::from_sides(20, 20, 100, 200);
assert_eq!(rect.lower_right(), Point::new(100, 20));
sourcepub fn upper_left(&self) -> Point
pub fn upper_left(&self) -> Point
The upper left corner of the rectangle.
§Example
let rect = Rect::from_sides(20, 20, 100, 200);
assert_eq!(rect.upper_left(), Point::new(20, 200));
sourcepub fn upper_right(&self) -> Point
pub fn upper_right(&self) -> Point
The upper right corner of the rectangle.
§Example
let rect = Rect::from_sides(20, 20, 100, 200);
assert_eq!(rect.upper_right(), Point::new(100, 200));
sourcepub fn corner(&self, corner: Corner) -> Point
pub fn corner(&self, corner: Corner) -> Point
Returns the desired corner of the rectangle.
§Example
let rect = Rect::from_sides(20, 20, 100, 200);
assert_eq!(rect.corner(Corner::LowerLeft), Point::new(20, 20));
assert_eq!(rect.corner(Corner::LowerRight), Point::new(100, 20));
assert_eq!(rect.corner(Corner::UpperLeft), Point::new(20, 200));
assert_eq!(rect.corner(Corner::UpperRight), Point::new(100, 200));
sourcepub fn side(&self, side: Side) -> i64
pub fn side(&self, side: Side) -> i64
Returns the desired side of the rectangle.
§Example
let rect = Rect::from_sides(20, 20, 100, 200);
assert_eq!(rect.side(Side::Bot), 20);
assert_eq!(rect.side(Side::Left), 20);
assert_eq!(rect.side(Side::Top), 200);
assert_eq!(rect.side(Side::Right), 100);
sourcepub fn edge(&self, side: Side) -> Edge
pub fn edge(&self, side: Side) -> Edge
Returns the desired edge of the rectangle.
§Example
let rect = Rect::from_sides(20, 20, 100, 200);
assert_eq!(rect.edge(Side::Bot), Edge::new(Side::Bot, 20, Span::new(20, 100)));
assert_eq!(rect.edge(Side::Top), Edge::new(Side::Top, 200, Span::new(20, 100)));
assert_eq!(rect.edge(Side::Left), Edge::new(Side::Left, 20, Span::new(20, 200)));
assert_eq!(rect.edge(Side::Right), Edge::new(Side::Right, 100, Span::new(20, 200)));
sourcepub fn snap_to_grid(&self, grid: i64) -> Rect
pub fn snap_to_grid(&self, grid: i64) -> Rect
Snaps the corners of this rectangle to the given grid.
Note that the rectangle may have zero area after snapping.
§Example
let rect = Rect::from_sides(17, 23, 101, 204);
assert_eq!(rect.snap_to_grid(5), Rect::from_sides(15, 25, 100, 205));
let rect = Rect::from_sides(16, 17, 101, 104);
assert_eq!(rect.snap_to_grid(5), Rect::from_sides(15, 15, 100, 105));
let rect = Rect::from_sides(16, 17, 17, 18);
assert_eq!(rect.snap_to_grid(5), Rect::from_sides(15, 15, 15, 20));
sourcepub fn cutout(&self, clip: Rect) -> [Rect; 4]
pub fn cutout(&self, clip: Rect) -> [Rect; 4]
Based on clip
, cuts a hole in this rectangle and returns the four surrounding pieces.
Assumes that clip
is entirely contained by this rectangle.
§Example
let rect = Rect::from_sides(0, 0, 100, 100);
let clip = Rect::from_sides(20, 20, 80, 80);
assert_eq!(rect.cutout(clip), [
Rect::from_sides(0, 80, 100, 100),
Rect::from_sides(0, 0, 100, 20),
Rect::from_sides(0, 0, 20, 100),
Rect::from_sides(80, 0, 100, 100),
]);
sourcepub fn has_integer_center(&self) -> bool
pub fn has_integer_center(&self) -> bool
Returns whether the rectangle’s center has integer coordinates.
§Example
let rect = Rect::from_sides(0, 0, 100, 100);
assert!(rect.has_integer_center());
let rect = Rect::from_sides(0, 0, 99, 100);
assert!(!rect.has_integer_center());
Trait Implementations§
source§impl BoundingUnion<Rect> for Rect
impl BoundingUnion<Rect> for Rect
source§impl Contains<Point> for Rect
impl Contains<Point> for Rect
source§fn contains(&self, other: &Point) -> Containment
fn contains(&self, other: &Point) -> Containment
source§fn partially_intersects(&self, other: &T) -> bool
fn partially_intersects(&self, other: &T) -> bool
other
is fully or partially enclosed in this shape.source§impl<'de> Deserialize<'de> for Rect
impl<'de> Deserialize<'de> for Rect
source§fn deserialize<__D>(
__deserializer: __D
) -> Result<Rect, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D
) -> Result<Rect, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
source§impl HasTransformedView for Rect
impl HasTransformedView for Rect
§type TransformedView = Rect
type TransformedView = Rect
Self
.source§fn transformed_view(
&self,
trans: Transformation
) -> <Rect as HasTransformedView>::TransformedView
fn transformed_view( &self, trans: Transformation ) -> <Rect as HasTransformedView>::TransformedView
self
.source§impl Ord for Rect
impl Ord for Rect
source§impl PartialEq for Rect
impl PartialEq for Rect
source§impl PartialOrd for Rect
impl PartialOrd for Rect
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl Serialize for Rect
impl Serialize for Rect
source§fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
source§impl TransformMut for Rect
impl TransformMut for Rect
source§fn transform_mut(&mut self, trans: Transformation)
fn transform_mut(&mut self, trans: Transformation)
Transformation
trans
.source§impl TranslateMut for Rect
impl TranslateMut for Rect
source§fn translate_mut(&mut self, p: Point)
fn translate_mut(&mut self, p: Point)
Point
through mutation.impl Copy for Rect
impl Eq for Rect
impl StructuralPartialEq 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> AlignBbox for Twhere
T: AlignBboxMut,
impl<T> AlignBbox for Twhere
T: AlignBboxMut,
source§impl<T> AlignBboxMut for Twhere
T: AlignRectMut + Bbox,
impl<T> AlignBboxMut for Twhere
T: AlignRectMut + Bbox,
source§impl<T> AlignRect for Twhere
T: AlignRectMut,
impl<T> AlignRect for Twhere
T: AlignRectMut,
source§impl<T> AlignRectMut for Twhere
T: Translate,
impl<T> AlignRectMut for Twhere
T: Translate,
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> BoundingUnion<Option<T>> for Twhere
T: BoundingUnion<T, Output = T> + Clone,
impl<T> BoundingUnion<Option<T>> for Twhere
T: BoundingUnion<T, Output = T> + Clone,
source§fn bounding_union(
&self,
other: &Option<T>
) -> <T as BoundingUnion<Option<T>>>::Output
fn bounding_union( &self, other: &Option<T> ) -> <T as BoundingUnion<Option<T>>>::Output
other
. Read more§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
source§impl<T, U> CustomHardwareType<Flipped<T>> for Uwhere
U: CustomHardwareType<T>,
T: HardwareType,
impl<T, U> CustomHardwareType<Flipped<T>> for Uwhere
U: CustomHardwareType<T>,
T: HardwareType,
source§fn from_layout_type(other: &Flipped<T>) -> U
fn from_layout_type(other: &Flipped<T>) -> U
source§impl<T, U> CustomHardwareType<InOut<T>> for Uwhere
U: CustomHardwareType<T>,
T: HardwareType,
impl<T, U> CustomHardwareType<InOut<T>> for Uwhere
U: CustomHardwareType<T>,
T: HardwareType,
source§fn from_layout_type(other: &InOut<T>) -> U
fn from_layout_type(other: &InOut<T>) -> U
source§impl<T, U> CustomHardwareType<Input<T>> for Uwhere
U: CustomHardwareType<T>,
T: HardwareType,
impl<T, U> CustomHardwareType<Input<T>> for Uwhere
U: CustomHardwareType<T>,
T: HardwareType,
source§fn from_layout_type(other: &Input<T>) -> U
fn from_layout_type(other: &Input<T>) -> U
source§impl<T, U> CustomHardwareType<Output<T>> for Uwhere
U: CustomHardwareType<T>,
T: HardwareType,
impl<T, U> CustomHardwareType<Output<T>> for Uwhere
U: CustomHardwareType<T>,
T: HardwareType,
source§fn from_layout_type(other: &Output<T>) -> U
fn from_layout_type(other: &Output<T>) -> U
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§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
§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.§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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request