Struct Rectangle

Source
pub struct Rectangle<T> {
    pub min: Point<T>,
    pub max: Point<T>,
}
Expand description

A non-rotated rectangle, used to represent AABB collision boxes.

§Notice

The constructor will not check the legality of the parameters, it is possible that two points coincide, or the side length is negative

§Examples

let rect = Rectangle::new(0.0, 0.0, 1.0, 1.0);

Fields§

§min: Point<T>

origin x points of the rectangle

§max: Point<T>

origin y points of the rectangle

Implementations§

Source§

impl<T> Rectangle<T>

Constructors for rectangle_2d

Source

pub fn empty() -> Rectangle<T>
where T: Zero,

Create a rectangle_2d from 4 properties

§Notice

The constructor will not check the legality of the parameters, call [is_valid] to ensure that the rectangle_2d is legal.

§Examples
let rect = Rectangle::empty();
Source

pub fn new(x: T, y: T, width: T, height: T) -> Rectangle<T>
where T: Clone + Add<Output = T>,

Create a rectangle_2d from 4 properties

§Notice

The constructor will not check the legality of the parameters, call [is_valid] to ensure that the rectangle_2d is legal.

§Examples
let rect = Rectangle::new(0.0, 0.0, 1.0, 1.0);
Source

pub fn from_anchor<P>(origin: P, width: T, height: T) -> Rectangle<T>
where T: Clone + Add<Output = T>, P: Into<Point<T>>,

Create a [Rectangle] from anchor (top left point) and 2 properties

§Notice

The constructor will not check the legality of the parameters, call [is_valid] to ensure that the rectangle_2d is legal.

§Examples
let rect = Rectangle::from_anchor((0.0, 0.0), 1.0, 1.0);
Source

pub fn from_center<P>(center: P, width: T, height: T) -> Rectangle<T>
where T: Clone + One + Add<Output = T> + Sub<Output = T> + Div<Output = T>, P: Into<Point<T>>,

Create a rectangle_2d from center and 2 properties

§Notice

The constructor will not check the legality of the parameters, call [is_valid] to ensure that the rectangle_2d is legal.

§Examples
let rect = Rectangle::from_center((0.0, 0.0), 1.0, 1.0);
Source

pub fn from_origin(width: T, height: T) -> Rectangle<T>
where T: Clone + Zero<Output = T> + One + Add + Sub<Output = T> + Div<Output = T>,

Source

pub fn from_min_max<P1, P2>(min: P1, max: P2) -> Rectangle<T>
where P1: Into<Point<T>>, P2: Into<Point<T>>,

Create a rectangle_2d from two diagonal points.

§Notice

The constructor will not check the legality of the parameters, it is possible that two points coincide, or the side length is negative

§Examples
let rect = Rectangle::from_min_max((0.0, 0.0), (1.0, 1.0));
Source§

impl<T> Rectangle<T>

Source

pub fn width(&self) -> T
where T: Clone + Sub<Output = T>,

Get the width of the rectangle

§Examples
let rect = Rectangle::new(0.0, 0.0, 1.0, 1.0);
assert_eq!(rect.width(), 1.0);
Source

pub fn height(&self) -> T
where T: Clone + Sub<Output = T>,

Get the height of the rectangle

§Examples
let rect = Rectangle::new(0.0, 0.0, 1.0, 1.0);
assert_eq!(rect.height(), 1.0);
Source

pub fn origin(&self) -> Point<T>
where T: Clone,

Get the origin of the rectangle

§Examples
let rect = Rectangle::from_center((0.0, 0.0), 1.0,1.0);
assert_eq!(rect.origin(), -Point::new(0.5, 0.5));
Source

pub fn center(&self) -> Point<T>
where T: Clone + One + Add<Output = T> + Sub<Output = T> + Div<Output = T>,

Get the center point of the rectangle

§Examples
let rect = Rectangle::new(0.0, 0.0, 1.0, 1.0);
assert_eq!(rect.center(), Point::new(0.5, 0.5));
Source

pub fn ref_inner(&self) -> Rectangle<&T>

Move reference to the inner value

Source

pub fn contains(&self, point: &Point<T>) -> bool
where T: Clone + PartialOrd,

Source

pub fn overlaps(&self, other: &Rectangle<T>) -> bool
where T: Clone + PartialOrd,

Check if two rectangle had overlapped

Source

pub fn area(&self) -> T
where T: Clone + Mul<Output = T> + Sub<Output = T>,

Get the area of the rectangle

§Examples
let rect = Rectangle::new(0.0, 0.0, 2.0, 2.0);
assert_eq!(rect.area(), 4.0);

Trait Implementations§

Source§

impl<T> BitAndAssign for Rectangle<T>
where T: PartialOrd,

Source§

fn bitand_assign(&mut self, rhs: Rectangle<T>)

Returns the intersection of two rectangles, possibly with negative area, meaning the empty set

Source§

impl<T> BitOrAssign for Rectangle<T>
where T: PartialOrd,

Source§

fn bitor_assign(&mut self, rhs: Rectangle<T>)

Returns a larger rectangle that encompasses these two rectangles

Source§

impl<T> Clone for Rectangle<T>
where T: Clone,

Source§

fn clone(&self) -> Rectangle<T>

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<T> Debug for Rectangle<T>
where T: Debug,

Source§

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

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

impl<T> Display for Rectangle<T>
where T: Display,

Source§

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

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

impl<T> Hash for Rectangle<T>
where T: Hash,

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

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<T> PartialEq for Rectangle<T>
where T: PartialEq,

Source§

fn eq(&self, other: &Rectangle<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Shape2D for Rectangle<T>
where T: Signed + Clone + PartialOrd,

Source§

fn is_valid(&self) -> bool

A valid rectangle means it has a positive area.

§Examples
let rect = Rectangle::new(0.0, 0.0, 1.0, 1.0);
assert!(rect.is_valid());
Source§

fn boundary(&self) -> Rectangle<<Rectangle<T> as Shape2D>::Value>

§SAFETY

It may return a zero area rectangle if shape is not valid.

It never returns a rectangle with negative area.

§Examples
let rect = Rectangle::new(0.0, 0.0, -1.0, -1.0);
assert_eq!(rect.boundary(), Rectangle::new(0.0,0.0, 1.0,1.0));
Source§

fn vertices<'a>( &'a self, _: usize, ) -> <Rectangle<T> as Shape2D>::VertexIterator<'a>

Returns four vertices counterclockwise in the ↑Y coordinate system

Source§

fn edges<'a>(&'a self, _: usize) -> <Rectangle<T> as Shape2D>::LineIterator<'a>

Returns four edges counterclockwise in the ↑Y coordinate system

Source§

type Value = T

The value type of the shape.
Source§

type VertexIterator<'a> = IntoIter<Point<T>> where T: 'a

The value type of the shape.
Source§

type LineIterator<'a> = IntoIter<Line<T>> where T: 'a

The value type of the shape.
Source§

fn normalize(&mut self) -> bool

Returns true if the shape successfully normalized.
Source§

impl<T> Copy for Rectangle<T>
where T: Copy,

Source§

impl<T> Eq for Rectangle<T>
where T: Eq,

Source§

impl<T> StructuralPartialEq for Rectangle<T>

Auto Trait Implementations§

§

impl<T> Freeze for Rectangle<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Rectangle<T>
where T: RefUnwindSafe,

§

impl<T> Send for Rectangle<T>
where T: Send,

§

impl<T> Sync for Rectangle<T>
where T: Sync,

§

impl<T> Unpin for Rectangle<T>
where T: Unpin,

§

impl<T> UnwindSafe for Rectangle<T>
where T: UnwindSafe,

Blanket Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.