Struct gdnative::core_types::geom::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.

The 3D counterpart to Rect2 is Aabb.

Fields§

§position: Vector2

The rectangle’s position in 2D space.

§size: Vector2

Width and height.

Implementations§

§

impl Rect2

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

Creates a Rect2 by position and size.

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

Creates a Rect2 by x, y, width, and height.

pub fn end(self) -> Vector2

Ending corner. This is calculated as position + size.

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

Ending corner. Setting this value will change the size.

pub fn abs(self) -> Rect2

Returns a rectangle with equivalent position and area, modified so that the top-left corner is the origin and width and height are positive.

pub fn area(self) -> f32

Returns the area of the rectangle. See also has_no_area.

pub fn has_no_area(self) -> bool

Returns true if the rectangle is flat or empty. See also area.

Note: If the Rect2 has a negative size and is not flat or empty, this method will return true. Use abs to make the size positive.

§Example
let rect = Rect2::new(
    Vector2::new(2.0, 3.0),
    Vector2::new(-3.0, -4.0),
);
assert!(rect.has_no_area());
assert!(!rect.abs().has_no_area());

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

Returns true if the rectangle contains a point. By convention, the right and bottom edges of the rectangle 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 is_equal_approx(self, b: Rect2) -> bool

Returns true if this rectangle and b are approximately equal, by calling is_equal_approx on each component.

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

Returns true if the inside of the rectangle overlaps with b (i.e. they have at least one point in common).

This excludes borders. See intersects_including_borders for inclusive check.

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

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

Returns true if the rectangle overlaps with b (i.e. they have at least one point in common) or their borders touch even without intersection.

This includes borders. See intersects for exclusive check.

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

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

Returns true if this rectangle (inclusively) encloses b.

This is true when self covers all the area of b, and possibly (but not necessarily) more.

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

Returns the intersection of this rectangle and b, or None if they don’t intersect.

This is similar to the GDScript clip function, but returns None instead of self if there is no intersection. This method excludes borders just like intersects.

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

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

§Example
let rect = Rect2::new(
    Vector2::new(-3.0, 2.0),
    Vector2::new(1.0, 1.0),
);

let rect2 = rect.expand(Vector2::new(0.0, -1.0));

assert_eq!(rect2.position, Vector2::new(-3.0, -1.0));
assert_eq!(rect2.size, Vector2::new(3.0, 4.0));

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

Returns a copy of this rectangle grown by a given amount of units on all the sides.

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

Returns a copy of this rectangle grown by a given amount of units towards each direction individually.

pub fn grow_margin(self, margin: Margin, amount: f32) -> Rect2

Returns a copy of this rectangle grown by a given amount of units towards the Margin direction.

Trait Implementations§

§

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 CoerceFromVariant for Rect2

§

impl Debug for Rect2

§

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

Formats the value using the given formatter. Read more
§

impl<'de> Deserialize<'de> for Rect2

§

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

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

impl Export for Rect2

§

type Hint = NoHint

A type-specific hint type that is valid for the type being exported. Read more
§

fn export_info(_hint: Option<<Rect2 as Export>::Hint>) -> ExportInfo

Returns ExportInfo given an optional typed hint.
§

impl FromVariant for Rect2

§

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 Serialize for Rect2

§

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
§

impl ToVariant for Rect2

§

fn to_variant(&self) -> Variant

§

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

§

impl<T> OwnedToVariant for T
where T: ToVariant,

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 = 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.
source§

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