reckfellENGINE

Rect2I

An integer-coordinate rectangle: cells, boundaries, intersections and extents.

On this page
struct Rect2IReckfellSharpInherits ValueType

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.

C#
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
# Rect2I · 1/4
C#
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.

Parameters
NameTypeDefault
positionVector2I—
sizeVector2I—
position

Starting corner.

size

Full width and height, not an ending corner.

# Rect2I · 2/4
C#
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.

Parameters
NameTypeDefault
positionVector2I—
widthint—
heightint—
position

Starting corner.

width

Full X extent.

height

Full Y extent.

# Rect2I · 3/4
C#
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.

Parameters
NameTypeDefault
xint—
yint—
sizeVector2I—
x

Starting-corner X coordinate.

y

Starting-corner Y coordinate.

size

Full width and height, not an ending corner.

# Rect2I · 4/4
C#
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.

Parameters
NameTypeDefault
xint—
yint—
widthint—
heightint—
x

Starting-corner X coordinate.

y

Starting-corner Y coordinate.

width

Full X extent.

height

Full Y extent.

Properties4

AreaProperty
# Area
C#
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
# End
C#
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
# Position
C#
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
# Size
C#
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
# Abs
C#
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.

Returns Rect2I

A copy with nonnegative extents.

Enclosesbool
# Encloses
C#
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.

Parameters
NameTypeDefault
bRect2I—
b

Rectangle to enclose, in the same coordinate system.

Returns bool

true when all b bounds lie within this rectangle inclusive bounds.

Equals2 overloads
# Equals · 1/2
C#
public bool Equals(Rect2I other);

Compares Position and Size exactly, without normalization, tolerance or an overlap test.

Parameters
NameTypeDefault
otherRect2I—
other

Other rectangle for exact comparison.

Returns bool

true for exact equality.

# Equals · 2/2
C#
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.

Parameters
NameTypeDefault
objobject—
obj

Object to compare; null is allowed.

Returns bool

true for exact equality.

ExpandRect2I
# Expand
C#
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.

Parameters
NameTypeDefault
toVector2I—
to

Point in rectangle coordinates; the result maximum boundary may coincide with it.

Returns Rect2I

A new rectangle with bounds expanded if needed.

GetCenterVector2I
# GetCenter
C#
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.

Returns Vector2I

Center coordinates with integer truncation of half the size.

GetHashCodeint
# GetHashCode
C#
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.

Returns int

The hash of the current Position and Size.

GrowRect2I
# Grow
C#
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.

Parameters
NameTypeDefault
byint—
by

Padding on the selected side or each side, in rectangle-coordinate units.

Returns Rect2I

The new rectangle; the original is unchanged.

GrowIndividualRect2I
# GrowIndividual
C#
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.

Parameters
NameTypeDefault
leftint—
topint—
rightint—
bottomint—
left

Left-side padding.

top

Top-side padding.

right

Right-side padding.

bottom

Bottom-side padding.

Returns Rect2I

The new rectangle; the original is unchanged.

GrowSideRect2I
# GrowSide
C#
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.

Parameters
NameTypeDefault
sideSide—
byint—
side

Side.Left, Side.Top, Side.Right or Side.Bottom.

by

Padding on the selected side or each side, in rectangle-coordinate units.

Returns Rect2I

The new rectangle; the original is unchanged.

HasAreabool
# HasArea
C#
public bool HasArea();

Returns Size.X > 0 && Size.Y > 0. This checks positive extents; Area > 0 is not equivalent when components are negative.

Returns bool

true only for positive width and height.

HasPointbool
# HasPoint
C#
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.

Parameters
NameTypeDefault
pointVector2I—
point

Point in rectangle coordinates.

Returns bool

true when the point belongs to the half-open rectangle.

IntersectionRect2I
# Intersection
C#
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.

Parameters
NameTypeDefault
bRect2I—
b

Rectangle with nonnegative Size in the same coordinate system.

Returns Rect2I

The common region, or default when Intersects(b) fails.

Intersectsbool
# Intersects
C#
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.

Parameters
NameTypeDefault
bRect2I—
b

Other rectangle in the same coordinate system.

Returns bool

The result of the interval tests on both axes.

MergeRect2I
# Merge
C#
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.

Parameters
NameTypeDefault
bRect2I—
b

Rectangle whose bounds must also be enclosed.

Returns Rect2I

The new enclosing rectangle.

ToString2 overloads
# ToString · 1/2
C#
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).

Returns string

Text containing Position and Size.

# ToString · 2/2
C#
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.

Parameters
NameTypeDefault
formatstring—
format

A .NET numeric format, such as "D3" for leading zeros; null selects the default format.

Returns string

Text containing Position and Size.

op_Equalitybool
# op_Equality
C#
public static bool operator ==(Rect2I left, Rect2I right);

Compares Position and Size exactly, without normalization, tolerance or an overlap test.

Parameters
NameTypeDefault
leftRect2I—
rightRect2I—
left

Left rectangle.

right

Right rectangle.

Returns bool

true for exact equality.

op_ExplicitRect2I
# op_Explicit
C#
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.

Parameters
NameTypeDefault
valueRect2—
value

Floating-point rectangle; validate range before conversion.

Returns Rect2I

The rectangle with truncated position and size components.

op_ImplicitRect2
# op_Implicit
C#
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.

Parameters
NameTypeDefault
valueRect2I—
value

Integer-coordinate rectangle.

Returns Rect2

A float-coordinate rectangle, potentially with precision loss.

op_Inequalitybool
# op_Inequality
C#
public static bool operator !=(Rect2I left, Rect2I right);

Compares Position and Size exactly, without normalization, tolerance or an overlap test. Operator != negates this comparison.

Parameters
NameTypeDefault
leftRect2I—
rightRect2I—
left

Left rectangle.

right

Right rectangle.

Returns bool

true for inequality.

Reckfell Engine documentationBack to top ↑
QUICK LINKSGetting startedEntity hierarchyC# & .NET 10Scenes and prefabs
Reckfell guides and full API K
reckfellENGINE
RECKFELL ACCOUNT

Sign in

Sign in to your Reckfell account.

One account for the entire Reckfell platform.