Trait vector2math::rectangle::Rectangle
source · pub trait Rectangle: Copy {
type Vector: Vector2;
Show 49 methods
// Required methods
fn new(top_left: Self::Vector, size: Self::Vector) -> Self;
fn top_left(self) -> Self::Vector;
fn size(self) -> Self::Vector;
// Provided methods
fn square(top_left: Self::Vector, side_length: Scalar<Self>) -> Self { ... }
fn centered(center: Self::Vector, size: Self::Vector) -> Self { ... }
fn square_centered(center: Self::Vector, side_length: Scalar<Self>) -> Self { ... }
fn map_into<R>(self) -> R
where R: Rectangle,
Scalar<R>: From<Scalar<Self>> { ... }
fn map_rect(self) -> [Scalar<Self>; 4] { ... }
fn map_with<R, F>(self, f: F) -> R
where R: Rectangle,
F: FnMut(Scalar<Self>) -> <<R as Rectangle>::Vector as Vector2>::Scalar { ... }
fn abs_size(self) -> Self::Vector { ... }
fn top_right(self) -> Self::Vector { ... }
fn bottom_left(self) -> Self::Vector { ... }
fn bottom_right(self) -> Self::Vector { ... }
fn abs_top_left(self) -> Self::Vector { ... }
fn abs_top_right(self) -> Self::Vector { ... }
fn abs_bottom_left(self) -> Self::Vector { ... }
fn abs_bottom_right(self) -> Self::Vector { ... }
fn top(self) -> Scalar<Self> { ... }
fn bottom(self) -> Scalar<Self> { ... }
fn left(self) -> Scalar<Self> { ... }
fn right(self) -> Scalar<Self> { ... }
fn abs_top(self) -> Scalar<Self> { ... }
fn abs_bottom(self) -> Scalar<Self> { ... }
fn abs_left(self) -> Scalar<Self> { ... }
fn abs_right(self) -> Scalar<Self> { ... }
fn width(self) -> Scalar<Self> { ... }
fn height(self) -> Scalar<Self> { ... }
fn abs_width(self) -> Scalar<Self> { ... }
fn abs_height(self) -> Scalar<Self> { ... }
fn center(self) -> Self::Vector { ... }
fn with_size(self, size: Self::Vector) -> Self { ... }
fn with_top(self, top: Scalar<Self>) -> Self { ... }
fn with_bottom(self, bottom: Scalar<Self>) -> Self { ... }
fn with_left(self, left: Scalar<Self>) -> Self { ... }
fn with_right(self, right: Scalar<Self>) -> Self { ... }
fn perimeter(self) -> Scalar<Self> { ... }
fn area(self) -> Scalar<Self> { ... }
fn translated(self, offset: Self::Vector) -> Self { ... }
fn scaled(self, scale: Scalar<Self>) -> Self { ... }
fn scaled2(self, scale: Self::Vector) -> Self { ... }
fn corners(self) -> [Self::Vector; 4] { ... }
fn contains(self, point: Self::Vector) -> bool { ... }
fn contains_all<I>(self, points: I) -> bool
where I: IntoIterator<Item = Self::Vector> { ... }
fn contains_any<I>(self, points: I) -> bool
where I: IntoIterator<Item = Self::Vector> { ... }
fn bounding<I>(points: I) -> Option<Self>
where I: IntoIterator<Item = Self::Vector> { ... }
fn inner_margin(self, margin: Scalar<Self>) -> Self { ... }
fn inner_margins(
self,
[left, right, top, bottom]: [Scalar<Self>; 4]
) -> Self { ... }
fn outer_margin(self, margin: Scalar<Self>) -> Self { ... }
fn outer_margins(
self,
[left, right, top, bottom]: [Scalar<Self>; 4]
) -> Self { ... }
}
Expand description
Trait for manipulating axis-aligned rectangles
Because the primary expected use for this crate is in 2D graphics and alignment implementations, a coordinate system where the positive Y direction is “down” is assumed.
Note
Methods of the form abs_*
account for the case where the size is negative.
If the size is not negative, they are identical to their non-abs_*
counterparts.
use vector2math::*;
let pos_size = [1, 2, 3, 4];
assert_eq!(pos_size.right(), pos_size.abs_right());
let neg_size = [1, 2, -3, -4];
assert_ne!(neg_size.right(), neg_size.abs_right());
let points = vec![
[-1, 0],
[1, 5],
[3, 2],
];
let bounding_rect: [i32; 4] = Rectangle::bounding(points).unwrap();
assert_eq!(
bounding_rect,
[-1, 0, 4, 5]
);
Required Associated Types§
Required Methods§
Provided Methods§
sourcefn square(top_left: Self::Vector, side_length: Scalar<Self>) -> Self
fn square(top_left: Self::Vector, side_length: Scalar<Self>) -> Self
Create a new square from a top-left corner position and a side length
sourcefn centered(center: Self::Vector, size: Self::Vector) -> Self
fn centered(center: Self::Vector, size: Self::Vector) -> Self
Create a new rectangle from a center position and a size
sourcefn square_centered(center: Self::Vector, side_length: Scalar<Self>) -> Self
fn square_centered(center: Self::Vector, side_length: Scalar<Self>) -> Self
Create a new square from a top-left corner position and a side length
sourcefn map_into<R>(self) -> Rwhere
R: Rectangle,
Scalar<R>: From<Scalar<Self>>,
fn map_into<R>(self) -> Rwhere R: Rectangle, Scalar<R>: From<Scalar<Self>>,
Map this rectangle to a rectangle of another type
sourcefn map_rect(self) -> [Scalar<Self>; 4]
fn map_rect(self) -> [Scalar<Self>; 4]
Map this rectangle to a [Scalar<Self>; 4]
This is an alias for Rectangle::map_into::<[Scalar<Self>; 4]>()
that is more concise
sourcefn map_with<R, F>(self, f: F) -> Rwhere
R: Rectangle,
F: FnMut(Scalar<Self>) -> <<R as Rectangle>::Vector as Vector2>::Scalar,
fn map_with<R, F>(self, f: F) -> Rwhere R: Rectangle, F: FnMut(Scalar<Self>) -> <<R as Rectangle>::Vector as Vector2>::Scalar,
Map this rectangle to a rectangle of another type using a function
sourcefn bottom_left(self) -> Self::Vector
fn bottom_left(self) -> Self::Vector
Get the bottom-left corner position
sourcefn bottom_right(self) -> Self::Vector
fn bottom_right(self) -> Self::Vector
Get the bottom-right corner position
sourcefn abs_top_left(self) -> Self::Vector
fn abs_top_left(self) -> Self::Vector
Get the absolute top-left corner position
sourcefn abs_top_right(self) -> Self::Vector
fn abs_top_right(self) -> Self::Vector
Get the absolute top-right corner position
sourcefn abs_bottom_left(self) -> Self::Vector
fn abs_bottom_left(self) -> Self::Vector
Get the absolute bottom-left corner position
sourcefn abs_bottom_right(self) -> Self::Vector
fn abs_bottom_right(self) -> Self::Vector
Get the absolute bottom-right corner position
sourcefn abs_bottom(self) -> Scalar<Self>
fn abs_bottom(self) -> Scalar<Self>
Get the absolute bottom y
sourcefn abs_height(self) -> Scalar<Self>
fn abs_height(self) -> Scalar<Self>
Get the absolute height
sourcefn with_size(self, size: Self::Vector) -> Self
fn with_size(self, size: Self::Vector) -> Self
Transform the rectangle into one with a different size
sourcefn with_top(self, top: Scalar<Self>) -> Self
fn with_top(self, top: Scalar<Self>) -> Self
Get the rectangle that is this one with a different top bound
sourcefn with_bottom(self, bottom: Scalar<Self>) -> Self
fn with_bottom(self, bottom: Scalar<Self>) -> Self
Get the rectangle that is this one with a different bottom bound
sourcefn with_left(self, left: Scalar<Self>) -> Self
fn with_left(self, left: Scalar<Self>) -> Self
Get the rectangle that is this one with a different left bound
sourcefn with_right(self, right: Scalar<Self>) -> Self
fn with_right(self, right: Scalar<Self>) -> Self
Get the rectangle that is this one with a different right bound
sourcefn translated(self, offset: Self::Vector) -> Self
fn translated(self, offset: Self::Vector) -> Self
Get the rectangle that is this one translated by some vector
sourcefn scaled(self, scale: Scalar<Self>) -> Self
fn scaled(self, scale: Scalar<Self>) -> Self
Get the rectangle that is this one with a scalar-scaled size
sourcefn scaled2(self, scale: Self::Vector) -> Self
fn scaled2(self, scale: Self::Vector) -> Self
Get the rectangle that is this one with a vector-scaled size
sourcefn corners(self) -> [Self::Vector; 4]
fn corners(self) -> [Self::Vector; 4]
Get an array the rectangle’s four corners, clockwise from top-left
sourcefn contains(self, point: Self::Vector) -> bool
fn contains(self, point: Self::Vector) -> bool
Check that the rectangle contains the given point. Includes edges.
sourcefn contains_all<I>(self, points: I) -> boolwhere
I: IntoIterator<Item = Self::Vector>,
fn contains_all<I>(self, points: I) -> boolwhere I: IntoIterator<Item = Self::Vector>,
Check that the rectangle contains all points
sourcefn contains_any<I>(self, points: I) -> boolwhere
I: IntoIterator<Item = Self::Vector>,
fn contains_any<I>(self, points: I) -> boolwhere I: IntoIterator<Item = Self::Vector>,
Check that the rectangle contains any point
sourcefn bounding<I>(points: I) -> Option<Self>where
I: IntoIterator<Item = Self::Vector>,
fn bounding<I>(points: I) -> Option<Self>where I: IntoIterator<Item = Self::Vector>,
Get the smallest rectangle that contains all the points
Returns None
if the iterator is empty
sourcefn inner_margin(self, margin: Scalar<Self>) -> Self
fn inner_margin(self, margin: Scalar<Self>) -> Self
Get the rectangle that is inside this one with the given margin on all sides
sourcefn inner_margins(self, [left, right, top, bottom]: [Scalar<Self>; 4]) -> Self
fn inner_margins(self, [left, right, top, bottom]: [Scalar<Self>; 4]) -> Self
Get the rectangle that is inside this one with the given margins
Margins should be ordered [left, right, top, bottom]
sourcefn outer_margin(self, margin: Scalar<Self>) -> Self
fn outer_margin(self, margin: Scalar<Self>) -> Self
Get the rectangle that is outside this one with the given margin on all sides
sourcefn outer_margins(self, [left, right, top, bottom]: [Scalar<Self>; 4]) -> Self
fn outer_margins(self, [left, right, top, bottom]: [Scalar<Self>; 4]) -> Self
Get the rectangle that is outside this one with the given margins
Margins should be ordered [left, right, top, bottom]