#[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.
Rect
does NOT implement Default
, because there is no obvious default value.
Rect::ZERO
may seem reasonable, but when used as a bounding box, Rect::NOTHING
is a better default - so be explicit instead!
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§
§impl Rect
impl Rect
pub const EVERYTHING: Rect = _
pub const EVERYTHING: Rect = _
Infinite rectangle that contains every point.
pub const NOTHING: Rect = _
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)))
pub const fn from_min_max(min: Pos2, max: Pos2) -> Rect
pub fn from_min_size(min: Pos2, size: Vec2) -> Rect
pub fn from_min_size(min: Pos2, size: Vec2) -> Rect
left-top corner plus a size (stretching right-down).
pub fn from_center_size(center: Pos2, size: Vec2) -> Rect
pub fn from_x_y_ranges( x_range: impl Into<Rangef>, y_range: impl Into<Rangef>, ) -> Rect
pub fn from_two_pos(a: Pos2, b: Pos2) -> Rect
pub fn from_two_pos(a: Pos2, b: Pos2) -> Rect
Returns the bounding rectangle of the two points.
pub fn from_points(points: &[Pos2]) -> Rect
pub fn from_points(points: &[Pos2]) -> Rect
Bounding-box around the points.
pub fn everything_right_of(left_x: f32) -> Rect
pub fn everything_right_of(left_x: f32) -> Rect
A Rect
that contains every point to the right of the given X coordinate.
pub fn everything_left_of(right_x: f32) -> Rect
pub fn everything_left_of(right_x: f32) -> Rect
A Rect
that contains every point to the left of the given X coordinate.
pub fn everything_below(top_y: f32) -> Rect
pub fn everything_below(top_y: f32) -> Rect
A Rect
that contains every point below a certain y coordinate
pub fn everything_above(bottom_y: f32) -> Rect
pub fn everything_above(bottom_y: f32) -> Rect
A Rect
that contains every point above a certain y coordinate
pub fn translate(self, amnt: Vec2) -> Rect
pub fn intersects(self, other: Rect) -> bool
pub fn set_height(&mut self, h: f32)
pub fn set_height(&mut self, h: f32)
keep min
pub fn set_center(&mut self, center: Pos2)
pub fn set_center(&mut self, center: Pos2)
Keep size
pub fn contains(&self, p: Pos2) -> bool
pub fn contains_rect(&self, other: Rect) -> bool
pub fn clamp(&self, p: Pos2) -> Pos2
pub fn clamp(&self, p: Pos2) -> Pos2
Return the given points clamped to be inside the rectangle
Panics if Self::is_negative
.
pub fn extend_with(&mut self, p: Pos2)
pub fn extend_with_x(&mut self, x: f32)
pub fn extend_with_x(&mut self, x: f32)
Expand to include the given x coordinate
pub fn extend_with_y(&mut self, y: f32)
pub fn extend_with_y(&mut self, y: f32)
Expand to include the given y coordinate
pub fn union(self, other: Rect) -> Rect
pub fn union(self, other: Rect) -> Rect
The union of two bounding rectangle, i.e. the minimum Rect
that contains both input rectangles.
pub fn intersect(self, other: Rect) -> Rect
pub fn intersect(self, other: Rect) -> Rect
The intersection of two Rect
, i.e. the area covered by both.
pub fn center(&self) -> Pos2
pub fn width(&self) -> f32
pub fn height(&self) -> f32
pub fn aspect_ratio(&self) -> f32
pub fn aspect_ratio(&self) -> f32
Width / height
aspect_ratio < 1
: portrait / highaspect_ratio = 1
: squareaspect_ratio > 1
: landscape / wide
pub fn square_proportions(&self) -> Vec2
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.
pub fn area(&self) -> f32
pub fn distance_to_pos(&self, pos: Pos2) -> f32
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.
pub fn distance_sq_to_pos(&self, pos: Pos2) -> f32
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.
pub fn signed_distance_to_pos(&self, pos: Pos2) -> f32
pub fn signed_distance_to_pos(&self, pos: Pos2) -> f32
Signed distance to the edge of the box.
Negative inside the box.
let rect = Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0));
assert_eq!(rect.signed_distance_to_pos(pos2(0.50, 0.50)), -0.50);
assert_eq!(rect.signed_distance_to_pos(pos2(0.75, 0.50)), -0.25);
assert_eq!(rect.signed_distance_to_pos(pos2(1.50, 0.50)), 0.50);
pub fn lerp_inside(&self, t: Vec2) -> Pos2
pub fn lerp_inside(&self, t: Vec2) -> Pos2
pub fn lerp_towards(&self, other: &Rect, t: f32) -> Rect
pub fn lerp_towards(&self, other: &Rect, t: f32) -> Rect
Linearly self towards other rect.
pub fn x_range(&self) -> Rangef
pub fn y_range(&self) -> Rangef
pub fn bottom_up_range(&self) -> Rangef
pub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
width < 0 || height < 0
pub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
width > 0 && height > 0
§impl Rect
impl Rect
§Convenience functions (assumes origin is towards left top):
pub fn bottom_mut(&mut self) -> &mut f32
pub fn bottom_mut(&mut self) -> &mut f32
max.y
pub fn set_bottom(&mut self, y: f32)
pub fn set_bottom(&mut self, y: f32)
max.y
pub fn left_top(&self) -> Pos2
pub fn center_top(&self) -> Pos2
pub fn right_top(&self) -> Pos2
pub fn left_center(&self) -> Pos2
pub fn right_center(&self) -> Pos2
pub fn left_bottom(&self) -> Pos2
pub fn center_bottom(&self) -> Pos2
pub fn right_bottom(&self) -> Pos2
pub fn split_left_right_at_fraction(&self, t: f32) -> (Rect, Rect)
pub fn split_left_right_at_fraction(&self, t: f32) -> (Rect, Rect)
Split rectangle in left and right halves. t
is expected to be in the (0,1) range.
pub fn split_left_right_at_x(&self, split_x: f32) -> (Rect, Rect)
pub fn split_left_right_at_x(&self, split_x: f32) -> (Rect, Rect)
Split rectangle in left and right halves at the given x
coordinate.
pub fn split_top_bottom_at_fraction(&self, t: f32) -> (Rect, Rect)
pub fn split_top_bottom_at_fraction(&self, t: f32) -> (Rect, Rect)
Split rectangle in top and bottom halves. t
is expected to be in the (0,1) range.
pub fn split_top_bottom_at_y(&self, split_y: f32) -> (Rect, Rect)
pub fn split_top_bottom_at_y(&self, split_y: f32) -> (Rect, Rect)
Split rectangle in top and bottom halves at the given y
coordinate.
Trait Implementations§
§impl<'de> Deserialize<'de> for Rect
impl<'de> Deserialize<'de> for Rect
§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Rect, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Rect, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
§impl Serialize for Rect
impl Serialize for Rect
§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl Copy for Rect
impl Eq for Rect
impl Pod for Rect
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§
§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
§fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
T
[ShaderType
] for self
. When used in [AsBindGroup
]
derives, it is safe to assume that all images in self
exist.§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
§type Bits = T
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
.§fn is_valid_bit_pattern(_bits: &T) -> bool
fn is_valid_bit_pattern(_bits: &T) -> bool
bits
as &Self
.source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> Conv for T
impl<T> Conv for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> DynEq for T
impl<T> DynEq for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
fn equivalent(&self, key: &K) -> bool
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<T> Serialize for T
impl<T> Serialize for T
fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<Ok, Error>
source§impl<T> Serialize for T
impl<T> Serialize for T
fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>
fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.