Struct gdnative::core_types::Rect2

source ·
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§

source§

impl Rect2

source

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

Creates a Rect2 by position and size.

source

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

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

source

pub fn end(self) -> Vector2

Ending corner. This is calculated as position + size.

source

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

Ending corner. Setting this value will change the size.

source

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.

source

pub fn area(self) -> f32

Returns the area of the rectangle. See also has_no_area.

source

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());
source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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));
source

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

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

source

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.

source

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§

source§

impl Clone for Rect2

source§

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

impl CoerceFromVariant for Rect2

source§

impl Debug for Rect2

source§

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

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

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

source§

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

impl Export for Rect2

§

type Hint = NoHint

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

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

Returns ExportInfo given an optional typed hint.
source§

impl FromVariant for Rect2

source§

impl PartialEq<Rect2> for Rect2

source§

fn eq(&self, other: &Rect2) -> 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 Serialize for Rect2

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

source§

impl Copy for Rect2

source§

impl StructuralPartialEq for Rect2

Auto Trait Implementations§

§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · 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> OwnedToVariant for Twhere
    T: ToVariant,

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.
const: unstable · 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.
const: unstable · source§

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

Performs the conversion.
source§

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