Struct Rect

Source
#[repr(C)]
pub struct Rect { pub min: Pos2, pub max: Pos2, }
Expand description

A rectangular region of space.

Usually a Rect has a positive (or zero) size, and then Self::min <= Self::max. In these cases Self::min is the left-top corner and Self::max is the right-bottom corner.

A rectangle is allowed to have a negative size, which happens when the order of min and max are swapped. These are usually a sign of an error.

Normally the unit is points (logical pixels) in screen space coordinates.

Fields§

§min: Pos2

One of the corners of the rectangle, usually the left top one.

§max: Pos2

The other corner, opposing Self::min. Usually the right bottom one.

Implementations§

Source§

impl Rect

Source

pub const EVERYTHING: Rect

Infinite rectangle that contains every point.

Source

pub const NOTHING: Rect

The inverse of Self::EVERYTHING: stretches from positive infinity to negative infinity. Contains no points.

This is useful as the seed for bounding boxes.

§Example:
let mut rect = Rect::NOTHING;
assert!(rect.size() == Vec2::splat(-f32::INFINITY));
assert!(rect.contains(pos2(0.0, 0.0)) == false);
rect.extend_with(pos2(2.0, 1.0));
rect.extend_with(pos2(0.0, 3.0));
assert_eq!(rect, Rect::from_min_max(pos2(0.0, 1.0), pos2(2.0, 3.0)))
Source

pub const NAN: Rect

An invalid Rect filled with f32::NAN;

Source

pub const fn from_min_max(min: Pos2, max: Pos2) -> Rect

Source

pub fn from_min_size(min: Pos2, size: Vec2) -> Rect

left-top corner plus a size (stretching right-down).

Source

pub fn from_center_size(center: Pos2, size: Vec2) -> Rect

Source

pub fn from_x_y_ranges( x_range: RangeInclusive<f32>, y_range: RangeInclusive<f32>, ) -> Rect

Source

pub fn from_two_pos(a: Pos2, b: Pos2) -> Rect

Returns the bounding rectangle of the two points.

Source

pub fn from_points(points: &[Pos2]) -> Rect

Bounding-box around the points.

Source

pub fn everything_right_of(left_x: f32) -> Rect

A Rect that contains every point to the right of the given X coordinate.

Source

pub fn everything_left_of(right_x: f32) -> Rect

A Rect that contains every point to the left of the given X coordinate.

Source

pub fn everything_below(top_y: f32) -> Rect

A Rect that contains every point below a certain y coordinate

Source

pub fn everything_above(bottom_y: f32) -> Rect

A Rect that contains every point above a certain y coordinate

Source

pub fn expand(self, amnt: f32) -> Rect

Expand by this much in each direction, keeping the center

Source

pub fn expand2(self, amnt: Vec2) -> Rect

Expand by this much in each direction, keeping the center

Source

pub fn shrink(self, amnt: f32) -> Rect

Shrink by this much in each direction, keeping the center

Source

pub fn shrink2(self, amnt: Vec2) -> Rect

Shrink by this much in each direction, keeping the center

Source

pub fn translate(self, amnt: Vec2) -> Rect

Source

pub fn rotate_bb(self, rot: Rot2) -> Rect

Rotate the bounds (will expand the Rect)

Source

pub fn intersects(self, other: Rect) -> bool

Source

pub fn set_width(&mut self, w: f32)

keep min

Source

pub fn set_height(&mut self, h: f32)

keep min

Source

pub fn set_center(&mut self, center: Pos2)

Keep size

Source

pub fn contains(&self, p: Pos2) -> bool

Source

pub fn contains_rect(&self, other: Rect) -> bool

Source

pub fn clamp(&self, p: Pos2) -> Pos2

Return the given points clamped to be inside the rectangle Panics if Self::is_negative.

Source

pub fn extend_with(&mut self, p: Pos2)

Source

pub fn extend_with_x(&mut self, x: f32)

Expand to include the given x coordinate

Source

pub fn extend_with_y(&mut self, y: f32)

Expand to include the given y coordinate

Source

pub fn union(self, other: Rect) -> Rect

The union of two bounding rectangle, i.e. the minimum Rect that contains both input rectangles.

Source

pub fn intersect(self, other: Rect) -> Rect

The intersection of two Rect, i.e. the area covered by both.

Source

pub fn center(&self) -> Pos2

Source

pub fn size(&self) -> Vec2

Source

pub fn width(&self) -> f32

Source

pub fn height(&self) -> f32

Source

pub fn aspect_ratio(&self) -> f32

Width / height

  • aspect_ratio < 1: portrait / high
  • aspect_ratio = 1: square
  • aspect_ratio > 1: landscape / wide
Source

pub fn square_proportions(&self) -> Vec2

[2, 1] for wide screen, and [1, 2] for portrait, etc. At least one dimension = 1, the other >= 1 Returns the proportions required to letter-box a square view area.

Source

pub fn area(&self) -> f32

Source

pub fn distance_to_pos(&self, pos: Pos2) -> f32

The distance from the rect to the position.

The distance is zero when the position is in the interior of the rectangle.

Source

pub fn distance_sq_to_pos(&self, pos: Pos2) -> f32

The distance from the rect to the position, squared.

The distance is zero when the position is in the interior of the rectangle.

Source

pub fn signed_distance_to_pos(&self, pos: Pos2) -> f32

Signed distance to the edge of the box.

Negative inside the box.

Source

pub fn lerp(&self, t: Vec2) -> Pos2

Linearly interpolate so that [0, 0] is Self::min and [1, 1] is Self::max.

Source

pub fn x_range(&self) -> RangeInclusive<f32>

Source

pub fn y_range(&self) -> RangeInclusive<f32>

Source

pub fn bottom_up_range(&self) -> RangeInclusive<f32>

Source

pub fn is_negative(&self) -> bool

width < 0 || height < 0

Source

pub fn is_positive(&self) -> bool

width > 0 && height > 0

Source

pub fn is_finite(&self) -> bool

True if all members are also finite.

Source

pub fn any_nan(self) -> bool

True if any member is NaN.

Source§

impl Rect

§Convenience functions (assumes origin is towards left top):
Source

pub fn left(&self) -> f32

min.x

Source

pub fn left_mut(&mut self) -> &mut f32

min.x

Source

pub fn set_left(&mut self, x: f32)

min.x

Source

pub fn right(&self) -> f32

max.x

Source

pub fn right_mut(&mut self) -> &mut f32

max.x

Source

pub fn set_right(&mut self, x: f32)

max.x

Source

pub fn top(&self) -> f32

min.y

Source

pub fn top_mut(&mut self) -> &mut f32

min.y

Source

pub fn set_top(&mut self, y: f32)

min.y

Source

pub fn bottom(&self) -> f32

max.y

Source

pub fn bottom_mut(&mut self) -> &mut f32

max.y

Source

pub fn set_bottom(&mut self, y: f32)

max.y

Source

pub fn left_top(&self) -> Pos2

Source

pub fn center_top(&self) -> Pos2

Source

pub fn right_top(&self) -> Pos2

Source

pub fn left_center(&self) -> Pos2

Source

pub fn right_center(&self) -> Pos2

Source

pub fn left_bottom(&self) -> Pos2

Source

pub fn center_bottom(&self) -> Pos2

Source

pub fn right_bottom(&self) -> Pos2

Trait Implementations§

Source§

impl Clone for Rect

Source§

fn clone(&self) -> Rect

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

Source§

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

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

impl From<[Pos2; 2]> for Rect

from (min, max) or (left top, right bottom)

Source§

fn from(_: [Pos2; 2]) -> Rect

Converts to this type from the input type.
Source§

impl PartialEq for Rect

Source§

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

Source§

fn zeroed() -> Self

Source§

impl Copy for Rect

Source§

impl Eq for Rect

Source§

impl Pod for Rect

Source§

impl StructuralPartialEq for Rect

Auto Trait Implementations§

§

impl Freeze for Rect

§

impl RefUnwindSafe for Rect

§

impl Send for Rect

§

impl Sync for Rect

§

impl Unpin for Rect

§

impl UnwindSafe for Rect

Blanket Implementations§

Source§

impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
where T: Real + Zero + Arithmetics + Clone, Swp: WhitePoint<T>, Dwp: WhitePoint<T>, D: AdaptFrom<S, Swp, Dwp, T>,

Source§

fn adapt_into_using<M>(self, method: M) -> D
where M: TransformMatrix<T>,

Convert the source color to the destination color using the specified method.
Source§

fn adapt_into(self) -> D

Convert the source color to the destination color using the bradford method by default.
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, C> ArraysFrom<C> for T
where C: IntoArrays<T>,

Source§

fn arrays_from(colors: C) -> T

Cast a collection of colors into a collection of arrays.
Source§

impl<T, C> ArraysInto<C> for T
where C: FromArrays<T>,

Source§

fn arrays_into(self) -> C

Cast this collection of arrays into a collection of colors.
Source§

impl<T> AsAny for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Source§

fn type_name(&self) -> &'static str

Gets the type name of self
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<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for U
where T: FromCam16Unclamped<WpParam, U>,

Source§

type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
Source§

fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
Source§

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

Source§

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> 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, C> ComponentsFrom<C> for T
where C: IntoComponents<T>,

Source§

fn components_from(colors: C) -> T

Cast a collection of colors into a collection of color components.
Source§

impl<T> Downcast for T
where T: AsAny + ?Sized,

Source§

fn is<T>(&self) -> bool
where T: AsAny,

Returns true if the boxed type is the same as T. Read more
Source§

fn downcast_ref<T>(&self) -> Option<&T>
where T: AsAny,

Forward to the method defined on the type Any.
Source§

fn downcast_mut<T>(&mut self) -> Option<&mut T>
where T: AsAny,

Forward to the method defined on the type Any.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromAngle<T> for T

Source§

fn from_angle(angle: T) -> T

Performs a conversion from angle.
Source§

impl<T, U> FromStimulus<U> for T
where U: IntoStimulus<T>,

Source§

fn from_stimulus(other: U) -> T

Converts other into Self, while performing the appropriate scaling, rounding and clamping.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, U> IntoAngle<U> for T
where U: FromAngle<T>,

Source§

fn into_angle(self) -> U

Performs a conversion into T.
Source§

impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for U
where T: Cam16FromUnclamped<WpParam, U>,

Source§

type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
Source§

fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
Source§

impl<T, U> IntoColor<U> for T
where U: FromColor<T>,

Source§

fn into_color(self) -> U

Convert into T with values clamped to the color defined bounds Read more
Source§

impl<T, U> IntoColorUnclamped<U> for T
where U: FromColorUnclamped<T>,

Source§

fn into_color_unclamped(self) -> U

Convert into T. The resulting color might be invalid in its color space Read more
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> IntoStimulus<T> for T

Source§

fn into_stimulus(self) -> T

Converts self into T, while performing the appropriate scaling, rounding and clamping.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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, C> TryComponentsInto<C> for T
where C: TryFromComponents<T>,

Source§

type Error = <C as TryFromComponents<T>>::Error

The error for when try_into_colors fails to cast.
Source§

fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>

Try to cast this collection of color components into a collection of colors. 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.
Source§

impl<T, U> TryIntoColor<U> for T
where U: TryFromColor<T>,

Source§

fn try_into_color(self) -> Result<U, OutOfBounds<U>>

Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more
Source§

impl<C, U> UintsFrom<C> for U
where C: IntoUints<U>,

Source§

fn uints_from(colors: C) -> U

Cast a collection of colors into a collection of unsigned integers.
Source§

impl<C, U> UintsInto<C> for U
where C: FromUints<U>,

Source§

fn uints_into(self) -> C

Cast this collection of unsigned integers into a collection of colors.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

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

Source§

impl<T> Component for T
where T: Send + Sync + 'static,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<E> Event for E
where E: Clone + Send + Sync + 'static,

Source§

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

Source§

impl<T> Resource for T
where T: AsAny + Send + Sync + 'static,

Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,

Source§

impl<T> SerializableAny for T
where T: 'static + Any + Clone + for<'a> Send + Sync,