pub struct Rect<P, E> {
    pub x: P,
    pub y: P,
    pub w: E,
    pub h: E,
}
Expand description

2D rectangle, represented by a bottom-left position, and 2D extents.

Most operations on a Rect are better done by converting it to Aabr form. In fact, most existing code in the wild implicitly does this and so does this crate.

Aabr structs are often more convenient, faster and probably less confusing. The Rect type is provided because it exists for a lot of APIs (including some system APIs, OpenGL, and others).

Please note that in most APIs in the wild (but NOT in vek), the Y axis is treated as pointing downwards because it’s the one of window space.

Keeping the Y axis upwards is a lot more convenient implementation-wise, and still matches the convention used in 3D spaces.

Fields§

§x: P

X position of the bottom-left corner.

§y: P

Y position of the bottom-left corner.

§w: E

Width.

§h: E

Height, with Y axis going upwards.

Implementations§

source§

impl<P, E> Rect<P, E>

source

pub fn new(x: P, y: P, w: E, h: E) -> Rect<P, E>

Creates a new rectangle from position elements and extent elements.

source

pub fn position(self) -> Vec2<P>

Gets this rectangle’s position.

source

pub fn set_position(&mut self, p: Vec2<P>)

Sets this rectangle’s position.

source

pub fn extent(self) -> Extent2<E>

Gets this rectangle’s extent (size).

source

pub fn set_extent(&mut self, e: Extent2<E>)

Sets this rectangle’s extent (size).

source

pub fn position_extent(self) -> (Vec2<P>, Extent2<E>)

Gets this rectangle’s position and extent (size).

source

pub fn map<DP, DE, PF, EF>(self, pf: PF, ef: EF) -> Rect<DP, DE>where PF: FnMut(P) -> DP, EF: FnMut(E) -> DE,

Returns this rectangle, converted with the given closures (one for position elements, the other for extent elements).

source

pub fn as_<DP, DE>(self) -> Rect<DP, DE>where P: AsPrimitive<DP>, DP: 'static + Copy, E: AsPrimitive<DE>, DE: 'static + Copy,

Converts this rectangle to a rectangle of another type, using the as conversion.

source§

impl<T> Rect<T, T>where T: Copy + Add<T, Output = T>,

source

pub fn into_aabr(self) -> Aabr<T>

Converts this into the matching axis-aligned bounding shape representation.

source

pub fn contains_point(self, p: Vec2<T>) -> boolwhere T: PartialOrd<T>,

Does this rectangle contain the given point ?

source

pub fn contains_rect(self, other: Rect<T, T>) -> boolwhere T: PartialOrd<T>,

Does this rectangle fully contain the given one ?

source

pub fn collides_with_rect(self, other: Rect<T, T>) -> boolwhere T: PartialOrd<T>,

Does this rectangle collide with another ?

source

pub fn center(self) -> Vec2<T>where T: One + Div<T, Output = T>,

Gets this rectangle’s center.

source§

impl<T> Rect<T, T>where T: PartialOrd<T> + Sub<T, Output = T> + Add<T, Output = T> + Copy,

source

pub fn expanded_to_contain_point(self, p: Vec2<T>) -> Rect<T, T>where T: PartialOrd<T>,

Returns this shape so that it contains the given point.

source

pub fn expand_to_contain_point(&mut self, p: Vec2<T>)where T: PartialOrd<T>,

Expands this shape so that it contains the given point.

source

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

Gets the smallest rectangle that contains both this one and another.

source

pub fn intersection(self, other: Rect<T, T>) -> Rect<T, T>

Gets the largest rectangle contained by both this one and another.

source

pub fn expand_to_contain(&mut self, other: Rect<T, T>)

Sets this rectangle to the union of itself with another.

source

pub fn intersect(&mut self, other: Rect<T, T>)

Sets this rectangle to the intersection of itself with another.

source

pub fn collision_vector_with_rect(self, other: Rect<T, T>) -> Vec2<T>where T: One + Div<T, Output = T>,

Gets a vector that tells how much self penetrates other.

source

pub fn split_at_x(self, sp: T) -> [Rect<T, T>; 2]

Splits this shape in two, by a straight plane along the x axis. The returned tuple is (low, high).

Panics

sp is assumed to be a position along the x axis that is within this shape’s bounds.

source

pub fn split_at_y(self, sp: T) -> [Rect<T, T>; 2]

Splits this shape in two, by a straight plane along the y axis. The returned tuple is (low, high).

Panics

sp is assumed to be a position along the y axis that is within this shape’s bounds.

Trait Implementations§

source§

impl<P, E> Clone for Rect<P, E>where P: Clone, E: Clone,

source§

fn clone(&self) -> Rect<P, E>

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<P, E> Debug for Rect<P, E>where P: Debug, E: Debug,

source§

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

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

impl<P, E> Default for Rect<P, E>where P: Default, E: Default,

source§

fn default() -> Rect<P, E>

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

impl<'de, P, E> Deserialize<'de> for Rect<P, E>where P: Deserialize<'de>, E: Deserialize<'de>,

source§

fn deserialize<__D>( __deserializer: __D ) -> Result<Rect<P, E>, <__D as Deserializer<'de>>::Error>where __D: Deserializer<'de>,

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

impl<P, E> From<(Vec2<P>, Extent2<E>)> for Rect<P, E>

source§

fn from(t: (Vec2<P>, Extent2<E>)) -> Rect<P, E>

Converts to this type from the input type.
source§

impl<T> From<Aabr<T>> for Rect<T, T>where T: Copy + Sub<T, Output = T>,

source§

fn from(aab: Aabr<T>) -> Rect<T, T>

Converts to this type from the input type.
source§

impl<T> From<Rect<T, T>> for Aabr<T>where T: Copy + Add<T, Output = T>,

source§

fn from(rect: Rect<T, T>) -> Aabr<T>

Converts to this type from the input type.
source§

impl<P, E> Hash for Rect<P, E>where P: Hash, E: Hash,

source§

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

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<P, E> PartialEq<Rect<P, E>> for Rect<P, E>where P: PartialEq<P>, E: PartialEq<E>,

source§

fn eq(&self, other: &Rect<P, E>) -> 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<P, E> Serialize for Rect<P, E>where P: Serialize, E: Serialize,

source§

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

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

impl<P, E> Copy for Rect<P, E>where P: Copy, E: Copy,

source§

impl<P, E> Eq for Rect<P, E>where P: Eq, E: Eq,

source§

impl<P, E> StructuralEq for Rect<P, E>

source§

impl<P, E> StructuralPartialEq for Rect<P, E>

Auto Trait Implementations§

§

impl<P, E> RefUnwindSafe for Rect<P, E>where E: RefUnwindSafe, P: RefUnwindSafe,

§

impl<P, E> Send for Rect<P, E>where E: Send, P: Send,

§

impl<P, E> Sync for Rect<P, E>where E: Sync, P: Sync,

§

impl<P, E> Unpin for Rect<P, E>where E: Unpin, P: Unpin,

§

impl<P, E> UnwindSafe for Rect<P, E>where E: UnwindSafe, P: 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,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
§

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

§

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

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

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

§

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

Compare self to key and return true if they are equal.
§

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

§

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

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

impl<T> Finalize for T

§

unsafe fn finalize_raw(data: *mut ())

Safety Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Initialize for Twhere T: Default,

§

fn initialize(&mut self)

§

unsafe fn initialize_raw(data: *mut ())

Safety Read more
source§

impl<T, U> Into<U> for Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
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.
source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

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

source§

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