Struct geometry::rect::Rect

source ·
pub struct Rect { /* private fields */ }
Expand description

An axis-aligned rectangle, specified by lower-left and upper-right corners.

Implementations§

source§

impl Rect

source

pub fn from_dims(dims: Dims) -> Self

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

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

pub const fn from_point(p: Point) -> Self

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

pub fn from_sides(left: i64, bot: i64, right: i64, top: i64) -> Self

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.

source

pub const unsafe fn from_sides_unchecked( left: i64, bot: i64, right: i64, top: i64 ) -> Self

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.

source

pub fn from_corners_option(ll: Point, ur: Point) -> Option<Self>

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

pub fn from_sides_option( left: i64, bot: i64, right: i64, top: i64 ) -> Option<Self>

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

pub const fn from_xy(x: i64, y: i64) -> Self

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

pub fn new(lower_left: Point, upper_right: Point) -> Self

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

pub const unsafe fn new_unchecked(p0: Point, p1: Point) -> Self

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.

source

pub const fn from_spans(h: Span, v: Span) -> Self

Creates a rectangle from horizontal and vertical Spans.

§Example
let hspan = Span::new(15, 30);
let vspan = Span::new(20, 40);
let rect = Rect::from_spans(hspan, vspan);
assert_eq!(rect.left(), 15);
assert_eq!(rect.bot(), 20);
assert_eq!(rect.right(), 30);
assert_eq!(rect.top(), 40);
source

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

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

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

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

pub const fn hspan(&self) -> Span

Returns the horizontal Span of the rectangle.

§Example
let rect = Rect::from_sides(10, 20, 30, 40);
assert_eq!(rect.hspan(), Span::new(10, 30));
source

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

pub fn with_hspan(self, hspan: Span) -> Self

Returns a new Rect with the given hspan and the same vertical span.

§Example
let rect = Rect::from_sides(10, 20, 30, 40);
let new_hspan = Span::new(100, 200);
let new_rect = rect.with_hspan(new_hspan);
assert_eq!(new_rect, Rect::from_sides(100, 20, 200, 40));
source

pub fn with_vspan(self, vspan: Span) -> Self

Returns a Rect with the given vspan and the same horizontal span.

§Example
let rect = Rect::from_sides(10, 20, 30, 40);
let new_vspan = Span::new(100, 200);
let new_rect = rect.with_vspan(new_vspan);
assert_eq!(new_rect, Rect::from_sides(10, 100, 30, 200));
source

pub fn with_span(self, span: Span, dir: Dir) -> Self

Returns a Rect with the given span in the given dir, and the current span in the other direction.

§Example
let rect = Rect::from_sides(10, 20, 30, 40);
let new_vspan = Span::new(100, 200);
let new_rect = rect.with_span(new_vspan, Dir::Vert);
assert_eq!(new_rect, Rect::from_sides(10, 100, 30, 200));
source

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

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

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

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

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

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

pub fn inner_span(&self, other: Self, 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));
source

pub fn outer_span(&self, other: Self, 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));
source

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

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

pub fn from_dir_spans(dir: Dir, parallel_span: Span, perp_span: Span) -> Self

Creates a rectangle from two Spans, 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));
source

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

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

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

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

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

pub fn union(self, other: Self) -> Self

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

pub fn union_all<T>(rects: impl Iterator<Item = T>) -> Self
where T: Into<Self>,

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.

source

pub fn union_all_option<T>(rects: impl Iterator<Item = T>) -> Option<Self>
where T: Into<Option<Self>>,

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

pub fn intersection(self, other: Self) -> Option<Self>

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

pub fn expand_all(&self, amount: i64) -> Self

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

pub fn expand_dir(&self, dir: Dir, amount: i64) -> Self

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

pub fn expand_side(&self, side: Side, amount: i64) -> Self

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

pub fn expand_corner(self, corner: Corner, amount: i64) -> Self

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

pub fn expand_sides(&self, sides: Sides<i64>) -> Self

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

pub fn shrink_all(&self, amount: i64) -> Option<Self>

Shrinks the rectangle by amount on all sides.

Returns None if shrinking would make the rectangle invalid.

§Example
let rect = Rect::from_sides(0, 0, 100, 200);
assert_eq!(rect.shrink_all(20), Some(Rect::from_sides(20, 20, 80, 180)));
assert_eq!(rect.shrink_all(105), None);
source

pub fn shrink_dir(&self, dir: Dir, amount: i64) -> Option<Self>

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

pub fn shrink_side(&self, side: Side, amount: i64) -> Option<Self>

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

pub fn shrink_sides(&self, sides: Sides<i64>) -> Option<Self>

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

pub fn shrink_corner(self, corner: Corner, amount: i64) -> Option<Self>

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

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

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

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

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

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

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

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

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

pub fn snap_to_grid(&self, grid: i64) -> Self

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

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

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 Bbox for Rect

source§

fn bbox(&self) -> Option<Rect>

Computes the axis-aligned rectangular bounding box. Read more
source§

fn bbox_rect(&self) -> Rect

Computes the axis-aligned rectangular bounding box, panicking if it is empty.
source§

impl BoundingUnion<Rect> for Rect

§

type Output = Rect

The type of the output shape representing the bounding union.
source§

fn bounding_union(&self, other: &Rect) -> Self::Output

Calculates the bounding union of this shape with other. 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 Contains<Point> for Rect

source§

fn contains(&self, other: &Point) -> Containment

Returns a Containment indicating how other is enclosed within this shape. Read more
source§

fn encloses(&self, other: &T) -> bool

Returns true if other is fully enclosed in this shape.
source§

fn partially_intersects(&self, other: &T) -> bool

Returns true if other is fully or partially enclosed in this shape.
source§

impl Debug for Rect

source§

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

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

impl Default for Rect

source§

fn default() -> Rect

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for Rect

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl From<Dims> for Rect

source§

fn from(value: Dims) -> Self

Converts the Dims to a Rect as described in Rect::from_dims.

source§

impl From<Rect> for Dims

source§

fn from(value: Rect) -> Self

Obtains Dims from the given Rect using Rect::dims.

source§

impl From<Rect> for Shape

source§

fn from(value: Rect) -> Self

Converts to this type from the input type.
source§

impl HasTransformedView for Rect

§

type TransformedView = Rect

An object storing a transformed view of Self.
source§

fn transformed_view(&self, trans: Transformation) -> Self::TransformedView

Produces a transformed view of self.
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 Intersect<Rect> for Rect

§

type Output = Rect

The type of the output shape representing the intersection.
source§

fn intersect(&self, bounds: &Rect) -> Option<Self::Output>

Calculates the intersection of this shape with other. Read more
source§

impl Ord for Rect

source§

fn cmp(&self, other: &Rect) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
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 PartialOrd for Rect

source§

fn partial_cmp(&self, other: &Rect) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for Rect

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl TransformMut for Rect

source§

fn transform_mut(&mut self, trans: Transformation)

Applies matrix-vector Transformation trans.
source§

impl TranslateMut for Rect

source§

fn translate_mut(&mut self, p: Point)

Translates the shape by a Point through mutation.
source§

impl Copy for Rect

source§

impl Eq for Rect

source§

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 T
where T: AlignBboxMut,

source§

fn align_bbox(self, mode: AlignMode, other: impl Bbox, offset: i64) -> Self

Align self using its bounding box and the bounding box of other. Read more
source§

impl<T> AlignBboxMut for T
where T: AlignRectMut + Bbox,

source§

fn align_bbox_mut(&mut self, mode: AlignMode, other: impl Bbox, offset: i64)

Align self using its bounding box and the bounding box of other. Read more
source§

impl<T> AlignRect for T
where T: AlignRectMut,

source§

fn align(self, mode: AlignMode, srect: Rect, orect: Rect, offset: i64) -> Self

Align self based on the relationship between srect and orect. Read more
source§

impl<T> AlignRectMut for T
where T: Translate,

source§

fn align_mut(&mut self, mode: AlignMode, srect: Rect, orect: Rect, offset: i64)

Align self based on the relationship between srect and orect. Read more
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> BoundingUnion<Option<T>> for T
where T: BoundingUnion<T, Output = T> + Clone,

§

type Output = T

The type of the output shape representing the bounding union.
source§

fn bounding_union( &self, other: &Option<T> ) -> <T as BoundingUnion<Option<T>>>::Output

Calculates the bounding union of this shape with other. 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> PlaceBbox for T
where T: PlaceBboxMut,

source§

fn place_bbox(self, mode: PlaceMode, pt: Point) -> Self

Places an object at the given point using its boudning box. Read more
source§

impl<T> PlaceBboxMut for T
where T: PlaceRectMut + Bbox,

source§

fn place_bbox_mut(&mut self, mode: PlaceMode, pt: Point)

Places an object at the given point using its bounding box. Read more
source§

impl<T> PlaceRect for T
where T: PlaceRectMut,

source§

fn place(self, mode: PlaceMode, srect: Rect, pt: Point) -> Self

Places an object at the given point. Read more
source§

impl<T> PlaceRectMut for T
where T: TranslateMut,

source§

fn place_mut(&mut self, mode: PlaceMode, srect: Rect, pt: Point)

Places an object at the given point. Read more
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> Transform for T
where T: TransformMut,

source§

fn transform(self, trans: Transformation) -> Self

Applies matrix-vector Transformation trans. Read more
source§

impl<T> Translate for T
where T: TranslateMut,

source§

fn translate(self, p: Point) -> Self

Translates the shape by a Point through mutation. 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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,