Struct gardiz::rect::Rect

source ·
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>

source

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>

source

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>

source

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>

source

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>

source

pub fn is_empty(&self) -> boolwhere S: Zero,

Returns whether the rectangle is empty (i.e. size is zero).

source

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>

source

pub fn wrapping_end(&self) -> Vec2<T>where T: WrappingAdd,

Returns coordinates one unit past the end (bottom-right), wrapping around on overflow.

source

pub fn saturating_end(&self) -> Vec2<T>where T: SaturatingAdd,

Returns coordinates one unit past the end (bottom-right), saturating on overflow.

source

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>

source

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).

source

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>

source

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.

source

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.

source

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>

source

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).

source

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);
source

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.

source

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.

source

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.

source

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>

source

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.

source

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.

source

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>

source

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.

source

pub fn rows<'this>(&'this self) -> Rows<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 .

source

pub fn borders<'this>(&'this self) -> Borders<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: AddAssign + One + Ord + Clone,

Iterator over the inner borders of this rectangle.

Trait Implementations§

source§

impl<T: Clone, S: Clone> Clone for Rect<T, S>

source§

fn clone(&self) -> Rect<T, S>

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: Debug, S: Debug> Debug for Rect<T, S>

source§

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

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

impl<T: Default, S: Default> Default for Rect<T, S>

source§

fn default() -> Rect<T, S>

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

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>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T: Hash, S: Hash> Hash for Rect<T, S>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

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: Ord, S: Ord> Ord for Rect<T, S>

source§

fn cmp(&self, other: &Rect<T, S>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl<T: PartialEq, S: PartialEq> PartialEq<Rect<T, S>> for Rect<T, S>

source§

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

source§

fn partial_cmp(&self, other: &Rect<T, S>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T, S> Serialize for Rect<T, S>where T: Serialize, S: Serialize,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<T: Copy, S: Copy> Copy for Rect<T, S>

source§

impl<T: Eq, S: Eq> Eq for Rect<T, S>

source§

impl<T, S> StructuralEq for Rect<T, S>

source§

impl<T, S> StructuralPartialEq for Rect<T, S>

Auto Trait Implementations§

§

impl<T, S> RefUnwindSafe for Rect<T, S>where S: RefUnwindSafe, T: RefUnwindSafe,

§

impl<T, S> Send for Rect<T, S>where S: Send, T: Send,

§

impl<T, S> Sync for Rect<T, S>where S: Sync, T: Sync,

§

impl<T, S> Unpin for Rect<T, S>where S: Unpin, T: Unpin,

§

impl<T, S> UnwindSafe for Rect<T, S>where S: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Twhere 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,