Rect2

2D axis-aligned bounding box using floating point coordinates.

Description

Rect2 consists of a position, a size, and several utility functions. It is typically used for fast overlap tests.

It uses floating-point coordinates. If you need integer coordinates, use Rect2i instead.

The 3D counterpart to Rect2 is AABB.

Negative values for size are not supported and will not work for most methods. Use abs to get a Rect2 with a positive size.

Tutorials

Properties

Vector2

end

Vector2(0, 0)

Vector2

position

Vector2(0, 0)

Vector2

size

Vector2(0, 0)

Constructors

Rect2

Rect2 ( )

Rect2

Rect2 ( Rect2 from )

Rect2

Rect2 ( Rect2i from )

Rect2

Rect2 ( Vector2 position, Vector2 size )

Rect2

Rect2 ( float x, float y, float width, float height )

Methods

Rect2

abs ( ) const

bool

encloses ( Rect2 b ) const

Rect2

expand ( Vector2 to ) const

float

get_area ( ) const

Vector2

get_center ( ) const

Rect2

grow ( float amount ) const

Rect2

grow_individual ( float left, float top, float right, float bottom ) const

Rect2

grow_side ( int side, float amount ) const

bool

has_no_area ( ) const

bool

has_point ( Vector2 point ) const

Rect2

intersection ( Rect2 b ) const

bool

intersects ( Rect2 b, bool include_borders=false ) const

bool

is_equal_approx ( Rect2 rect ) const

Rect2

merge ( Rect2 b ) const

Operators

bool

operator != ( Rect2 right )

Rect2

operator * ( Transform2D right )

bool

operator == ( Rect2 right )

Property Descriptions

Default

Vector2(0, 0)

Ending corner. This is calculated as position + size. Setting this value will change the size.


Default

Vector2(0, 0)

Beginning corner. Typically has values lower than end.


Default

Vector2(0, 0)

Size from position to end. Typically, all components are positive.

If the size is negative, you can use abs to fix it.

Constructor Descriptions

Constructs a default-initialized Rect2 with default (zero) values of position and size.


Constructs a Rect2 as a copy of the given Rect2.


Constructs a Rect2 from a Rect2i.


Constructs a Rect2 by position and size.


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

Method Descriptions

Returns a Rect2 with equivalent position and area, modified so that the top-left corner is the origin and width and height are positive.


Returns true if this Rect2 completely encloses another one.


Returns a copy of this Rect2 expanded to include a given point.

Example:

# position (-3, 2), size (1, 1)
var rect = Rect2(Vector2(-3, 2), Vector2(1, 1))
# position (-3, -1), size (3, 4), so we fit both rect and Vector2(0, -1)
var rect2 = rect.expand(Vector2(0, -1))

  • float get_area ( ) const

Returns the area of the Rect2. See also has_no_area.


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


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


Returns a copy of the Rect2 grown by the specified amount on each side individually.


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


  • bool has_no_area ( ) const

Returns true if the Rect2 is flat or empty, false otherwise. See also get_area.

Note: If the Rect2 has a negative size and is not flat or empty, has_no_area will return true.


Returns true if the Rect2 contains a point. By convention, the right and bottom edges of the Rect2 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.


Returns the intersection of this Rect2 and b.

If the rectangles do not intersect, an empty Rect2 is returned.


Returns true if the Rect2 overlaps with b (i.e. they have at least one point in common).

If include_borders is true, they will also be considered overlapping if their borders touch, even without intersection.


Returns true if this Rect2 and rect are approximately equal, by calling is_equal_approx on each component.


Returns a larger Rect2 that contains this Rect2 and b.

Operator Descriptions

Returns true if the rectangles are not equal.

Note: Due to floating-point precision errors, consider using is_equal_approx instead, which is more reliable.


Inversely transforms (multiplies) the Rect2 by the given Transform2D transformation matrix.


Returns true if the rectangles are exactly equal.

Note: Due to floating-point precision errors, consider using is_equal_approx instead, which is more reliable.