Rect2I
An integer-coordinate rectangle: cells, boundaries, intersections and extents.
Rect2I 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 int values. Keep sums, differences and area representable as int: overflow can corrupt the bounds. Abs() throws OverflowException for a Size component equal to int.MinValue. To include a whole cell, merge with Rect2I(cell, Vector2I.One), as in the example.
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
Rect2I4 overloads
public Rect2I(Vector2I position, Vector2I size);Constructs the rectangle from Position and full Size, not two corners. Stores values directly without normalizing negative extents. default(Rect2I) has zero position and size.
| Name | Type | Default |
|---|---|---|
position | Vector2I | — |
size | Vector2I | — |
positionStarting corner.
sizeFull width and height, not an ending corner.
public Rect2I(Vector2I position, int width, int height);Constructs the rectangle from Position and full Size, not two corners. Stores values directly without normalizing negative extents. default(Rect2I) has zero position and size.
| Name | Type | Default |
|---|---|---|
position | Vector2I | — |
width | int | — |
height | int | — |
positionStarting corner.
widthFull X extent.
heightFull Y extent.
public Rect2I(int x, int y, Vector2I size);Constructs the rectangle from Position and full Size, not two corners. Stores values directly without normalizing negative extents. default(Rect2I) has zero position and size.
| Name | Type | Default |
|---|---|---|
x | int | — |
y | int | — |
size | Vector2I | — |
xStarting-corner X coordinate.
yStarting-corner Y coordinate.
sizeFull width and height, not an ending corner.
public Rect2I(int x, int y, int width, int height);Constructs the rectangle from Position and full Size, not two corners. Stores values directly without normalizing negative extents. default(Rect2I) has zero position and size.
| Name | Type | Default |
|---|---|---|
x | int | — |
y | int | — |
width | int | — |
height | int | — |
xStarting-corner X coordinate.
yStarting-corner Y coordinate.
widthFull X extent.
heightFull Y extent.
Properties4
AreaProperty
public int Area { get; }Computes Size.X * Size.Y without normalization. Two negative extents also produce a positive product; use HasArea() to require positive extents. In the installed SDK, int multiplication can overflow without clamping the result.
EndProperty
public Vector2I 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 Vector2I Position { get; set; }Starting corner. Assignment preserves Size and shifts End by the same amount. The calling code determines the coordinate system.
SizeProperty
public Vector2I Size { get; set; }Full width and height: End = Position + Size. Assignment preserves Position. Negative components are not corrected; use Abs() to normalize them.
Methods21
AbsRect2I
public Rect2I 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. A Size component equal to int.MinValue throws OverflowException; also avoid Position + Size overflow.
Rect2IA copy with nonnegative extents.
Enclosesbool
public bool Encloses(Rect2I 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 | Rect2I | — |
bRectangle to enclose, in the same coordinate system.
booltrue when all b bounds lie within this rectangle inclusive bounds.
Equals2 overloads
public bool Equals(Rect2I other);Compares Position and Size exactly, without normalization, tolerance or an overlap test.
| Name | Type | Default |
|---|---|---|
other | Rect2I | — |
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 Rect2I; null and other types return false.
| Name | Type | Default |
|---|---|---|
obj | object | — |
objObject to compare; null is allowed.
booltrue for exact equality.
ExpandRect2I
public Rect2I Expand(Vector2I 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. To include a whole cell, use Merge(new Rect2I(to, Vector2I.One)), first ensuring to + Vector2I.One cannot overflow.
| Name | Type | Default |
|---|---|---|
to | Vector2I | — |
toPoint in rectangle coordinates; the result maximum boundary may coincide with it.
Rect2IA new rectangle with bounds expanded if needed.
GetCenterVector2I
public Vector2I GetCenter();Computes Position + Size / 2: first divides Size components using integer division toward zero, then adds Position. For odd positive extents, the result is biased toward Position even at negative coordinates. For example, Position.X = -10 and Size.X = 3 produce -9.
Vector2ICenter coordinates with integer truncation of half the size.
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<Rect2I, 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.
GrowRect2I
public Rect2I Grow(int by);Returns a copy with Position - Vector2I.One * by and Size + Vector2I.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 | int | — |
byPadding on the selected side or each side, in rectangle-coordinate units.
Rect2IThe new rectangle; the original is unchanged.
GrowIndividualRect2I
public Rect2I GrowIndividual(int left, int top, int right, int 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 | int | — |
top | int | — |
right | int | — |
bottom | int | — |
leftLeft-side padding.
topTop-side padding.
rightRight-side padding.
bottomBottom-side padding.
Rect2IThe new rectangle; the original is unchanged.
GrowSideRect2I
public Rect2I GrowSide(Side side, int 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 | int | — |
sideSide.Left, Side.Top, Side.Right or Side.Bottom.
byPadding on the selected side or each side, in rectangle-coordinate units.
Rect2IThe 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(Vector2I point);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.
| Name | Type | Default |
|---|---|---|
point | Vector2I | — |
pointPoint in rectangle coordinates.
booltrue when the point belongs to the half-open rectangle.
IntersectionRect2I
public Rect2I Intersection(Rect2I 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(Rect2I), 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 | Rect2I | — |
bRectangle with nonnegative Size in the same coordinate system.
Rect2IThe common region, or default when Intersects(b) fails.
Intersectsbool
public bool Intersects(Rect2I b);Tests intervals on X and Y. For positive extents, side- or corner-only contact returns false. This overload has no includeBorders argument. 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 | Rect2I | — |
bOther rectangle in the same coordinate system.
boolThe result of the interval tests on both axes.
MergeRect2I
public Rect2I Merge(Rect2I 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 | Rect2I | — |
bRectangle whose bounds must also be enclosed.
Rect2IThe 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 "D3" for leading zeros; null selects the default format.
stringText containing Position and Size.
op_Equalitybool
public static bool operator ==(Rect2I left, Rect2I right);Compares Position and Size exactly, without normalization, tolerance or an overlap test.
| Name | Type | Default |
|---|---|---|
left | Rect2I | — |
right | Rect2I | — |
leftLeft rectangle.
rightRight rectangle.
booltrue for exact equality.
op_ExplicitRect2I
public static explicit operator Rect2I(Rect2 value);Converts Rect2 to Rect2I by truncating Position and Size components independently toward zero. For example, (-1.9, 1.9), (2.9, 3.9) becomes (-1, 1), (2, 3). This neither rounds End nor constructs an enclosing pixel region. Use finite values representable as int.
| Name | Type | Default |
|---|---|---|
value | Rect2 | — |
valueFloating-point rectangle; validate range before conversion.
Rect2IThe rectangle with truncated position and size components.
op_ImplicitRect2
public static implicit operator Rect2(Rect2I value);Converts Position and Size from int to float and constructs Rect2. No explicit cast is required, but large integers can lose precision: 16777217 becomes 16777216 in the current float SDK. Converting back need not restore the original coordinates.
| Name | Type | Default |
|---|---|---|
value | Rect2I | — |
valueInteger-coordinate rectangle.
Rect2A float-coordinate rectangle, potentially with precision loss.
op_Inequalitybool
public static bool operator !=(Rect2I left, Rect2I right);Compares Position and Size exactly, without normalization, tolerance or an overlap test. Operator != negates this comparison.
| Name | Type | Default |
|---|---|---|
left | Rect2I | — |
right | Rect2I | — |
leftLeft rectangle.
rightRight rectangle.
booltrue for inequality.