Rect2i

2D axis-aligned bounding box using integer coordinates.

Description

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

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

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

Tutorials

Properties

Vector2i

end

Vector2i(0, 0)

Vector2i

position

Vector2i(0, 0)

Vector2i

size

Vector2i(0, 0)

Constructors

Rect2i

Rect2i ( )

Rect2i

Rect2i ( Rect2i from )

Rect2i

Rect2i ( Rect2 from )

Rect2i

Rect2i ( Vector2i position, Vector2i size )

Rect2i

Rect2i ( int x, int y, int width, int height )

Methods

Rect2i

abs ( ) const

bool

encloses ( Rect2i b ) const

Rect2i

expand ( Vector2i to ) const

int

get_area ( ) const

Vector2i

get_center ( ) const

Rect2i

grow ( int amount ) const

Rect2i

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

Rect2i

grow_side ( int side, int amount ) const

bool

has_no_area ( ) const

bool

has_point ( Vector2i point ) const

Rect2i

intersection ( Rect2i b ) const

bool

intersects ( Rect2i b ) const

Rect2i

merge ( Rect2i b ) const

Operators

bool

operator != ( Rect2i right )

bool

operator == ( Rect2i right )

Property Descriptions

Default

Vector2i(0, 0)

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


Default

Vector2i(0, 0)

Beginning corner. Typically has values lower than end.


Default

Vector2i(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 Rect2i with default (zero) values of position and size.


Constructs a Rect2i as a copy of the given Rect2i.


Constructs a new Rect2i from Rect2. The floating point coordinates will be truncated.


Constructs a Rect2i by position and size.


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

Method Descriptions

Returns a Rect2i 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 Rect2i completely encloses another one.


Returns a copy of this Rect2i expanded so that the borders align with the given point.

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

  • int get_area ( ) const

Returns the area of the Rect2i. See also has_no_area.


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

If size is an odd number, the returned center value will be rounded towards position.


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


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


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


  • bool has_no_area ( ) const

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

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


Returns true if the Rect2i contains a point. By convention, the right and bottom edges of the Rect2i are considered exclusive, so points on these edges are not included.

Note: This method is not reliable for Rect2i with a negative size. Use abs to get a positive sized equivalent rectangle to check for contained points.


Returns the intersection of this Rect2i and b.

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


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


Returns a larger Rect2i that contains this Rect2i and b.

Operator Descriptions

Returns true if the rectangles are not equal.


Returns true if the rectangles are equal.