[][src]Trait vector2math::Rectangle

pub trait Rectangle: Copy {
    type Scalar: Scalar;
    type Vector: Vector2<Scalar = Self::Scalar>;
    fn new(top_left: Self::Vector, size: Self::Vector) -> Self;
fn top_left(self) -> Self::Vector;
fn size(self) -> Self::Vector; fn square(top_left: Self::Vector, side_length: Self::Scalar) -> Self { ... }
fn centered(center: Self::Vector, size: Self::Vector) -> Self { ... }
fn square_centered(center: Self::Vector, side_length: Self::Scalar) -> Self { ... }
fn map<R>(self) -> R
    where
        R: Rectangle,
        R::Scalar: From<Self::Scalar>
, { ... }
fn map_f32(self) -> [f32; 4]
    where
        f32: From<Self::Scalar>
, { ... }
fn map_f64(self) -> [f64; 4]
    where
        f64: From<Self::Scalar>
, { ... }
fn map_with<R, F>(self, f: F) -> R
    where
        R: Rectangle,
        F: FnMut(Self::Scalar) -> <<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) -> Self::Scalar { ... }
fn bottom(self) -> Self::Scalar { ... }
fn left(self) -> Self::Scalar { ... }
fn right(self) -> Self::Scalar { ... }
fn abs_top(self) -> Self::Scalar { ... }
fn abs_bottom(self) -> Self::Scalar { ... }
fn abs_left(self) -> Self::Scalar { ... }
fn abs_right(self) -> Self::Scalar { ... }
fn width(self) -> Self::Scalar { ... }
fn height(self) -> Self::Scalar { ... }
fn abs_width(self) -> Self::Scalar { ... }
fn abs_height(self) -> Self::Scalar { ... }
fn center(self) -> Self::Vector { ... }
fn with_top_left(self, top_left: Self::Vector) -> Self { ... }
fn with_center(self, center: Self::Vector) -> Self { ... }
fn with_size(self, size: Self::Vector) -> Self { ... }
fn perimeter(self) -> Self::Scalar { ... }
fn area(self) -> Self::Scalar { ... }
fn translated(self, offset: Self::Vector) -> Self { ... }
fn scaled(self, scale: Self::Scalar) -> Self { ... }
fn scaled2(self, scale: Self::Vector) -> Self { ... }
fn corners(self) -> IntoIter<Self::Vector> { ... }
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: Self::Scalar) -> Self { ... }
fn inner_margins(self, margins: [Self::Scalar; 4]) -> Self { ... }
fn outer_margin(self, margin: Self::Scalar) -> Self { ... }
fn outer_margins(self, margins: [Self::Scalar; 4]) -> Self { ... } }

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 if 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]
);

Associated Types

type Scalar: Scalar

The scalar type

type Vector: Vector2<Scalar = Self::Scalar>

The vector type

Loading content...

Required methods

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

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

fn top_left(self) -> Self::Vector

Get the top-left corner position

fn size(self) -> Self::Vector

Get the size

Loading content...

Provided methods

fn square(top_left: Self::Vector, side_length: Self::Scalar) -> Self

Create a new square from a top-left corner position and a side length

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

Create a new rectangle from a center position and a size

fn square_centered(center: Self::Vector, side_length: Self::Scalar) -> Self

Create a new square from a top-left corner position and a side length

fn map<R>(self) -> R where
    R: Rectangle,
    R::Scalar: From<Self::Scalar>, 

Map this rectangle to a rectangle of another type

fn map_f32(self) -> [f32; 4] where
    f32: From<Self::Scalar>, 

Map this rectangle to a [f32;4]

This is an alias for Rectangle::map::<[f32;4]>() that is more concise

fn map_f64(self) -> [f64; 4] where
    f64: From<Self::Scalar>, 

Map this rectangle to a [f64;4]

This is an alias for Rectangle::map::<[f64;4]>() that is more concise

fn map_with<R, F>(self, f: F) -> R where
    R: Rectangle,
    F: FnMut(Self::Scalar) -> <<R as Rectangle>::Vector as Vector2>::Scalar

Map this rectangle to a rectangle of another type using a function

fn abs_size(self) -> Self::Vector

Get the absolute size

fn top_right(self) -> Self::Vector

Get the top-right corner position

fn bottom_left(self) -> Self::Vector

Get the bottom-left corner position

fn bottom_right(self) -> Self::Vector

Get the bottom-right corner position

fn abs_top_left(self) -> Self::Vector

Get the absolute top-left corner position

fn abs_top_right(self) -> Self::Vector

Get the absolute top-right corner position

fn abs_bottom_left(self) -> Self::Vector

Get the absolute bottom-left corner position

fn abs_bottom_right(self) -> Self::Vector

Get the absolute bottom-right corner position

fn top(self) -> Self::Scalar

Get the top y

fn bottom(self) -> Self::Scalar

Get the bottom y

fn left(self) -> Self::Scalar

Get the left x

fn right(self) -> Self::Scalar

Get the right x

fn abs_top(self) -> Self::Scalar

Get the absolute top y

fn abs_bottom(self) -> Self::Scalar

Get the absolute bottom y

fn abs_left(self) -> Self::Scalar

Get the absolute left x

fn abs_right(self) -> Self::Scalar

Get the absolute right x

fn width(self) -> Self::Scalar

Get the width

fn height(self) -> Self::Scalar

Get the height

fn abs_width(self) -> Self::Scalar

Get the absolute width

fn abs_height(self) -> Self::Scalar

Get the absolute height

fn center(self) -> Self::Vector

Get the position of the center

fn with_top_left(self, top_left: Self::Vector) -> Self

Transform the rectangle into one with a different top-left corner position

fn with_center(self, center: Self::Vector) -> Self

Transform the rectangle into one with a different center position

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

Transform the rectangle into one with a different size

fn perimeter(self) -> Self::Scalar

Get the perimeter

fn area(self) -> Self::Scalar

Get the area

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

Get the rectangle that is this one translated by some vector

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

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

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

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

fn corners(self) -> IntoIter<Self::Vector>

Get an iterator over the rectangle's four corners

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

Check that the rectangle contains the given point

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

Check that the rectangle contains all points

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

Check that the rectangle contains any point

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

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

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

fn inner_margins(self, margins: [Self::Scalar; 4]) -> Self

Get the rectangle that is inside this one with the given margins

Margins should be ordered [left, right, top, bottom]

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

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

fn outer_margins(self, margins: [Self::Scalar; 4]) -> Self

Get the rectangle that is outside this one with the given margins

Margins should be ordered [left, right, top, bottom]

Loading content...

Implementors

impl<P> Rectangle for P where
    P: Pair + Copy,
    P::Item: Vector2
[src]

type Scalar = <P::Item as Vector2>::Scalar

type Vector = P::Item

Loading content...