Struct glamour::Rect

source ·
#[repr(C)]
pub struct Rect<T: Unit = f32> { pub origin: Point2<T>, pub size: Size2<T>, }
Expand description

2D axis-aligned rectangle represented as “origin” and “size”.

Fields§

§origin: Point2<T>

Lower bound of the rect.

§size: Size2<T>

Size of the rect.

Implementations§

source§

impl<T: Unit> Rect<T>

source

pub const ZERO: Self = _

Zero rect (origin = 0.0, size = 0.0).

source

pub fn new(origin: impl Into<Point2<T>>, size: impl Into<Size2<T>>) -> Rect<T>

New Rect from origin/size.

source

pub fn from_untyped(untyped: Rect<T::Scalar>) -> Rect<T>

Bitcast an untyped instance to self.

source

pub fn to_untyped(self) -> Rect<T::Scalar>

Bitcast self to an untyped instance.

source

pub fn as_untyped(&self) -> &Rect<T::Scalar>

Reinterpret cast self as the untyped variant.

source

pub fn as_untyped_mut(&mut self) -> &mut Rect<T::Scalar>

Reinterpret cast self as the untyped variant.

source

pub fn cast<T2>(self) -> Rect<T2>
where T2: Unit<Scalar = T::Scalar>,

Cast to a different coordinate space with the same underlying scalar type.

source

pub fn cast_ref<T2>(&self) -> &Rect<T2>
where T2: Unit<Scalar = T::Scalar>,

Cast to a different coordinate space with the same underlying scalar type.

source

pub fn cast_mut<T2>(&mut self) -> &mut Rect<T2>
where T2: Unit<Scalar = T::Scalar>,

Cast to a different coordinate space with the same underlying scalar type.

source

pub fn try_cast<T2>(self) -> Option<Rect<T2>>
where T2: Unit,

Cast to a different coordinate space with scalar type conversion. Returns None if any component could not be converted to the target scalar type.

source

pub const fn from_tuple((origin, size): (Point2<T>, Size2<T>)) -> Self

Instantiate from tuple.

source

pub const fn to_tuple(self) -> (Point2<T>, Size2<T>)

Convert to tuple.

source

pub const fn from_size(size: Size2<T>) -> Self

Rect at (0.0, 0.0) with size.

source

pub fn from_box(b: Box2<T>) -> Self

Create from Box2.

Note: This may lose precision due to floating point arithmetic.

source

pub fn from_min_max(min: Point2<T>, max: Point2<T>) -> Self

Create from min/max points.

source

pub fn from_points<I>(points: I) -> Self
where I: IntoIterator<Item = Point2<T>>,

Calculate the bounding rect that covers all points.

source

pub const fn min(&self) -> Point2<T>

Get rect lower bound (origin).

source

pub fn max(&self) -> Point2<T>

Get rect upper bound (origin + size).

source

pub const fn width(&self) -> T::Scalar

Width of the rectangle.

source

pub const fn height(&self) -> T::Scalar

Height of the rectangle.

source

pub fn x_range(&self) -> Range<T::Scalar>

Range of X coordinates covered by this rectangle.

source

pub fn y_range(&self) -> Range<T::Scalar>

Range of Y coordinates covered by this rectangle.

source

pub fn corners(&self) -> [Point2<T>; 4]

Corners of the rectangle, clockwise from top left.

source

pub fn center(&self) -> Point2<T>

Get the point at the center of the rect.

source

pub fn translate(self, by: Vector2<T>) -> Self

Translate a copy of the rect by vector.

source

pub fn inflate(&self, by: Size2<T>) -> Self

Increase the size of the rect by moving the origin back by the size, and increasing the size of the rectangle by 2 * by.

§Example
let r = Rect::<f32>::new((10.0, 10.0), (10.0, 10.0));
let r = r.inflate((10.0, 10.0).into());
assert_eq!(r.origin, (0.0, 0.0));
assert_eq!(r.size, (30.0, 30.0));
source

pub fn to_box2(&self) -> Box2<T>

Convert to Box2.

source

pub fn area(&self) -> T::Scalar

Get the area of the rect (equivalent to self.size.area()).

source

pub fn is_empty(&self) -> bool

True if size is zero or negative or NaN.

source

pub fn is_negative(&self) -> bool

True if size is negative or NaN.

source§

impl<T> Rect<T>
where T: Unit, T::Scalar: FloatScalar,

source

pub fn is_finite(&self) -> bool

True if the rect only contains finite and non-NaN coordinates.

source

pub fn round(self) -> Self

Round all coordinates.

Note: This may create an empty rect from a non-empty rect.

§Example
let r = Rect::<f32>::new((0.51, 0.49), (0.51, 0.49));
let r = r.round();
assert_eq!(r, Rect::<f32>::new((1.0, 0.0), (1.0, 0.0)));
source

pub fn round_in(self) -> Self

Round all coordinates towards the center of the rect.

This function needs to convert the rect to a Box2 before rounding, which loses both performance and precision. Use Box2 if you need to perform this operation frequently.

Note: This may create an empty rect from a non-empty rect.

§Example
let r = Rect::<f32>::new((0.51, 0.49), (1.51, 1.49));
let r = r.round_in();
assert_eq!(r, Rect::<f32>::new((1.0, 1.0), (1.0, 0.0)));
source

pub fn round_out(self) -> Self

Round all coordinates away from the center of the rect.

This function needs to convert the rect to a Box2 before rounding, which loses both performance and precision. Use Box2 if you need to perform this operation frequently.

Note: As opposed to Rect::round() and Rect::round_in(), this will not create an empty rect from a non-empty rect.

§Example
let r = Rect::<f32>::new((0.51, 0.49), (1.51, 1.49));
let r = r.round_out();
assert_eq!(r, Rect::new((0.0, 0.0), (3.0, 2.0)));
source

pub fn lerp(self, other: Self, t: T::Scalar) -> Self

Linear interpolation between two rects.

Trait Implementations§

source§

impl<T> AbsDiffEq<(Point2<T>, Size2<T>)> for Rect<T>
where T: Unit, T::Scalar: AbsDiffEq, <T::Scalar as AbsDiffEq>::Epsilon: Copy,

§

type Epsilon = <<T as Unit>::Scalar as AbsDiffEq>::Epsilon

Used for specifying relative comparisons.
source§

fn default_epsilon() -> Self::Epsilon

The default tolerance to use when testing values that are close together. Read more
source§

fn abs_diff_eq( &self, other: &(Point2<T>, Size2<T>), epsilon: Self::Epsilon ) -> bool

A test for equality that uses the absolute difference to compute the approximate equality of two numbers.
source§

fn abs_diff_ne( &self, other: &(Point2<T>, Size2<T>), epsilon: Self::Epsilon ) -> bool

The inverse of AbsDiffEq::abs_diff_eq.
source§

impl<T: Unit> AbsDiffEq for Rect<T>

§

type Epsilon = <<T as Unit>::Scalar as AbsDiffEq>::Epsilon

Used for specifying relative comparisons.
source§

fn default_epsilon() -> Self::Epsilon

The default tolerance to use when testing values that are close together. Read more
source§

fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool

A test for equality that uses the absolute difference to compute the approximate equality of two numbers.
source§

fn abs_diff_ne(&self, other: &Self, epsilon: Self::Epsilon) -> bool

The inverse of AbsDiffEq::abs_diff_eq.
source§

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

source§

fn clone(&self) -> Self

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: Unit> Contains<Point2<T>> for Rect<T>

source§

fn contains(&self, point: &Point2<T>) -> bool

Returns true if thing is inside self.
source§

impl<T: Unit> Debug for Rect<T>

source§

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

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

impl<T: Unit> Default for Rect<T>

source§

fn default() -> Self

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

impl<T: Unit> From<(Point2<T>, Size2<T>)> for Rect<T>

source§

fn from(tuple: (Point2<T>, Size2<T>)) -> Rect<T>

Converts to this type from the input type.
source§

impl<T: Unit> From<Box2<T>> for Rect<T>

source§

fn from(x: Box2<T>) -> Self

Converts to this type from the input type.
source§

impl<T: Unit> From<Rect<T>> for (Point2<T>, Size2<T>)

source§

fn from(value: Rect<T>) -> (Point2<T>, Size2<T>)

Converts to this type from the input type.
source§

impl<T: Unit> From<Rect<T>> for Box2<T>

source§

fn from(x: Rect<T>) -> Self

Converts to this type from the input type.
source§

impl<T> Hash for Rect<T>
where T: Unit, T::Scalar: 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: Unit> Intersection<Box2<T>> for Rect<T>

§

type Intersection = Rect<T>

The type of intersection. Read more
source§

fn intersects(&self, thing: &Box2<T>) -> bool

True if thing intersects with self.
source§

fn intersection(&self, thing: &Box2<T>) -> Option<Self::Intersection>

If thing intersects with self, return the intersection. Otherwise, returns None.
source§

impl<T: Unit> Intersection<Point2<T>> for Rect<T>

§

type Intersection = Point2<T>

The type of intersection. Read more
source§

fn intersects(&self, thing: &Point2<T>) -> bool

True if thing intersects with self.
source§

fn intersection(&self, thing: &Point2<T>) -> Option<Self::Intersection>

If thing intersects with self, return the intersection. Otherwise, returns None.
source§

impl<T: Unit> Intersection for Rect<T>

§

type Intersection = Rect<T>

The type of intersection. Read more
source§

fn intersects(&self, thing: &Rect<T>) -> bool

True if thing intersects with self.
source§

fn intersection(&self, thing: &Rect<T>) -> Option<Self::Intersection>

If thing intersects with self, return the intersection. Otherwise, returns None.
source§

impl<T: Unit> PartialEq<(Point2<T>, Size2<T>)> for Rect<T>

source§

fn eq(&self, (origin, size): &(Point2<T>, Size2<T>)) -> 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<T: Unit> PartialEq for Rect<T>

source§

fn eq(&self, other: &Self) -> 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<T> RelativeEq<(Point2<T>, Size2<T>)> for Rect<T>
where T: Unit, T::Scalar: RelativeEq, <T::Scalar as AbsDiffEq>::Epsilon: Copy,

source§

fn default_max_relative() -> Self::Epsilon

The default relative tolerance for testing values that are far-apart. Read more
source§

fn relative_eq( &self, other: &(Point2<T>, Size2<T>), epsilon: Self::Epsilon, max_relative: Self::Epsilon ) -> bool

A test for equality that uses a relative comparison if the values are far apart.
source§

fn relative_ne( &self, other: &(Point2<T>, Size2<T>), epsilon: Self::Epsilon, max_relative: Self::Epsilon ) -> bool

The inverse of RelativeEq::relative_eq.
source§

impl<T: Unit> RelativeEq for Rect<T>
where T::Scalar: RelativeEq,

source§

fn default_max_relative() -> Self::Epsilon

The default relative tolerance for testing values that are far-apart. Read more
source§

fn relative_eq( &self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon ) -> bool

A test for equality that uses a relative comparison if the values are far apart.
source§

fn relative_ne( &self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon ) -> bool

The inverse of RelativeEq::relative_eq.
source§

impl<T: Unit> UlpsEq<(Point2<T>, Size2<T>)> for Rect<T>
where T::Scalar: UlpsEq, <T::Scalar as AbsDiffEq>::Epsilon: Copy,

source§

fn default_max_ulps() -> u32

The default ULPs to tolerate when testing values that are far-apart. Read more
source§

fn ulps_eq( &self, other: &(Point2<T>, Size2<T>), epsilon: Self::Epsilon, max_ulps: u32 ) -> bool

A test for equality that uses units in the last place (ULP) if the values are far apart.
source§

fn ulps_ne( &self, other: &(Point2<T>, Size2<T>), epsilon: Self::Epsilon, max_ulps: u32 ) -> bool

The inverse of UlpsEq::ulps_eq.
source§

impl<T: Unit> UlpsEq for Rect<T>
where T::Scalar: UlpsEq,

source§

fn default_max_ulps() -> u32

The default ULPs to tolerate when testing values that are far-apart. Read more
source§

fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool

A test for equality that uses units in the last place (ULP) if the values are far apart.
source§

fn ulps_ne(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool

The inverse of UlpsEq::ulps_eq.
source§

impl<T> Union for Rect<T>
where T: Unit,

§

type Union = Rect<T>

The type of the union.
source§

fn union(self, other: Self) -> Self

Compute the union of two things.
source§

impl<T: Unit> Zeroable for Rect<T>

SAFETY: All members are Pod, and we are #[repr(C)]

source§

fn zeroed() -> Self

source§

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

source§

impl<T> Eq for Rect<T>
where T: Unit, T::Scalar: Eq,

source§

impl<T: Unit> Pod for Rect<T>

SAFETY: All members are Pod, and we are #[repr(C)]

Auto Trait Implementations§

§

impl<T> Freeze for Rect<T>
where <T as Unit>::Scalar: Freeze,

§

impl<T> RefUnwindSafe for Rect<T>
where <T as Unit>::Scalar: RefUnwindSafe,

§

impl<T> Send for Rect<T>

§

impl<T> Sync for Rect<T>

§

impl<T> Unpin for Rect<T>
where <T as Unit>::Scalar: Unpin,

§

impl<T> UnwindSafe for Rect<T>
where <T as Unit>::Scalar: 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> CheckedBitPattern for T
where T: AnyBitPattern,

§

type Bits = T

Self must have the same layout as the specified Bits except for the possible invalid bit patterns being checked during is_valid_bit_pattern.
source§

fn is_valid_bit_pattern(_bits: &T) -> bool

If this function returns true, then it must be valid to reinterpret bits as &Self.
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> 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.
source§

impl<T> AnyBitPattern for T
where T: Pod,

source§

impl<T> NoUninit for T
where T: Pod,

source§

impl<T> PodValue for T
where T: Copy + Debug + Default + PartialEq + Pod + Send + Sync + Serializable + 'static,

source§

impl<T> Serializable for T