Struct gdnative::core_types::Rect2
source · [−]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
sourceimpl Rect2
impl Rect2
sourcepub fn from_components(x: f32, y: f32, width: f32, height: f32) -> Rect2
pub fn from_components(x: f32, y: f32, width: f32, height: f32) -> Rect2
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) -> Rect2
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.
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: Rect2) -> bool
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.
sourcepub fn intersects(self, b: Rect2) -> bool
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.
sourcepub fn intersects_including_borders(self, b: Rect2) -> bool
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.
sourcepub fn encloses(self, b: Rect2) -> bool
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.
sourcepub fn intersection(self, b: Rect2) -> Option<Rect2>
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.
sourcepub fn merge(self, b: Rect2) -> Rect2
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.
sourcepub fn expand(self, to: Vector2) -> Rect2
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));
sourcepub fn grow(self, by: f32) -> Rect2
pub fn grow(self, by: f32) -> Rect2
Returns a copy of this rectangle grown by a given amount of units on all the sides.
Trait Implementations
sourceimpl CoerceFromVariant for Rect2
impl CoerceFromVariant for Rect2
fn coerce_from_variant(v: &Variant) -> Rect2
sourceimpl<'de> Deserialize<'de> for Rect2
impl<'de> Deserialize<'de> for Rect2
sourcefn deserialize<__D>(
__deserializer: __D
) -> Result<Rect2, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D
) -> Result<Rect2, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
sourceimpl Export for Rect2
impl Export for Rect2
sourcefn export_info(_hint: Option<<Rect2 as Export>::Hint>) -> ExportInfo
fn export_info(_hint: Option<<Rect2 as Export>::Hint>) -> ExportInfo
ExportInfo
given an optional typed hint.