#[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: Vector2The rectangle’s position in 2D space.
size: Vector2Width and height.
Implementations§
Source§impl Rect2
impl Rect2
Sourcepub fn from_components(x: f32, y: f32, width: f32, height: f32) -> Self
pub fn from_components(x: f32, y: f32, width: f32, height: f32) -> Self
Creates a Rect2 by x, y, width, and height.
Sourcepub fn set_end(&mut self, new_end: Vector2)
pub fn set_end(&mut self, new_end: Vector2)
Ending corner. Setting this value will change the size.
Sourcepub fn abs(self) -> Self
pub fn abs(self) -> Self
Returns a rectangle with equivalent position and area, modified so that the top-left corner
is the origin and width and height are positive.
Sourcepub fn area(self) -> f32
pub fn area(self) -> f32
Returns the area of the rectangle. See also has_no_area.
Sourcepub fn has_no_area(self) -> bool
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());Sourcepub fn contains_point(self, point: Vector2) -> bool
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.
Sourcepub fn is_equal_approx(self, b: Self) -> bool
pub fn is_equal_approx(self, b: Self) -> bool
Returns true if this rectangle and b are approximately equal, by calling
is_equal_approx on each component.
Sourcepub fn intersects(self, b: Self) -> bool
pub fn intersects(self, b: Self) -> 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.
Sourcepub fn intersects_including_borders(self, b: Self) -> bool
pub fn intersects_including_borders(self, b: Self) -> 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.
Sourcepub fn encloses(self, b: Self) -> bool
pub fn encloses(self, b: Self) -> 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.
Sourcepub fn intersection(self, b: Self) -> Option<Self>
pub fn intersection(self, b: Self) -> Option<Self>
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.
Sourcepub fn merge(self, b: Self) -> Self
pub fn merge(self, b: Self) -> Self
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.
Sourcepub fn expand(self, to: Vector2) -> Self
pub fn expand(self, to: Vector2) -> Self
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));Sourcepub fn grow(self, by: f32) -> Self
pub fn grow(self, by: f32) -> Self
Returns a copy of this rectangle grown by a given amount of units on all the sides.
Sourcepub fn grow_individual(
self,
left: f32,
top: f32,
right: f32,
bottom: f32,
) -> Self
pub fn grow_individual( self, left: f32, top: f32, right: f32, bottom: f32, ) -> Self
Returns a copy of this rectangle grown by a given amount of units towards each direction individually.
Sourcepub fn grow_margin(self, margin: Margin, amount: f32) -> Self
pub fn grow_margin(self, margin: Margin, amount: f32) -> Self
Returns a copy of this rectangle grown by a given amount of units towards the Margin
direction.
Trait Implementations§
Source§impl CoerceFromVariant for Rect2
impl CoerceFromVariant for Rect2
fn coerce_from_variant(v: &Variant) -> Self
Source§impl Export for Rect2
impl Export for Rect2
Source§type Hint = NoHint
type Hint = NoHint
Source§fn export_info(_hint: Option<Self::Hint>) -> ExportInfo
fn export_info(_hint: Option<Self::Hint>) -> ExportInfo
ExportInfo given an optional typed hint.