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§

source

type Vector: Vector2

The vector type

Required Methods§

source

fn new(top_left: Self::Vector, size: Self::Vector) -> Self

Create a new rectangle from a top-left corner position and a size

source

fn top_left(self) -> Self::Vector

Get the top-left corner position

source

fn size(self) -> Self::Vector

Get the size

Provided Methods§

source

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

source

fn centered(center: Self::Vector, size: Self::Vector) -> Self

Create a new rectangle from a center position and a size

source

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

source

fn map_into<R>(self) -> Rwhere R: Rectangle, Scalar<R>: From<Scalar<Self>>,

Map this rectangle to a rectangle of another type

source

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

source

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

source

fn abs_size(self) -> Self::Vector

Get the absolute size

source

fn top_right(self) -> Self::Vector

Get the top-right corner position

source

fn bottom_left(self) -> Self::Vector

Get the bottom-left corner position

source

fn bottom_right(self) -> Self::Vector

Get the bottom-right corner position

source

fn abs_top_left(self) -> Self::Vector

Get the absolute top-left corner position

source

fn abs_top_right(self) -> Self::Vector

Get the absolute top-right corner position

source

fn abs_bottom_left(self) -> Self::Vector

Get the absolute bottom-left corner position

source

fn abs_bottom_right(self) -> Self::Vector

Get the absolute bottom-right corner position

source

fn top(self) -> Scalar<Self>

Get the top y

source

fn bottom(self) -> Scalar<Self>

Get the bottom y

source

fn left(self) -> Scalar<Self>

Get the left x

source

fn right(self) -> Scalar<Self>

Get the right x

source

fn abs_top(self) -> Scalar<Self>

Get the absolute top y

source

fn abs_bottom(self) -> Scalar<Self>

Get the absolute bottom y

source

fn abs_left(self) -> Scalar<Self>

Get the absolute left x

source

fn abs_right(self) -> Scalar<Self>

Get the absolute right x

source

fn width(self) -> Scalar<Self>

Get the width

source

fn height(self) -> Scalar<Self>

Get the height

source

fn abs_width(self) -> Scalar<Self>

Get the absolute width

source

fn abs_height(self) -> Scalar<Self>

Get the absolute height

source

fn center(self) -> Self::Vector

Get the position of the center

source

fn with_size(self, size: Self::Vector) -> Self

Transform the rectangle into one with a different size

source

fn with_top(self, top: Scalar<Self>) -> Self

Get the rectangle that is this one with a different top bound

source

fn with_bottom(self, bottom: Scalar<Self>) -> Self

Get the rectangle that is this one with a different bottom bound

source

fn with_left(self, left: Scalar<Self>) -> Self

Get the rectangle that is this one with a different left bound

source

fn with_right(self, right: Scalar<Self>) -> Self

Get the rectangle that is this one with a different right bound

source

fn perimeter(self) -> Scalar<Self>

Get the perimeter

source

fn area(self) -> Scalar<Self>

Get the area

source

fn translated(self, offset: Self::Vector) -> Self

Get the rectangle that is this one translated by some vector

source

fn scaled(self, scale: Scalar<Self>) -> Self

Get the rectangle that is this one with a scalar-scaled size

source

fn scaled2(self, scale: Self::Vector) -> Self

Get the rectangle that is this one with a vector-scaled size

source

fn corners(self) -> [Self::Vector; 4]

Get an array the rectangle’s four corners, clockwise from top-left

source

fn contains(self, point: Self::Vector) -> bool

Check that the rectangle contains the given point. Includes edges.

source

fn contains_all<I>(self, points: I) -> boolwhere I: IntoIterator<Item = Self::Vector>,

Check that the rectangle contains all points

source

fn contains_any<I>(self, points: I) -> boolwhere I: IntoIterator<Item = Self::Vector>,

Check that the rectangle contains any point

source

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

source

fn inner_margin(self, margin: Scalar<Self>) -> Self

Get the rectangle that is inside this one with the given margin on all sides

source

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]

source

fn outer_margin(self, margin: Scalar<Self>) -> Self

Get the rectangle that is outside this one with the given margin on all sides

source

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]

Implementors§

source§

impl<P> Rectangle for Pwhere P: Pair + Copy, P::Item: Vector2,

§

type Vector = <P as Pair>::Item