godot::builtin

Struct Rect2

#[repr(C)]
pub struct Rect2 { pub position: Vector2, pub size: Vector2, }
Expand description

2D axis-aligned bounding box.

Rect2 consists of a position, a size, and several utility functions. It is typically used for fast overlap tests.

§All bounding-box types

DimensionFloating-pointInteger
2DRect2Rect2i
3DAabb


You can convert to Rect2i using cast_int().

§Godot docs

Rect2 (stable)

Fields§

§position: Vector2§size: Vector2

Implementations§

§

impl Rect2

pub const fn new(position: Vector2, size: Vector2) -> Rect2

Create a new Rect2 from a position and a size.

Godot equivalent: Rect2(Vector2 position, Vector2 size)

pub fn from_corners(position: Vector2, end: Vector2) -> Rect2

Create a new Rect2 with the first corner at position and the opposite corner at end.

pub const fn from_components(x: f32, y: f32, width: f32, height: f32) -> Rect2

Create a new Rect2 from four reals representing position (x,y) and size (width,height).

Godot equivalent: Rect2(float x, float y, float width, float height)

pub const fn from_rect2i(rect: Rect2i) -> Rect2

👎Deprecated: Moved to Rect2i::cast_float()

pub const fn cast_int(self) -> Rect2i

Create a new Rect2i from a Rect2, using as for real to i32 conversions.

Godot equivalent: Rect2i(Rect2 from)

pub fn abs(self) -> Rect2

Returns a rectangle with the same geometry, with top-left corner as position and non-negative size.

pub fn encloses(self, b: Rect2) -> bool

Whether self covers at least the entire area of b (and possibly more).

pub fn expand(self, to: Vector2) -> Rect2

Returns a copy of this rectangle expanded to include a given point.

Note: This method is not reliable for Rect2 with a negative size. Use abs to get a positive sized equivalent rectangle for expanding.

pub fn merge(self, b: Rect2) -> Rect2

Returns a larger rectangle that contains this Rect2 and b.

Note: This method is not reliable for Rect2 with a negative size. Use abs to get a positive sized equivalent rectangle for merging.

pub fn area(self) -> f32

Returns the area of the rectangle.

pub fn center(self) -> Vector2

Returns the center of the Rect2, which is equal to position + (size / 2).

pub fn grow(self, amount: f32) -> Rect2

Returns a copy of the Rect2 grown by the specified amount on all sides.

pub fn grow_individual( self, left: f32, top: f32, right: f32, bottom: f32, ) -> Rect2

Returns a copy of the Rect2 grown by the specified amount on each side individually.

pub fn grow_side(self, side: Side, amount: f32) -> Rect2

Returns a copy of the Rect2 grown by the specified amount on the specified RectSide.

amount may be negative, but care must be taken: If the resulting size has negative components the computation may be incorrect.

pub fn has_area(self) -> bool

Returns true if the Rect2 has area, and false if the Rect2 is linear, empty, or has a negative size. See also get_area.

pub fn has_point(self, point: Vector2) -> bool

Returns true if the Rect2 contains a point. By convention, the right and bottom edges of the Rect2 are considered exclusive, so points on these edges are not included.

Note: This method is not reliable for Rect2 with a negative size. Use abs to get a positive sized equivalent rectangle to check for contained points.

pub fn intersection(self, b: Rect2) -> Option<Rect2>

Returns the intersection of this Rect2 and b. If the rectangles do not intersect, an empty Rect2 is returned.

pub fn intersects(self, b: Rect2) -> bool

Checks whether two rectangles have at least one point in common.

Also returns true if the rects only touch each other (share a point/edge). See intersects_exclude_borders if you want to return false in that case.

Godot equivalent: Rect2.intersects(Rect2 b, bool include_borders = true)

pub fn intersects_exclude_borders(self, b: Rect2) -> bool

Checks whether two rectangles have at least one inner point in common (not on the borders).

Returns false if the rects only touch each other (share a point/edge). See intersects if you want to return true in that case.

Godot equivalent: Rect2.intersects(AABB b, bool include_borders = false)

pub fn is_finite(self) -> bool

Returns true if this Rect2 is finite, by calling @GlobalScope.is_finite on each component.

pub fn end(self) -> Vector2

The end of the Rect2 calculated as position + size.

pub fn set_end(&mut self, end: Vector2)

Set size based on desired end-point.

pub fn assert_nonnegative(self)

Assert that the size of the Rect2 is not negative.

Certain functions will fail to give a correct result if the size is negative.

Trait Implementations§

§

impl ApproxEq for Rect2

§

fn approx_eq(&self, other: &Rect2) -> bool

Returns if the two Rect2s are approximately equal, by comparing position and size separately.

§

impl ArrayElement for Rect2

§

impl Clone for Rect2

§

fn clone(&self) -> Rect2

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
§

impl Debug for Rect2

§

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

Formats the value using the given formatter. Read more
§

impl Default for Rect2

§

fn default() -> Rect2

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

impl Display for Rect2

§

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

Formats Rect2 to match Godot’s string representation.

§Example
use godot::prelude::*;
let rect = Rect2::new(Vector2::new(0.0, 0.0), Vector2::new(1.0, 1.0));
assert_eq!(format!("{}", rect), "[P: (0, 0), S: (1, 1)]");
§

impl Export for Rect2

§

fn export_hint() -> PropertyHintInfo

The export info to use for an exported field of this type, if no other export info is specified.
§

fn as_node_class() -> Option<ClassName>

If this is a class inheriting Node, returns the ClassName; otherwise None. Read more
§

impl FromGodot for Rect2

§

fn try_from_godot( via: <Rect2 as GodotConvert>::Via, ) -> Result<Rect2, ConvertError>

Converts the Godot representation to this type, returning Err on failure.
§

fn from_godot(via: Self::Via) -> Self

⚠️ Converts the Godot representation to this type. Read more
§

fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>

Performs the conversion from a Variant, returning Err on failure.
§

fn from_variant(variant: &Variant) -> Self

⚠️ Performs the conversion from a Variant. Read more
§

impl GodotConvert for Rect2

§

type Via = Rect2

The type through which Self is represented in Godot.
§

impl Mul<Rect2> for Transform2D

§

fn mul(self, rhs: Rect2) -> <Transform2D as Mul<Rect2>>::Output

Transforms each coordinate in rhs.position and rhs.end() individually by this transform, then creates a Rect2 containing all of them.

§

type Output = Rect2

The resulting type after applying the * operator.
§

impl PartialEq for Rect2

§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl ToGodot for Rect2

§

type ToVia<'v> = <Rect2 as GodotConvert>::Via

Target type of to_godot(), which differs from Via for pass-by-reference types.
§

fn to_godot(&self) -> <Rect2 as ToGodot>::ToVia<'_>

Converts this type to the Godot type by reference, usually by cloning.
§

fn to_variant(&self) -> Variant

Converts this type to a Variant.
§

impl Var for Rect2

§

fn get_property(&self) -> <Rect2 as GodotConvert>::Via

§

fn set_property(&mut self, value: <Rect2 as GodotConvert>::Via)

§

fn var_hint() -> PropertyHintInfo

Specific property hints, only override if they deviate from GodotType::property_info, e.g. for enums/newtypes.
§

impl Copy for Rect2

§

impl GodotType for Rect2

§

impl StructuralPartialEq for Rect2

Auto Trait Implementations§

§

impl Freeze for Rect2

§

impl RefUnwindSafe for Rect2

§

impl Send for Rect2

§

impl Sync for Rect2

§

impl Unpin for Rect2

§

impl UnwindSafe for Rect2

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<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

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

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

source§

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