Rect2
A floating-point rectangle: boundaries, intersections and extents.
Rect2 is a C# struct storing a starting corner, Position, and full extents, Size. End = Position + Size. Sides are axis-aligned; the type stores no rotation. Assignment copies the value, and Abs(), Grow(), Expand() and Merge() return a new rectangle.
HasPoint() includes Position and excludes End: Position.X <= x < End.X and Position.Y <= y < End.Y. Adjacent rectangles therefore do not share contained points on their common side. For positive extents, Intersects() excludes contact alone, and Intersection() returns default for contact without retaining its location. Expand(point) can place point on End, so HasPoint(point) may still return false afterward.
Use the same coordinate system for the rectangle and tested points. Call Abs() first for negative Size; HasArea() requires positive extents. There is no C# conversion of this type to bool: choose the explicit test your task needs.
Coordinates and extents are float values. Validate finite rectangle and input-point coordinates before geometric tests; those tests do not validate numbers automatically. IsFinite() checks for NaN and infinities. Intersects(other, includeBorders: true) includes side and corner contact. Transform2D * Rect2 returns the enclosing rectangle of transformed corners; the result remains axis-aligned.
using Reckfell;
public static class RectangleExamples
{
public static bool Validate()
{
var bounds = new Rect2(0, 0, 2, 2);
var neighbor = new Rect2(2, 0, 2, 2);
bool excludesEnd = !bounds.HasPoint(bounds.End);
bool touches = bounds.Intersects(neighbor, includeBorders: true);
bool overlaps = bounds.Intersects(neighbor);
var cells = new Rect2I(0, 0, 2, 2);
var cell = new Vector2I(2, 1);
bool expandStillExcludes = !cells.Expand(cell).HasPoint(cell);
Rect2I withCell = cells.Merge(new Rect2I(cell, Vector2I.One));
return excludesEnd && touches && !overlaps
&& expandStillExcludes && withCell.HasPoint(cell)
&& withCell.Size == new Vector2I(3, 2);
}
}Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors4
Rect24 overloads
public Rect2(Vector2 position, Vector2 size);Constructs the rectangle from Position and full Size, not two corners. Stores values directly without normalizing negative extents. default(Rect2) has zero position and size.
| Name | Type | Default |
|---|---|---|
position | Vector2 | — |
size | Vector2 | — |
positionStarting corner.
sizeFull width and height, not an ending corner.
public Rect2(Vector2 position, float width, float height);Constructs the rectangle from Position and full Size, not two corners. Stores values directly without normalizing negative extents. default(Rect2) has zero position and size.
| Name | Type | Default |
|---|---|---|
position | Vector2 | — |
width | float | — |
height | float | — |
positionStarting corner.
widthFull X extent.
heightFull Y extent.
public Rect2(float x, float y, Vector2 size);Constructs the rectangle from Position and full Size, not two corners. Stores values directly without normalizing negative extents. default(Rect2) has zero position and size.
| Name | Type | Default |
|---|---|---|
x | float | — |
y | float | — |
size | Vector2 | — |
xStarting-corner X coordinate.
yStarting-corner Y coordinate.
sizeFull width and height, not an ending corner.
public Rect2(float x, float y, float width, float height);Constructs the rectangle from Position and full Size, not two corners. Stores values directly without normalizing negative extents. default(Rect2) has zero position and size.
| Name | Type | Default |
|---|---|---|
x | float | — |
y | float | — |
width | float | — |
height | float | — |
xStarting-corner X coordinate.
yStarting-corner Y coordinate.
widthFull X extent.
heightFull Y extent.
Properties4
AreaProperty
public float Area { get; }Computes Size.X * Size.Y without normalization. Two negative extents also produce a positive product; use HasArea() to require positive extents.
EndProperty
public Vector2 End { get; set; }Ending corner, Position + Size. Assignment changes Size to value - Position without moving Position. HasPoint() excludes the right and bottom boundaries at these coordinates.
PositionProperty
public Vector2 Position { get; set; }Starting corner. Assignment preserves Size and shifts End by the same amount. The calling code determines the coordinate system.
SizeProperty
public Vector2 Size { get; set; }Full width and height: End = Position + Size. Assignment preserves Position. Negative components are not corrected; use Abs() to normalize them.
Methods22
AbsRect2
public Rect2 Abs();Returns a copy with Position set to the componentwise minimum of the original Position and End, and Size to the absolute original Size. Zero extents remain zero. The original is unchanged.
Rect2A copy with nonnegative extents.
Enclosesbool
public bool Encloses(Rect2 b);Tests complete containment of b Position and End with inclusive bounds. An equal rectangle is enclosed; a zero rectangle on End can also be enclosed even though HasPoint(End) is false. These are distinct tests. Use nonnegative extents for both rectangles.
| Name | Type | Default |
|---|---|---|
b | Rect2 | — |
bRectangle to enclose, in the same coordinate system.
booltrue when all b bounds lie within this rectangle inclusive bounds.
Equals2 overloads
public bool Equals(Rect2 other);Compares Position and Size exactly, without normalization, tolerance or an overlap test. A NaN component does not equal its copy; +0 and -0 are equal.
| Name | Type | Default |
|---|---|---|
other | Rect2 | — |
otherOther rectangle for exact comparison.
booltrue for exact equality.
public override bool Equals(object? obj);Compares Position and Size exactly, without normalization, tolerance or an overlap test. For obj, accepts only a boxed Rect2; null and other types return false. A NaN component does not equal its copy; +0 and -0 are equal.
| Name | Type | Default |
|---|---|---|
obj | object | — |
objObject to compare; null is allowed.
booltrue for exact equality.
ExpandRect2
public Rect2 Expand(Vector2 to);Returns the smallest copy whose bounds cover the original bounds and to. A point expanding the right or bottom side becomes part of End and remains excluded by HasPoint(). No padding is added automatically. If the point must lie strictly inside, provide the required padding explicitly.
| Name | Type | Default |
|---|---|---|
to | Vector2 | — |
toPoint in rectangle coordinates; the result maximum boundary may coincide with it.
Rect2A new rectangle with bounds expanded if needed.
GetCenterVector2
public Vector2 GetCenter();Computes Position + Size * 0.5f in rectangle coordinates. Odd extents retain fractional halves; does not apply an object Transform2D.
Vector2Center coordinates with fractional half extents.
GetHashCodeint
public override int GetHashCode();Returns HashCode.Combine(Position, Size). Exactly equal values have equal hashes, but a hash match does not prove equality. By default, Dictionary<Rect2, T> uses exact Equals(), not approximate comparison. Do not persist the hash as an identifier or checksum across runs.
intThe hash of the current Position and Size.
GetSupportVector2
public Vector2 GetSupport(Vector2 direction);Selects a corner of the closed bounds: a positive direction component selects the corresponding End component; otherwise it selects Position. No normalization is needed; zero direction returns Position. A corner on End can be excluded by HasPoint() while still being the support point.
| Name | Type | Default |
|---|---|---|
direction | Vector2 | — |
directionDirection in rectangle coordinates; magnitude does not affect selection when signs stay the same.
Vector2The selected corner in rectangle coordinates.
GrowRect2
public Rect2 Grow(float by);Returns a copy with Position - Vector2.One * by and Size + Vector2.One * (2 * by). Padding applies to each of four sides, not once to the full size. Negative padding shrinks the region. Shrinking is not clamped at zero: excessive shrinking creates negative Size.
| Name | Type | Default |
|---|---|---|
by | float | — |
byPadding on the selected side or each side, in rectangle-coordinate units.
Rect2The new rectangle; the original is unchanged.
GrowIndividualRect2
public Rect2 GrowIndividual(float left, float top, float right, float bottom);Returns a copy with Position = Position - (left, top) and Size = Size + (left + right, top + bottom). Argument order is left, top, right, bottom. Negative padding shrinks the region. Shrinking is not clamped at zero: excessive shrinking creates negative Size.
| Name | Type | Default |
|---|---|---|
left | float | — |
top | float | — |
right | float | — |
bottom | float | — |
leftLeft-side padding.
topTop-side padding.
rightRight-side padding.
bottomBottom-side padding.
Rect2The new rectangle; the original is unchanged.
GrowSideRect2
public Rect2 GrowSide(Side side, float by);Returns a copy changing only Side.Left, Side.Top, Side.Right or Side.Bottom. Left and top growth also changes Position. An unknown Side value leaves the copy unchanged. Negative padding shrinks the region. Shrinking is not clamped at zero: excessive shrinking creates negative Size.
| Name | Type | Default |
|---|---|---|
side | Side | — |
by | float | — |
sideSide.Left, Side.Top, Side.Right or Side.Bottom.
byPadding on the selected side or each side, in rectangle-coordinate units.
Rect2The new rectangle; the original is unchanged.
HasAreabool
public bool HasArea();Returns Size.X > 0 && Size.Y > 0. This checks positive extents; Area > 0 is not equivalent when components are negative.
booltrue only for positive width and height.
HasPointbool
public bool HasPoint(Vector2 point);For finite coordinates: Tests Position.X <= point.X < End.X and Position.Y <= point.Y < End.Y. Left and top borders are included; right and bottom are excluded. A zero width or height contains no points. Call Abs() first for negative Size. NaN is not automatically rejected and can make this method return true for the point. Check IsFinite() on the bounds and the input point separately.
| Name | Type | Default |
|---|---|---|
point | Vector2 | — |
pointPoint in rectangle coordinates.
booltrue when the point belongs to the half-open rectangle.
IntersectionRect2
public Rect2 Intersection(Rect2 b);First calls Intersects(b) with its ordinary rules, then clips the common bounds. Separated rectangles and contact alone between positive-size rectangles return default(Rect2), a zero rectangle at the origin. This result does not retain the contact location. Normalize negative extents with Abs() before calling.
| Name | Type | Default |
|---|---|---|
b | Rect2 | — |
bRectangle with nonnegative Size in the same coordinate system.
Rect2The common region, or default when Intersects(b) fails.
Intersectsbool
public bool Intersects(Rect2 b, bool includeBorders = false);Tests intervals on X and Y. For positive extents, side- or corner-only contact returns false with includeBorders = false and true with includeBorders = true. Does not call HasArea(): a degenerate rectangle strictly inside another can return true. Both inputs need nonnegative Size and the same coordinate system.
| Name | Type | Default |
|---|---|---|
b | Rect2 | — |
includeBorders | bool | false |
bOther rectangle in the same coordinate system.
includeBordersDefaults to false; true includes side and corner contact. Does not change HasPoint() boundary rules.
boolThe result of the interval tests on both axes.
IsEqualApproxbool
public bool IsEqualApprox(Rect2 other);Compares Position and Size componentwise using Mathf.IsEqualApprox(). In the current float SDK, exact equality succeeds immediately; otherwise abs(a - b) < max(0.000001, 0.000001 * abs(a)) is required, with a taken from this rectangle. Operand order can affect boundary results; the test is not transitive. It does not replace Dictionary key equality or validate finiteness: equal infinities pass.
| Name | Type | Default |
|---|---|---|
other | Rect2 | — |
otherThe second rectangle for tolerance checks.
booltrue when all four component comparisons pass.
IsFinitebool
public bool IsFinite();Checks all four Position and Size components for NaN and infinity. Zero or negative extents can be finite: this does not replace HasArea().
booltrue when all four components are finite.
MergeRect2
public Rect2 Merge(Rect2 b);Returns the smallest rectangle enclosing both inputs. Any gap between separated regions is also included. Neither input is changed. Both rectangles need nonnegative Size and the same coordinate system.
| Name | Type | Default |
|---|---|---|
b | Rect2 | — |
bRectangle whose bounds must also be enclosed.
Rect2The new enclosing rectangle.
ToString2 overloads
public override string ToString();Returns Position.ToString(format) + ", " + Size.ToString(format), with two parenthesized vectors. Numbers use InvariantCulture. This is diagnostic text, not a save format or JSON. Equivalent to ToString(null).
stringText containing Position and Size.
public string ToString(string? format);Returns Position.ToString(format) + ", " + Size.ToString(format), with two parenthesized vectors. Numbers use InvariantCulture. This is diagnostic text, not a save format or JSON. The format applies to all four components; an invalid format can throw FormatException.
| Name | Type | Default |
|---|---|---|
format | string | — |
formatA .NET numeric format, such as "F2" for two decimal places; null selects the default format.
stringText containing Position and Size.
op_Equalitybool
public static bool operator ==(Rect2 left, Rect2 right);Compares Position and Size exactly, without normalization, tolerance or an overlap test. A NaN component does not equal its copy; +0 and -0 are equal.
| Name | Type | Default |
|---|---|---|
left | Rect2 | — |
right | Rect2 | — |
leftLeft rectangle.
rightRight rectangle.
booltrue for exact equality.
op_Inequalitybool
public static bool operator !=(Rect2 left, Rect2 right);Compares Position and Size exactly, without normalization, tolerance or an overlap test. Operator != negates this comparison. A NaN component does not equal its copy; +0 and -0 are equal.
| Name | Type | Default |
|---|---|---|
left | Rect2 | — |
right | Rect2 | — |
leftLeft rectangle.
rightRight rectangle.
booltrue for inequality.