#[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>
impl<T: Unit> Rect<T>
sourcepub fn new(origin: impl Into<Point2<T>>, size: impl Into<Size2<T>>) -> Rect<T>
pub fn new(origin: impl Into<Point2<T>>, size: impl Into<Size2<T>>) -> Rect<T>
New Rect from origin/size.
sourcepub fn from_untyped(untyped: Rect<T::Scalar>) -> Rect<T>
pub fn from_untyped(untyped: Rect<T::Scalar>) -> Rect<T>
Bitcast an untyped instance to self.
sourcepub fn to_untyped(self) -> Rect<T::Scalar>
pub fn to_untyped(self) -> Rect<T::Scalar>
Bitcast self to an untyped instance.
sourcepub fn as_untyped(&self) -> &Rect<T::Scalar>
pub fn as_untyped(&self) -> &Rect<T::Scalar>
Reinterpret cast self as the untyped variant.
sourcepub fn as_untyped_mut(&mut self) -> &mut Rect<T::Scalar>
pub fn as_untyped_mut(&mut self) -> &mut Rect<T::Scalar>
Reinterpret cast self as the untyped variant.
sourcepub fn cast<T2>(self) -> Rect<T2>
pub fn cast<T2>(self) -> Rect<T2>
Cast to a different coordinate space with the same underlying scalar type.
sourcepub fn cast_ref<T2>(&self) -> &Rect<T2>
pub fn cast_ref<T2>(&self) -> &Rect<T2>
Cast to a different coordinate space with the same underlying scalar type.
sourcepub fn cast_mut<T2>(&mut self) -> &mut Rect<T2>
pub fn cast_mut<T2>(&mut self) -> &mut Rect<T2>
Cast to a different coordinate space with the same underlying scalar type.
sourcepub fn try_cast<T2>(self) -> Option<Rect<T2>>where
T2: Unit,
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.
sourcepub const fn from_tuple((origin, size): (Point2<T>, Size2<T>)) -> Self
pub const fn from_tuple((origin, size): (Point2<T>, Size2<T>)) -> Self
Instantiate from tuple.
sourcepub fn from_box(b: Box2<T>) -> Self
pub fn from_box(b: Box2<T>) -> Self
Create from Box2
.
Note: This may lose precision due to floating point arithmetic.
sourcepub fn from_min_max(min: Point2<T>, max: Point2<T>) -> Self
pub fn from_min_max(min: Point2<T>, max: Point2<T>) -> Self
Create from min/max points.
sourcepub fn from_points<I>(points: I) -> Selfwhere
I: IntoIterator<Item = Point2<T>>,
pub fn from_points<I>(points: I) -> Selfwhere
I: IntoIterator<Item = Point2<T>>,
Calculate the bounding rect that covers all points
.
sourcepub fn inflate(&self, by: Size2<T>) -> Self
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));
sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
True if size is negative or NaN.
source§impl<T> Rect<T>
impl<T> Rect<T>
sourcepub fn round(self) -> Self
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)));
sourcepub fn round_in(self) -> Self
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)));
sourcepub fn round_out(self) -> Self
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)));
Trait Implementations§
source§impl<T> AbsDiffEq<(Point2<T>, Size2<T>)> for Rect<T>
impl<T> AbsDiffEq<(Point2<T>, Size2<T>)> for Rect<T>
§type Epsilon = <<T as Unit>::Scalar as AbsDiffEq>::Epsilon
type Epsilon = <<T as Unit>::Scalar as AbsDiffEq>::Epsilon
source§fn default_epsilon() -> Self::Epsilon
fn default_epsilon() -> Self::Epsilon
source§fn abs_diff_eq(
&self,
other: &(Point2<T>, Size2<T>),
epsilon: Self::Epsilon
) -> bool
fn abs_diff_eq( &self, other: &(Point2<T>, Size2<T>), epsilon: Self::Epsilon ) -> bool
source§fn abs_diff_ne(
&self,
other: &(Point2<T>, Size2<T>),
epsilon: Self::Epsilon
) -> bool
fn abs_diff_ne( &self, other: &(Point2<T>, Size2<T>), epsilon: Self::Epsilon ) -> bool
AbsDiffEq::abs_diff_eq
.source§impl<T: Unit> AbsDiffEq for Rect<T>
impl<T: Unit> AbsDiffEq for Rect<T>
§type Epsilon = <<T as Unit>::Scalar as AbsDiffEq>::Epsilon
type Epsilon = <<T as Unit>::Scalar as AbsDiffEq>::Epsilon
source§fn default_epsilon() -> Self::Epsilon
fn default_epsilon() -> Self::Epsilon
source§fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool
source§fn abs_diff_ne(&self, other: &Self, epsilon: Self::Epsilon) -> bool
fn abs_diff_ne(&self, other: &Self, epsilon: Self::Epsilon) -> bool
AbsDiffEq::abs_diff_eq
.source§impl<T: Unit> Intersection<Box2<T>> for Rect<T>
impl<T: Unit> Intersection<Box2<T>> for Rect<T>
§type Intersection = Rect<T>
type Intersection = Rect<T>
source§fn intersects(&self, thing: &Box2<T>) -> bool
fn intersects(&self, thing: &Box2<T>) -> bool
thing
intersects with self
.source§fn intersection(&self, thing: &Box2<T>) -> Option<Self::Intersection>
fn intersection(&self, thing: &Box2<T>) -> Option<Self::Intersection>
thing
intersects with self
, return the intersection. Otherwise,
returns None
.source§impl<T: Unit> Intersection<Point2<T>> for Rect<T>
impl<T: Unit> Intersection<Point2<T>> for Rect<T>
§type Intersection = Point2<T>
type Intersection = Point2<T>
source§fn intersects(&self, thing: &Point2<T>) -> bool
fn intersects(&self, thing: &Point2<T>) -> bool
thing
intersects with self
.source§fn intersection(&self, thing: &Point2<T>) -> Option<Self::Intersection>
fn intersection(&self, thing: &Point2<T>) -> Option<Self::Intersection>
thing
intersects with self
, return the intersection. Otherwise,
returns None
.source§impl<T: Unit> Intersection for Rect<T>
impl<T: Unit> Intersection for Rect<T>
§type Intersection = Rect<T>
type Intersection = Rect<T>
source§fn intersects(&self, thing: &Rect<T>) -> bool
fn intersects(&self, thing: &Rect<T>) -> bool
thing
intersects with self
.source§fn intersection(&self, thing: &Rect<T>) -> Option<Self::Intersection>
fn intersection(&self, thing: &Rect<T>) -> Option<Self::Intersection>
thing
intersects with self
, return the intersection. Otherwise,
returns None
.source§impl<T: Unit> PartialEq<(Point2<T>, Size2<T>)> for Rect<T>
impl<T: Unit> PartialEq<(Point2<T>, Size2<T>)> for Rect<T>
source§impl<T: Unit> PartialEq for Rect<T>
impl<T: Unit> PartialEq for Rect<T>
source§impl<T> RelativeEq<(Point2<T>, Size2<T>)> for Rect<T>
impl<T> RelativeEq<(Point2<T>, Size2<T>)> for Rect<T>
source§fn default_max_relative() -> Self::Epsilon
fn default_max_relative() -> Self::Epsilon
source§fn relative_eq(
&self,
other: &(Point2<T>, Size2<T>),
epsilon: Self::Epsilon,
max_relative: Self::Epsilon
) -> bool
fn relative_eq( &self, other: &(Point2<T>, Size2<T>), epsilon: Self::Epsilon, max_relative: Self::Epsilon ) -> bool
source§fn relative_ne(
&self,
other: &(Point2<T>, Size2<T>),
epsilon: Self::Epsilon,
max_relative: Self::Epsilon
) -> bool
fn relative_ne( &self, other: &(Point2<T>, Size2<T>), epsilon: Self::Epsilon, max_relative: Self::Epsilon ) -> bool
RelativeEq::relative_eq
.source§impl<T: Unit> RelativeEq for Rect<T>where
T::Scalar: RelativeEq,
impl<T: Unit> RelativeEq for Rect<T>where
T::Scalar: RelativeEq,
source§fn default_max_relative() -> Self::Epsilon
fn default_max_relative() -> Self::Epsilon
source§fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon
) -> bool
fn relative_eq( &self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon ) -> bool
source§fn relative_ne(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon
) -> bool
fn relative_ne( &self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon ) -> bool
RelativeEq::relative_eq
.source§impl<T: Unit> UlpsEq<(Point2<T>, Size2<T>)> for Rect<T>
impl<T: Unit> UlpsEq<(Point2<T>, Size2<T>)> for Rect<T>
source§fn default_max_ulps() -> u32
fn default_max_ulps() -> u32
source§impl<T: Unit> UlpsEq for Rect<T>
impl<T: Unit> UlpsEq for Rect<T>
impl<T: Unit> Copy for Rect<T>
impl<T> Eq for Rect<T>
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>
impl<T> RefUnwindSafe for Rect<T>
impl<T> Send for Rect<T>
impl<T> Sync for Rect<T>
impl<T> Unpin for Rect<T>
impl<T> UnwindSafe for Rect<T>
Blanket Implementations§
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
source§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
.source§fn is_valid_bit_pattern(_bits: &T) -> bool
fn is_valid_bit_pattern(_bits: &T) -> bool
bits
as &Self
.