Struct figures::Rect

source ·
pub struct Rect<Unit> {
    pub origin: Point<Unit>,
    pub size: Size<Unit>,
}
Expand description

A 2d area expressed as an origin (Point) and a Size.

Fields§

§origin: Point<Unit>

The origin of the rectangle

§size: Size<Unit>

The size of the rectangle.

Implementations§

source§

impl<Unit> Rect<Unit>

source

pub const fn new(origin: Point<Unit>, size: Size<Unit>) -> Self

Returns a new rectangle.

source

pub fn from_extents(p1: Point<Unit>, p2: Point<Unit>) -> Self
where Unit: Unit,

Returns a new rectangle using the given points to form the top-left and bottom-right of the rectangle.

The order of the parameters does not matter. The minimum values will form the top-left and the maximum values will form the bottom-right.

source

pub fn expand_rounded(self) -> Self
where Unit: Round + Unit,

Expands this rect to the nearest whole number.

This function will never return a smaller rectangle.

source

pub fn map<NewUnit>(self, map: impl FnMut(Unit) -> NewUnit) -> Rect<NewUnit>

Maps each component to map and returns a new value with the mapped components.

source

pub fn inset(self, amount: impl Into<Unit>) -> Self
where Unit: Add<Unit, Output = Unit> + AddAssign<Unit> + SubAssign<Unit> + Copy,

Returns a rectangle that has been inset by amount on all sides.

source

pub fn cast<NewUnit>(self) -> Rect<NewUnit>
where NewUnit: From<Unit>,

Converts the contents of this point to NewUnit using From.

source

pub fn try_cast<NewUnit>(self) -> Result<Rect<NewUnit>, NewUnit::Error>
where NewUnit: TryFrom<Unit>,

Converts the contents of this rect to NewUnit using TryFrom.

§Errors

Returns <NewUnit as TryFrom>::Error when the inner type cannot be converted. For this crate’s types, this genenerally will be

source

pub fn contains(&self, point: Point<Unit>) -> bool
where Unit: Unit,

Returns true if this rect contains point.

source

pub fn intersects(&self, other: &Self) -> bool
where Unit: Add<Output = Unit> + Ord + Copy,

Returns true if the areas of self and other overlap.

This function does not return true if the edges touch but do not overlap.

use figures::{Point, Rect, Size};

let a: Rect<i32> = Rect::new(Point::new(1, 1), Size::new(2, 2));
let b = Rect::new(Point::new(2, 2), Size::new(1, 1));
assert!(a.intersects(&b));
let c = Rect::new(Point::new(3, 1), Size::new(1, 1));
assert!(!a.intersects(&c));
source

pub fn intersection(&self, other: &Self) -> Option<Rect<Unit>>
where Unit: Unit,

Returns the overlapping rectangle of self and other. If the rectangles do not overlap, None will be returned.

use figures::{Point, Rect, Size};

let a: Rect<i32> = Rect::new(Point::new(1, 1), Size::new(3, 3));
let b = Rect::new(Point::new(2, 2), Size::new(3, 3));
assert_eq!(
    a.intersection(&b),
    Some(Rect::new(Point::new(2, 2), Size::new(2, 2)))
);
let c = Rect::new(Point::new(4, 1), Size::new(1, 1));
assert_eq!(a.intersection(&c), None);
source

pub fn extent(&self) -> Point<Unit>
where Unit: Unit,

Returns the non-origin point.

source§

impl<Unit> Rect<Unit>
where Unit: Add<Output = Unit> + Ord + Copy,

source

pub fn extents(&self) -> (Point<Unit>, Point<Unit>)

Returns the top-left and bottom-right points of this rectangle.

The first point returned will always be the top-right point, even if the size of the rectangle is negative.

Trait Implementations§

source§

impl<Unit> Add<Point<Unit>> for Rect<Unit>
where Unit: Add<Output = Unit>,

§

type Output = Rect<Unit>

The resulting type after applying the + operator.
source§

fn add(self, rhs: Point<Unit>) -> Self::Output

Performs the + operation. Read more
source§

impl<Unit: Clone> Clone for Rect<Unit>

source§

fn clone(&self) -> Rect<Unit>

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<Unit: Debug> Debug for Rect<Unit>

source§

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

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

impl<Unit> Default for Rect<Unit>
where Unit: Default,

source§

fn default() -> Self

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

impl<Unit> From<Size<Unit>> for Rect<Unit>
where Unit: Default,

source§

fn from(size: Size<Unit>) -> Self

Converts to this type from the input type.
source§

impl<Unit: Hash> Hash for Rect<Unit>

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<Unit> IntoSigned for Rect<Unit>
where Unit: IntoSigned,

§

type Signed = Rect<<Unit as IntoSigned>::Signed>

The signed representation of this type.
source§

fn into_signed(self) -> Self::Signed

Returns this value as an unsigned value. Values that are larger than can fit in an i32 are converted to i32::MAX.
source§

impl<Unit> IntoUnsigned for Rect<Unit>
where Unit: IntoUnsigned,

§

type Unsigned = Rect<<Unit as IntoUnsigned>::Unsigned>

The unsigned representation of this type.
source§

fn into_unsigned(self) -> Self::Unsigned

Returns this value as an unsigned value. Negative values will be converted to 0.
source§

impl<Unit: PartialEq> PartialEq for Rect<Unit>

source§

fn eq(&self, other: &Rect<Unit>) -> 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<Unit> Ranged for Rect<Unit>
where Unit: Ranged,

source§

const MAX: Self = _

The maximum value for this type.
source§

const MIN: Self = _

The minimum value for this type.
source§

impl<Unit> Sub<Point<Unit>> for Rect<Unit>
where Unit: Sub<Output = Unit>,

§

type Output = Rect<Unit>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Point<Unit>) -> Self::Output

Performs the - operation. Read more
source§

impl<Unit> Zero for Rect<Unit>
where Unit: Zero,

source§

const ZERO: Self = _

The zero value for this type.
source§

fn is_zero(&self) -> bool

Returns true if self represents 0.
source§

impl<Unit: Copy> Copy for Rect<Unit>

source§

impl<Unit: Eq> Eq for Rect<Unit>

source§

impl<Unit> StructuralPartialEq for Rect<Unit>

Auto Trait Implementations§

§

impl<Unit> Freeze for Rect<Unit>
where Unit: Freeze,

§

impl<Unit> RefUnwindSafe for Rect<Unit>
where Unit: RefUnwindSafe,

§

impl<Unit> Send for Rect<Unit>
where Unit: Send,

§

impl<Unit> Sync for Rect<Unit>
where Unit: Sync,

§

impl<Unit> Unpin for Rect<Unit>
where Unit: Unpin,

§

impl<Unit> UnwindSafe for Rect<Unit>
where Unit: 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<A> Cast for A

source§

fn cast<To>(self) -> To
where To: CastFrom<A>,

Casts self to the To type. This may be a lossy operation.
source§

impl<A> CastFrom<A> for A

source§

fn from_cast(from: A) -> A

Returns from as Self.
source§

impl<A, B> CastInto<A> for B
where A: CastFrom<B>,

source§

fn cast_into(self) -> A

Returns self as To.
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<Unit> IntoComponents<Unit> for Unit
where Unit: Copy,

source§

fn into_components(self) -> (Unit, Unit)

Extracts this type’s 2d vector components.
source§

fn to_vec<Type>(self) -> Type
where Type: FromComponents<Unit>,

Converts this type to another type using FromComponents and IntoComponents.
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, 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.