Skip to main content

Rect

Struct Rect 

Source
pub struct Rect {
    pub x: f32,
    pub y: f32,
    pub w: f32,
    pub h: f32,
}
Expand description

§Rect

Contains the position (x, y) and size (width, height) of a rectangle. Can be used for detecting AABB collisions or drawing rectangles.

§Examples

let my_rect = Rect::new(0.0, 0.0, 50.0, 50.0);

Drawing a Rect to the screen:

my_rect.draw(RED);
// or
draw_rectangle_from_rect(my_rect, RED);

Check collisions:

if my_rect.contains_point(mouse_position_vec2()) {
    println!("Mouse is inside the rect")
}

let aabb_intersection: Option<Rect> = my_rect.intersection(other_rect);

Fields§

§x: f32§y: f32§w: f32§h: f32

Implementations§

Source§

impl Rect

Source

pub const fn new(x: f32, y: f32, w: f32, h: f32) -> Self

Makes a new Rect object with the x, y position and Width x Height

Source

pub fn from_corners(a: Vector2, b: Vector2) -> Self

Build a Rect from two corner points (handles any order)

Source

pub fn from_center(center: Vector2, w: f32, h: f32) -> Self

Build a Rect centered on center with the given size

Source

pub const fn left(&self) -> f32

Source

pub const fn right(&self) -> f32

Source

pub const fn top(&self) -> f32

Source

pub const fn bottom(&self) -> f32

Source

pub const fn top_left(&self) -> Vector2

Source

pub const fn top_right(&self) -> Vector2

Source

pub const fn bottom_left(&self) -> Vector2

Source

pub const fn bottom_right(&self) -> Vector2

Source

pub const fn xy(&self) -> Vector2

Source

pub const fn wh(&self) -> Vector2

Source

pub const fn xw(&self) -> Vector2

Source

pub const fn xh(&self) -> Vector2

Source

pub const fn yw(&self) -> Vector2

Source

pub const fn yh(&self) -> Vector2

Source

pub fn corners(&self) -> [Vector2; 4]

Source

pub fn center(&self) -> Vector2

Source

pub const fn area(&self) -> f32

Source

pub const fn perimeter(&self) -> f32

Source

pub const fn size(&self) -> Vector2

Source

pub const fn position(&self) -> Vector2

Source

pub const fn with_x(&self, x: f32) -> Self

Get a new Rect with the specified x

Source

pub const fn with_y(&self, y: f32) -> Self

Get a new Rect with the specified y

Source

pub const fn with_w(&self, w: f32) -> Self

Get a new Rect with the specified w

Source

pub const fn with_h(&self, h: f32) -> Self

Get a new Rect with the specified h

Source

pub fn with_position(&self, pos: Vector2) -> Self

Get a new Rect with the specified position

Source

pub fn with_position_xy(&self, x: f32, y: f32) -> Self

Get a new Rect with the specified position

Source

pub fn change_position_by(&mut self, offset: Vector2)

Changes the position relatively by an offset Vector2.

Source

pub fn change_size_by(&mut self, offset: Vector2)

Changes the size relatively by an offset Vector2.

Source

pub fn with_size(&self, size: Vector2) -> Self

Get a new Rect with the specified size

Source

pub fn translated(&self, offset: Vector2) -> Self

Get a moved Rect by an offset

Source

pub fn centered_at(&self, center: Vector2) -> Self

Move the rect so its center is at center

Source

pub fn inflated(&self, amount: f32) -> Self

Grow/shrink the rect on all sides by amount (negative shrinks), keeping center fixed

Source

pub const fn scaled(&self, factor: f32) -> Self

Scale width/height around the top-left corner (position unchanged)

Source

pub fn scaled_from_center(&self, factor: f32) -> Self

Scale width/height around the rect’s center

Source

pub fn union(&self, other: &Rect) -> Self

Smallest rect that fully contains both self and other

Source

pub fn clamp_point(&self, point: Vector2) -> Vector2

Clamp a point to lie within this rect

Source

pub fn normalized(&self) -> Self

Fix a rect with negative width/height into one with positive dimensions

Source

pub fn to_array(&self) -> [f32; 4]

Source

pub fn lerp(self, other: Rect, t: f32) -> Self

Linearly interpolate between two rects

Source

pub const fn contains_point(&self, x: f32, y: f32) -> bool

Test if point (x, y) lies inside/on the Rect

Source

pub const fn contains_point_fully(&self, x: f32, y: f32) -> bool

Test if point (x, y) lies inside/on the Rect

Source

pub const fn contains_point_vec2(&self, point: Vector2) -> bool

Test if point (Vector2) lies inside/on the Rect

Source

pub const fn contains_point_fully_vec2(&self, point: Vector2) -> bool

Test if point (Vector2) lies inside/on the Rect

Source

pub fn contains_mouse(&self) -> bool

Test if mouse lies inside/on the Rect

Source

pub fn contains_mouse_fully(&self) -> bool

Test if mouse lies inside/on the Rect

Source

pub const fn intersects(&self, other: &Rect) -> bool

AABB overlap test

Source

pub const fn intersection(&self, other: Rect) -> Option<Rect>

Returns the overlapping region, or None if they don’t intersect

Source

pub const fn contains_rect_fully(&self, other: &Rect) -> bool

True if self fully contains other

Source

pub fn update(&mut self, new_rect: Rect) -> Rect

Updates the original rect with a new one and returns it as well

Source

pub fn draw(&self, color: Color)

Draw the Rect to the screen using a color

Source

pub fn draw_rounded(&self, radius: f32, color: Color)

Source

pub fn draw_rounded_ex( &self, top_left: f32, top_right: f32, bottom_left: f32, bottom_right: f32, color: Color, )

Source

pub fn draw_lines(&self, thickness: f32, color: Color)

Draw the Rect’s outline to the screen using a color and thickness

Source

pub fn draw_lines_rounded(&self, radius: f32, thickness: f32, color: Color)

Source

pub fn draw_lines_rounded_ex( &self, top_left: f32, top_right: f32, bottom_left: f32, bottom_right: f32, thickness: f32, color: Color, )

Source§

impl Rect

Source

pub fn resolve_collision(&mut self, other: Rect) -> Option<Vector2>

Pushes self out of an axis-aligned other. Returns the collision normal (direction self was pushed), or None.

Source

pub fn resolve_collision_obb( &mut self, angle: f32, other: &Rect, other_angle: f32, ) -> Option<Vector2>

Pushes self out of a rotated other (SAT). Returns the collision normal (direction self was pushed), or None.

Source

pub fn intersection_obb( &self, angle: f32, other: &Rect, other_angle: f32, ) -> Option<Vector2>

OBB vs OBB collision (SAT). Angles are in radians, each rect rotates around its own center. Returns the minimum translation vector that pushes self out of other, or None if not colliding.

Source

pub fn intersects_obb(&self, angle: f32, other: &Rect, other_angle: f32) -> bool

Bool-only OBB overlap test

Source

pub fn corners_by_angle(&self, angle: f32) -> [Vector2; 4]

Corners of the rect rotated by angle (radians) around its center

Source

pub fn draw_rotated(&self, angle: f32, color: Color)

Draw the Rect rotated by angle (radians) around its center

Source

pub fn draw_lines_rotated(&self, angle: f32, thickness: f32, color: Color)

Draw the rotated Rect’s outline

Trait Implementations§

Source§

impl Clone for Rect

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Rect

Source§

impl Debug for Rect

Source§

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

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

impl Default for Rect

Source§

fn default() -> Self

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

impl Into<[f32; 4]> for Rect

Source§

fn into(self) -> [f32; 4]

Converts this type into the (usually inferred) input type.
Source§

impl PartialEq for Rect

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Rect

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Rect

Auto Trait Implementations§

§

impl Freeze for Rect

§

impl RefUnwindSafe for Rect

§

impl Send for Rect

§

impl Sync for Rect

§

impl Unpin for Rect

§

impl UnsafeUnpin for Rect

§

impl UnwindSafe for Rect

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more