Struct Rect2
- Namespace
- Godot
- Assembly
- GodotSharp.dll
2D axis-aligned bounding box. Rect2 consists of a position, a size, and several utility functions. It is typically used for fast overlap tests.
public struct Rect2 : IEquatable<Rect2>
- Implements
- Inherited Members
Constructors
Rect2(Vector2, Vector2)
Constructs a Rect2 from a position and size.
public Rect2(Vector2 position, Vector2 size)
Parameters
Rect2(Vector2, float, float)
Constructs a Rect2 from a position, width, and height.
public Rect2(Vector2 position, float width, float height)
Parameters
Rect2(float, float, Vector2)
Constructs a Rect2 from x, y, and size.
public Rect2(float x, float y, Vector2 size)
Parameters
Rect2(float, float, float, float)
Constructs a Rect2 from x, y, width, and height.
public Rect2(float x, float y, float width, float height)
Parameters
x
floatThe position's X coordinate.
y
floatThe position's Y coordinate.
width
floatThe width.
height
floatThe height.
Properties
Area
public readonly float Area { get; }
Property Value
End
public Vector2 End { readonly get; set; }
Property Value
- Vector2
Getting is equivalent to
value
= Position + Size, setting is equivalent to Size =value
- Position
Position
Beginning corner. Typically has values lower than End.
public Vector2 Position { readonly get; set; }
Property Value
- Vector2
Directly uses a private field.
Size
Size from Position to End. Typically all components are positive. If the size is negative, you can use Abs() to fix it.
public Vector2 Size { readonly get; set; }
Property Value
- Vector2
Directly uses a private field.
Methods
Abs()
Returns a Rect2 with equivalent position and size, modified so that the top-left corner is the origin and width and height are positive.
public readonly Rect2 Abs()
Returns
Encloses(Rect2)
public readonly bool Encloses(Rect2 b)
Parameters
Returns
Equals(Rect2)
Returns true if this rect and other
are equal.
public readonly bool Equals(Rect2 other)
Parameters
other
Rect2The other rect to compare.
Returns
- bool
Whether or not the rects are exactly equal.
Equals(object)
Returns true if this rect and obj
are equal.
public override readonly bool Equals(object obj)
Parameters
obj
objectThe other object to compare.
Returns
- bool
Whether or not the rect and the other object are exactly equal.
Expand(Vector2)
Returns this Rect2 expanded to include a given point.
public readonly Rect2 Expand(Vector2 to)
Parameters
to
Vector2The point to include.
Returns
GetCenter()
public readonly Vector2 GetCenter()
Returns
- Vector2
The center.
GetHashCode()
Serves as the hash function for Rect2.
public override readonly int GetHashCode()
Returns
- int
A hash code for this rect.
Grow(float)
Returns a copy of the Rect2 grown by the specified amount on all sides.
public readonly Rect2 Grow(float by)
Parameters
by
floatThe amount to grow by.
Returns
- See Also
GrowIndividual(float, float, float, float)
Returns a copy of the Rect2 grown by the specified amount on each side individually.
public readonly Rect2 GrowIndividual(float left, float top, float right, float bottom)
Parameters
left
floatThe amount to grow by on the left side.
top
floatThe amount to grow by on the top side.
right
floatThe amount to grow by on the right side.
bottom
floatThe amount to grow by on the bottom side.
Returns
- See Also
GrowSide(Side, float)
public readonly Rect2 GrowSide(Side side, float by)
Parameters
Returns
- See Also
HasArea()
Returns true if the Rect2 has area, and false if the Rect2 is linear, empty, or has a negative Size. See also Area.
public readonly bool HasArea()
Returns
HasPoint(Vector2)
public readonly bool HasPoint(Vector2 point)
Parameters
point
Vector2The point to check.
Returns
Intersection(Rect2)
Returns the intersection of this Rect2 and b
.
If the rectangles do not intersect, an empty Rect2 is returned.
public readonly Rect2 Intersection(Rect2 b)
Parameters
Returns
Intersects(Rect2, bool)
Returns true if the Rect2 overlaps with b
(i.e. they have at least one point in common).
If includeBorders
is true,
they will also be considered overlapping if their borders touch,
even without intersection.
public readonly bool Intersects(Rect2 b, bool includeBorders = false)
Parameters
b
Rect2The other Rect2 to check for intersections with.
includeBorders
boolWhether or not to consider borders.
Returns
IsEqualApprox(Rect2)
Returns true if this rect and other
are approximately equal,
by running IsEqualApprox(Vector2) on each component.
public readonly bool IsEqualApprox(Rect2 other)
Parameters
other
Rect2The other rect to compare.
Returns
- bool
Whether or not the rects are approximately equal.
IsFinite()
Returns true if this Rect2 is finite, by calling IsFinite(float) on each component.
public bool IsFinite()
Returns
- bool
Whether this vector is finite or not.
Merge(Rect2)
public readonly Rect2 Merge(Rect2 b)
Parameters
Returns
ToString()
Converts this Rect2 to a string.
public override readonly string ToString()
Returns
- string
A string representation of this rect.
ToString(string)
Converts this Rect2 to a string with the given format
.
public readonly string ToString(string format)
Parameters
format
string
Returns
- string
A string representation of this rect.
Operators
operator ==(Rect2, Rect2)
Returns true if the Rect2s are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Rect2) instead, which is more reliable.
public static bool operator ==(Rect2 left, Rect2 right)
Parameters
Returns
- bool
Whether or not the rects are exactly equal.
operator !=(Rect2, Rect2)
Returns true if the Rect2s are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Rect2) instead, which is more reliable.
public static bool operator !=(Rect2 left, Rect2 right)
Parameters
Returns
- bool
Whether or not the rects are not equal.