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>
impl<P, E> Rect<P, E>
sourcepub fn new(x: P, y: P, w: E, h: E) -> Self
pub fn new(x: P, y: P, w: E, h: E) -> Self
Creates a new rectangle from position elements and extent elements.
sourcepub fn set_position(&mut self, p: Vec2<P>)
pub fn set_position(&mut self, p: Vec2<P>)
Sets this rectangle’s position.
sourcepub fn set_extent(&mut self, e: Extent2<E>)
pub fn set_extent(&mut self, e: Extent2<E>)
Sets this rectangle’s extent (size).
sourcepub fn position_extent(self) -> (Vec2<P>, Extent2<E>)
pub fn position_extent(self) -> (Vec2<P>, Extent2<E>)
Gets this rectangle’s position and extent (size).
sourcepub fn map<DP, DE, PF, EF>(self, pf: PF, ef: EF) -> Rect<DP, DE>
pub fn map<DP, DE, PF, EF>(self, pf: PF, ef: EF) -> Rect<DP, DE>
Returns this rectangle, converted with the given closures (one for position elements, the other for extent elements).
source§impl<T> Rect<T, T>
impl<T> Rect<T, T>
sourcepub fn into_aabr(self) -> Aabr<T>
pub fn into_aabr(self) -> Aabr<T>
Converts this into the matching axis-aligned bounding shape representation.
sourcepub fn contains_point(self, p: Vec2<T>) -> boolwhere
T: PartialOrd,
pub fn contains_point(self, p: Vec2<T>) -> boolwhere
T: PartialOrd,
Does this rectangle contain the given point ?
sourcepub fn contains_rect(self, other: Self) -> boolwhere
T: PartialOrd,
pub fn contains_rect(self, other: Self) -> boolwhere
T: PartialOrd,
Does this rectangle fully contain the given one ?
sourcepub fn collides_with_rect(self, other: Self) -> boolwhere
T: PartialOrd,
pub fn collides_with_rect(self, other: Self) -> boolwhere
T: PartialOrd,
Does this rectangle collide with another ?
source§impl<T> Rect<T, T>
impl<T> Rect<T, T>
sourcepub fn expanded_to_contain_point(self, p: Vec2<T>) -> Selfwhere
T: PartialOrd,
pub fn expanded_to_contain_point(self, p: Vec2<T>) -> Selfwhere
T: PartialOrd,
Returns this shape so that it contains the given point.
sourcepub fn expand_to_contain_point(&mut self, p: Vec2<T>)where
T: PartialOrd,
pub fn expand_to_contain_point(&mut self, p: Vec2<T>)where
T: PartialOrd,
Expands this shape so that it contains the given point.
sourcepub fn union(self, other: Self) -> Self
pub fn union(self, other: Self) -> Self
Gets the smallest rectangle that contains both this one and another.
sourcepub fn intersection(self, other: Self) -> Self
pub fn intersection(self, other: Self) -> Self
Gets the largest rectangle contained by both this one and another.
sourcepub fn expand_to_contain(&mut self, other: Self)
pub fn expand_to_contain(&mut self, other: Self)
Sets this rectangle to the union of itself with another.
sourcepub fn intersect(&mut self, other: Self)
pub fn intersect(&mut self, other: Self)
Sets this rectangle to the intersection of itself with another.
sourcepub fn collision_vector_with_rect(self, other: Self) -> Vec2<T>
pub fn collision_vector_with_rect(self, other: Self) -> Vec2<T>
Gets a vector that tells how much self
penetrates other
.
sourcepub fn split_at_x(self, sp: T) -> [Self; 2]
pub fn split_at_x(self, sp: T) -> [Self; 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.
sourcepub fn split_at_y(self, sp: T) -> [Self; 2]
pub fn split_at_y(self, sp: T) -> [Self; 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<'de, P, E> Deserialize<'de> for Rect<P, E>where
P: Deserialize<'de>,
E: Deserialize<'de>,
impl<'de, P, E> Deserialize<'de> for Rect<P, E>where
P: Deserialize<'de>,
E: 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>,
impl<P: Copy, E: Copy> Copy for Rect<P, E>
impl<P: Eq, E: Eq> Eq for Rect<P, E>
impl<P, E> StructuralPartialEq for Rect<P, E>
Auto Trait Implementations§
impl<P, E> Freeze for Rect<P, E>
impl<P, E> RefUnwindSafe for Rect<P, E>where
P: RefUnwindSafe,
E: RefUnwindSafe,
impl<P, E> Send for Rect<P, E>
impl<P, E> Sync for Rect<P, E>
impl<P, E> Unpin for Rect<P, E>
impl<P, E> UnwindSafe for Rect<P, E>where
P: UnwindSafe,
E: UnwindSafe,
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)