Table of Contents

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

position Vector2

The position.

size Vector2

The size.

Rect2(Vector2, float, float)

Constructs a Rect2 from a position, width, and height.

public Rect2(Vector2 position, float width, float height)

Parameters

position Vector2

The position.

width float

The width.

height float

The height.

Rect2(float, float, Vector2)

Constructs a Rect2 from x, y, and size.

public Rect2(float x, float y, Vector2 size)

Parameters

x float

The position's X coordinate.

y float

The position's Y coordinate.

size Vector2

The size.

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 float

The position's X coordinate.

y float

The position's Y coordinate.

width float

The width.

height float

The height.

Properties

Area

The area of this Rect2. See also HasArea().

public readonly float Area { get; }

Property Value

float

End

Ending corner. This is calculated as Position plus Size. Setting this value will change the size.

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

Rect2

The modified Rect2.

Encloses(Rect2)

Returns true if this Rect2 completely encloses another one.

public readonly bool Encloses(Rect2 b)

Parameters

b Rect2

The other Rect2 that may be enclosed.

Returns

bool

A bool for whether or not this Rect2 encloses b.

Equals(Rect2)

Returns true if this rect and other are equal.

public readonly bool Equals(Rect2 other)

Parameters

other Rect2

The 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 object

The 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 Vector2

The point to include.

Returns

Rect2

The expanded Rect2.

GetCenter()

Returns the center of the Rect2, which is equal to Position + (Size / 2).

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 float

The amount to grow by.

Returns

Rect2

The grown Rect2.

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 float

The amount to grow by on the left side.

top float

The amount to grow by on the top side.

right float

The amount to grow by on the right side.

bottom float

The amount to grow by on the bottom side.

Returns

Rect2

The grown Rect2.

See Also

GrowSide(Side, float)

Returns a copy of the Rect2 grown by the specified amount on the specified Side.

public readonly Rect2 GrowSide(Side side, float by)

Parameters

side Side

The side to grow.

by float

The amount to grow by.

Returns

Rect2

The grown Rect2.

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

bool

A bool for whether or not the Rect2 has area.

HasPoint(Vector2)

Returns true if the Rect2 contains a point, or false otherwise.

public readonly bool HasPoint(Vector2 point)

Parameters

point Vector2

The point to check.

Returns

bool

A bool for whether or not the Rect2 contains point.

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

b Rect2

The other Rect2.

Returns

Rect2

The intersection of this Rect2 and b, or an empty Rect2 if they do not intersect.

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 Rect2

The other Rect2 to check for intersections with.

includeBorders bool

Whether or not to consider borders.

Returns

bool

A bool for whether or not they are intersecting.

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 Rect2

The 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)

Returns a larger Rect2 that contains this Rect2 and b.

public readonly Rect2 Merge(Rect2 b)

Parameters

b Rect2

The other Rect2.

Returns

Rect2

The merged Rect2.

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

left Rect2

The left rect.

right Rect2

The right rect.

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

left Rect2

The left rect.

right Rect2

The right rect.

Returns

bool

Whether or not the rects are not equal.