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 to Rect2
is .
Tutorials
Methods
- Vector2 end
Ending corner. This is calculated as position + size
. Setting this value will change the size.
- position
Beginning corner. Typically has values lower than end.
- size
Size from position to . Typically, all components are positive.
If the size is negative, you can use abs to fix it.
Method Descriptions
Constructs a Rect2
by position and size.
Constructs a Rect2
by x, y, width, and height.
- Rect2 abs ( )
Returns a Rect2
with equivalent position and area, modified so that the top-left corner is the origin and width
and height
are positive.
- clip ( Rect2 b )
Returns the intersection of this and b.
- encloses ( Rect2 b )
Returns true
if this Rect2
completely encloses another one.
Returns a copy of this Rect2
expanded to include a given point.
Example:
- get_area ( )
Returns the area of the Rect2
. See also has_no_area.
- get_center ( )
Returns the center of the Rect2
, which is equal to position + ( / 2).
- Rect2 grow ( by )
Returns a copy of the Rect2
grown a given amount of units towards all the sides.
Returns a copy of the Rect2
grown a given amount of units towards each direction individually.
- grow_margin ( int margin, by )
Returns a copy of the Rect2
grown a given amount of units towards the Margin direction.
- has_no_area ( )
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, will return true
.
- bool has_point ( point )
Returns 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.
- intersects ( Rect2 b, include_borders=false )
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.
- Rect2 merge ( b )
Returns a larger Rect2
that contains this and b
.