Rect2

2D axis-aligned bounding box.

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.

The 3D counterpart toRect2 isAABB.

Tutorials

Properties

Vector2

end

Vector2(0,0)

Vector2

position

Vector2(0,0)

Vector2

size

Vector2(0,0)

Methods

Rect2

Rect2(Vector2 position,Vector2 size)

Rect2

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

Rect2

abs()

Rect2

clip(Rect2 b)

bool

encloses(Rect2 b)

Rect2

expand(Vector2 to)

float

get_area()

Rect2

grow(float by)

Rect2

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

Rect2

grow_margin(int margin,float by)

bool

has_no_area()

bool

has_point(Vector2 point)

bool

intersects(Rect2 b,bool include_borders=false)

bool

is_equal_approx(Rect2 rect)

Rect2

merge(Rect2 b)

Property Descriptions

Default

Vector2(0,0)

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


Default

Vector2(0,0)

Beginning corner. Typically has values lower thanend.


Default

Vector2(0,0)

Size fromposition toend. Typically, all components are positive.

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

Method Descriptions

Constructs aRect2 by position and size.


Constructs aRect2 by x, y, width, and height.


Returns aRect2 with equivalent position and area, modified so that the top-left corner is the origin andwidth andheight are positive.


Returns the intersection of thisRect2 and b.


Returnstrue if thisRect2 completely encloses another one.


Returns a copy of thisRect2 expanded to include a given point.

Example:

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

Returns the area of theRect2. See alsohas_no_area.


Returns a copy of theRect2 grown a given amount of units towards all the sides.


Returns a copy of theRect2 grown a given amount of units towards each direction individually.


Returns a copy of theRect2 grown a given amount of units towards theMargin direction.


Returnstrue if theRect2 is flat or empty,false otherwise. See alsoget_area.

Note: If theRect2 has a negative size and is not flat or empty,has_no_area will returntrue.


Returnstrue if theRect2 contains a point. By convention, the right and bottom edges of theRect2 are considered exclusive, so points on these edges arenot included.

Note: This method is not reliable forRect2 with anegative size. Useabs to get a positive sized equivalent rectangle to check for contained points.


Returnstrue if theRect2 overlaps withb (i.e. they have at least one point in common).

Ifinclude_borders istrue, they will also be considered overlapping if their borders touch, even without intersection.


Returnstrue if thisRect2 andrect are approximately equal, by callingis_equal_approx on each component.


Returns a largerRect2 that contains thisRect2 andb.