pub struct Rect<T, S = T> {
pub start: Vec2<T>,
pub size: Vec2<S>,
}
Expand description
A rectangle in a plane.
Fields§
§start: Vec2<T>
Starting top-left point.
size: Vec2<S>
Size at each dimension.
Implementations§
source§impl<T, S> Rect<T, S>
impl<T, S> Rect<T, S>
sourcepub fn from_range<U>(start: Vec2<T>, end: Vec2<U>) -> Selfwhere
T: Clone,
U: Sub<T, Output = S>,
pub fn from_range<U>(start: Vec2<T>, end: Vec2<U>) -> Selfwhere T: Clone, U: Sub<T, Output = S>,
Builds the rectangle from the given range start .. end
(i.e. end
excluded).
Examples
use gardiz::{rect::Rect, coord::Vec2};
let built_from_range = Rect::<u16>::from_range(
Vec2 { x: 5, y: 3 },
Vec2 { x: 7, y: 9 },
);
let actual = Rect {
start: Vec2 { x: 5, y: 3 },
size: Vec2 { x: 2, y: 6 },
};
assert_eq!(built_from_range, actual);
source§impl<T> Rect<T>
impl<T> Rect<T>
sourcepub fn try_from_range(start: Vec2<T>, end: Vec2<T>) -> Option<Self>where
T: Clone + CheckedSub,
pub fn try_from_range(start: Vec2<T>, end: Vec2<T>) -> Option<Self>where T: Clone + CheckedSub,
Tries to make a rectangle from a given range (end excluded), and returns
None
if overflows.
source§impl<T, S> Rect<T, S>
impl<T, S> Rect<T, S>
sourcepub fn from_range_incl<Z>(start: Vec2<T>, end: Vec2<T>) -> Selfwhere
T: Sub<Output = Z> + Clone + Ord,
Z: One + Add<Output = S>,
S: Zero,
pub fn from_range_incl<Z>(start: Vec2<T>, end: Vec2<T>) -> Selfwhere T: Sub<Output = Z> + Clone + Ord, Z: One + Add<Output = S>, S: Zero,
Builds the rectangle from the given inclusive range start ..= end
(i.e. end included).
Examples
use gardiz::{rect::Rect, coord::Vec2};
let built_from_range = Rect::<u16>::from_range_incl(
Vec2 { x: 5, y: 3 },
Vec2 { x: 6, y: 8 },
);
let actual = Rect {
start: Vec2 { x: 5, y: 3 },
size: Vec2 { x: 2, y: 6 },
};
assert_eq!(built_from_range, actual);
source§impl<T> Rect<T>
impl<T> Rect<T>
sourcepub fn try_from_range_incl(start: Vec2<T>, end: Vec2<T>) -> Option<Self>where
T: CheckedAdd + CheckedSub + One + Zero + Ord + Clone,
pub fn try_from_range_incl(start: Vec2<T>, end: Vec2<T>) -> Option<Self>where T: CheckedAdd + CheckedSub + One + Zero + Ord + Clone,
Tries to make a rectangle from a given range (end included), and returns
None
if overflows.
source§impl<T, S> Rect<T, S>
impl<T, S> Rect<T, S>
sourcepub fn is_empty(&self) -> boolwhere
S: Zero,
pub fn is_empty(&self) -> boolwhere S: Zero,
Returns whether the rectangle is empty (i.e. size is zero).
sourcepub fn end(self) -> Vec2<T::Output>where
T: Add<S>,
pub fn end(self) -> Vec2<T::Output>where T: Add<S>,
Returns coordinates one unit past the end (bottom-right) of the rectangle, i.e. the end excluded from the rectangle.
Examples
use gardiz::{rect::Rect, coord::Vec2};
let rectangle: Rect<u16> = Rect {
start: Vec2 { x: 5, y: 3 },
size: Vec2 { x: 2, y: 6 },
};
assert_eq!(rectangle.end(), Vec2 { x: 7, y: 9 });
source§impl<T> Rect<T>
impl<T> Rect<T>
sourcepub fn wrapping_end(&self) -> Vec2<T>where
T: WrappingAdd,
pub fn wrapping_end(&self) -> Vec2<T>where T: WrappingAdd,
Returns coordinates one unit past the end (bottom-right), wrapping around on overflow.
sourcepub fn saturating_end(&self) -> Vec2<T>where
T: SaturatingAdd,
pub fn saturating_end(&self) -> Vec2<T>where T: SaturatingAdd,
Returns coordinates one unit past the end (bottom-right), saturating on overflow.
sourcepub fn checked_end(&self) -> Option<Vec2<T>>where
T: CheckedAdd,
pub fn checked_end(&self) -> Option<Vec2<T>>where T: CheckedAdd,
Returns coordinates one unit past the end (bottom-right), returning
None
on overflow.
source§impl<T, S> Rect<T, S>
impl<T, S> Rect<T, S>
sourcepub fn end_ref<'this, U>(&'this self) -> Vec2<U>where
&'this T: Add<&'this S, Output = U>,
pub fn end_ref<'this, U>(&'this self) -> Vec2<U>where &'this T: Add<&'this S, Output = U>,
Returns coordinates one unit past the end (bottom-right), but without taking the rectangle (by reference).
sourcepub fn end_inclusive<U, V>(self) -> Vec2<V>where
S: Sub<Output = U> + One + Zero,
T: Add<U, Output = V> + Sub<Output = V> + One,
pub fn end_inclusive<U, V>(self) -> Vec2<V>where S: Sub<Output = U> + One + Zero, T: Add<U, Output = V> + Sub<Output = V> + One,
Returns the last coordinates (bottom-right) of the rectangle, i.e. returns an included end.
Examples
use gardiz::{rect::Rect, coord::Vec2};
let rectangle: Rect<u16> = Rect {
start: Vec2 { x: 5, y: 3 },
size: Vec2 { x: 2, y: 6 },
};
assert_eq!(rectangle.end_inclusive(), Vec2 { x: 6, y: 8 });
source§impl<T> Rect<T>
impl<T> Rect<T>
sourcepub fn wrapping_end_incl(&self) -> Vec2<T>where
T: WrappingAdd + WrappingSub + One,
pub fn wrapping_end_incl(&self) -> Vec2<T>where T: WrappingAdd + WrappingSub + One,
Returns the last coordinates (bottom-right) of the rectangle, wrapping around on overflow.
sourcepub fn saturating_end_incl(&self) -> Vec2<T>where
T: SaturatingAdd + SaturatingSub + One + Zero,
pub fn saturating_end_incl(&self) -> Vec2<T>where T: SaturatingAdd + SaturatingSub + One + Zero,
Returns the last coordinates (bottom-right) of the rectangle, saturating on overflow.
sourcepub fn checked_end_incl(&self) -> Option<Vec2<T>>where
T: CheckedAdd + CheckedSub + One + Zero,
pub fn checked_end_incl(&self) -> Option<Vec2<T>>where T: CheckedAdd + CheckedSub + One + Zero,
Returns the last coordinates (bottom-right) of the rectangle, returning
None
on overflow.
source§impl<T, S> Rect<T, S>
impl<T, S> Rect<T, S>
sourcepub fn end_incl_ref<'this, U, V>(&'this self) -> Vec2<V>where
&'this S: Sub<S, Output = U>,
S: One + Zero,
&'this T: Add<U, Output = V> + Sub<T, Output = V>,
T: One,
pub fn end_incl_ref<'this, U, V>(&'this self) -> Vec2<V>where &'this S: Sub<S, Output = U>, S: One + Zero, &'this T: Add<U, Output = V> + Sub<T, Output = V>, T: One,
Returns the last coordinates (bottom-right) of the rectangle, but without taking the rectangle (by reference).
sourcepub fn end_non_empty<U, V>(self) -> Option<Vec2<V>>where
S: Sub<Output = U> + One + Zero,
T: Add<U, Output = V> + Sub<Output = V> + One,
pub fn end_non_empty<U, V>(self) -> Option<Vec2<V>>where S: Sub<Output = U> + One + Zero, T: Add<U, Output = V> + Sub<Output = V> + One,
Returns last included coordinates of the rectangle (bottom-right),
but if the rectangle is empty, the output is None
. With this, it is
possible to extract the end of a “full rectangle”.
Examples
Actually Non-Empty
use gardiz::{rect::Rect, coord::Vec2};
let rectangle: Rect<u16> = Rect {
start: Vec2 { x: 5, y: 3 },
size: Vec2 { x: 2, y: 6 },
};
assert_eq!(rectangle.end_non_empty(), Some(Vec2 { x: 6, y: 8 }));
Empty
use gardiz::{rect::Rect, coord::Vec2};
let rectangle: Rect<u16> = Rect {
start: Vec2 { x: 5, y: 3 },
size: Vec2 { x: 0, y: 6 },
};
assert_eq!(rectangle.end_non_empty(), None);
sourcepub fn end_non_empty_ref<'this, U, V>(&'this self) -> Option<Vec2<V>>where
&'this S: Sub<S, Output = U>,
S: One + Zero,
&'this T: Add<U, Output = V> + Sub<T, Output = V>,
T: One,
pub fn end_non_empty_ref<'this, U, V>(&'this self) -> Option<Vec2<V>>where &'this S: Sub<S, Output = U>, S: One + Zero, &'this T: Add<U, Output = V> + Sub<T, Output = V>, T: One,
Returns last included coordinates of the rectangle (bottom-right), but
if the rectangle is empty, the output is None
, and without taking the
rectangle, computing by reference instead.
sourcepub fn has_point<'this, U>(&'this self, point: Vec2<T>) -> boolwhere
&'this S: Sub<S, Output = U>,
S: One + Zero,
&'this T: Add<U, Output = T> + Sub<T, Output = T>,
T: Sub<&'this T> + One + Ord,
pub fn has_point<'this, U>(&'this self, point: Vec2<T>) -> boolwhere &'this S: Sub<S, Output = U>, S: One + Zero, &'this T: Add<U, Output = T> + Sub<T, Output = T>, T: Sub<&'this T> + One + Ord,
Tests whether a given point is inside the rectangle.
sourcepub fn overlaps<'params, U>(&'params self, other: &'params Self) -> boolwhere
&'params S: Sub<S, Output = U>,
S: One + Zero,
&'params T: Add<U, Output = T> + Sub<T, Output = T>,
T: Sub<&'params T> + One + Ord,
pub fn overlaps<'params, U>(&'params self, other: &'params Self) -> boolwhere &'params S: Sub<S, Output = U>, S: One + Zero, &'params T: Add<U, Output = T> + Sub<T, Output = T>, T: Sub<&'params T> + One + Ord,
Tests whether two rectangles overlap in area.
sourcepub fn overlapped<'params, U, Z>(
&'params self,
other: &'params Self
) -> Rect<T, <Z as Add>::Output>where
&'params S: Sub<S, Output = U>,
S: One + Zero,
&'params T: Add<U, Output = T> + Sub<T, Output = T>,
T: Sub<&'params T, Output = Z> + One + Ord + Clone,
Z: One + Add,
pub fn overlapped<'params, U, Z>( &'params self, other: &'params Self ) -> Rect<T, <Z as Add>::Output>where &'params S: Sub<S, Output = U>, S: One + Zero, &'params T: Add<U, Output = T> + Sub<T, Output = T>, T: Sub<&'params T, Output = Z> + One + Ord + Clone, Z: One + Add,
Computes the overlapped area between two rectangles. An empty rectange is produced if both do not overlap.
Examples
use gardiz::{rect::Rect, coord::Vec2};
let left: Rect<u16> = Rect {
start: Vec2 { x: 5, y: 3 },
size: Vec2 { x: 10, y: 7 },
};
let right = Rect {
start: Vec2 { x: 9, y: 4 },
size: Vec2 { x: 9, y: 5 },
};
let overlapped = Rect {
start: Vec2 { x: 9, y: 4 },
size: Vec2 { x: 6, y: 5 },
};
assert_eq!(left.overlapped(&right), overlapped);
source§impl<T> Rect<T>
impl<T> Rect<T>
sourcepub fn wrapping_overlapped<'params>(&'params self, other: &'params Self) -> Selfwhere
T: WrappingAdd + WrappingSub + Ord + Clone,
pub fn wrapping_overlapped<'params>(&'params self, other: &'params Self) -> Selfwhere T: WrappingAdd + WrappingSub + Ord + Clone,
Computes overlapped area between two rectangles, wrapping around on overflow.
sourcepub fn saturating_overlapped<'params>(
&'params self,
other: &'params Self
) -> Selfwhere
T: SaturatingAdd + SaturatingSub + One + Zero + Ord + Clone,
pub fn saturating_overlapped<'params>( &'params self, other: &'params Self ) -> Selfwhere T: SaturatingAdd + SaturatingSub + One + Zero + Ord + Clone,
Computes overlapped area between two rectangles, saturating on overflow.
sourcepub fn checked_overlapped<'params>(
&'params self,
other: &'params Self
) -> Option<Self>where
T: CheckedAdd + CheckedSub + One + Zero + Ord + Clone,
pub fn checked_overlapped<'params>( &'params self, other: &'params Self ) -> Option<Self>where T: CheckedAdd + CheckedSub + One + Zero + Ord + Clone,
Computes overlapped area between two rectangles, returning None
on
overflow.
source§impl<T, S> Rect<T, S>
impl<T, S> Rect<T, S>
sourcepub fn columns<'this>(&'this self) -> Columns<T> ⓘwhere
&'this S: Sub<S, Output = T>,
S: One + Zero,
&'this T: Add<T, Output = T> + Sub<T, Output = T> + Add<&'this S, Output = T>,
T: One + AddAssign + Ord + Clone,
pub fn columns<'this>(&'this self) -> Columns<T> ⓘwhere &'this S: Sub<S, Output = T>, S: One + Zero, &'this T: Add<T, Output = T> + Sub<T, Output = T> + Add<&'this S, Output = T>, T: One + AddAssign + Ord + Clone,
Iterator over all coordinates of this rectangle in the direction of columns.
Trait Implementations§
source§impl<'de, T, S> Deserialize<'de> for Rect<T, S>where
T: Deserialize<'de>,
S: Deserialize<'de>,
impl<'de, T, S> Deserialize<'de> for Rect<T, S>where T: Deserialize<'de>, S: Deserialize<'de>,
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl<T: Ord, S: Ord> Ord for Rect<T, S>
impl<T: Ord, S: Ord> Ord for Rect<T, S>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl<T: PartialEq, S: PartialEq> PartialEq<Rect<T, S>> for Rect<T, S>
impl<T: PartialEq, S: PartialEq> PartialEq<Rect<T, S>> for Rect<T, S>
source§impl<T: PartialOrd, S: PartialOrd> PartialOrd<Rect<T, S>> for Rect<T, S>
impl<T: PartialOrd, S: PartialOrd> PartialOrd<Rect<T, S>> for Rect<T, S>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more