reckfellENGINE

Rect2

A floating-point rectangle: boundaries, intersections and extents.

On this page
struct Rect2ReckfellSharpInherits ValueType

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.

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

Rect24 overloads
# Rect2 · 1/4
C#
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.

Parameters
NameTypeDefault
positionVector2—
sizeVector2—
position

Starting corner.

size

Full width and height, not an ending corner.

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

Parameters
NameTypeDefault
positionVector2—
widthfloat—
heightfloat—
position

Starting corner.

width

Full X extent.

height

Full Y extent.

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

Parameters
NameTypeDefault
xfloat—
yfloat—
sizeVector2—
x

Starting-corner X coordinate.

y

Starting-corner Y coordinate.

size

Full width and height, not an ending corner.

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

Parameters
NameTypeDefault
xfloat—
yfloat—
widthfloat—
heightfloat—
x

Starting-corner X coordinate.

y

Starting-corner Y coordinate.

width

Full X extent.

height

Full Y extent.

Properties4

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

Returns Rect2

A copy with nonnegative extents.

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

Parameters
NameTypeDefault
bRect2—
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(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.

Parameters
NameTypeDefault
otherRect2—
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 Rect2; null and other types return false. A NaN component does not equal its copy; +0 and -0 are equal.

Parameters
NameTypeDefault
objobject—
obj

Object to compare; null is allowed.

Returns bool

true for exact equality.

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

Parameters
NameTypeDefault
toVector2—
to

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

Returns Rect2

A new rectangle with bounds expanded if needed.

GetCenterVector2
# GetCenter
C#
public Vector2 GetCenter();

Computes Position + Size * 0.5f in rectangle coordinates. Odd extents retain fractional halves; does not apply an object Transform2D.

Returns Vector2

Center coordinates with fractional half extents.

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<Rect2, 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.

GetSupportVector2
# GetSupport
C#
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.

Parameters
NameTypeDefault
directionVector2—
direction

Direction in rectangle coordinates; magnitude does not affect selection when signs stay the same.

Returns Vector2

The selected corner in rectangle coordinates.

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

Parameters
NameTypeDefault
byfloat—
by

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

Returns Rect2

The new rectangle; the original is unchanged.

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

Parameters
NameTypeDefault
leftfloat—
topfloat—
rightfloat—
bottomfloat—
left

Left-side padding.

top

Top-side padding.

right

Right-side padding.

bottom

Bottom-side padding.

Returns Rect2

The new rectangle; the original is unchanged.

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

Parameters
NameTypeDefault
sideSide—
byfloat—
side

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

by

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

Returns Rect2

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(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.

Parameters
NameTypeDefault
pointVector2—
point

Point in rectangle coordinates.

Returns bool

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

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

Parameters
NameTypeDefault
bRect2—
b

Rectangle with nonnegative Size in the same coordinate system.

Returns Rect2

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

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

Parameters
NameTypeDefault
bRect2—
includeBordersboolfalse
b

Other rectangle in the same coordinate system.

includeBorders

Defaults to false; true includes side and corner contact. Does not change HasPoint() boundary rules.

Returns bool

The result of the interval tests on both axes.

IsEqualApproxbool
# IsEqualApprox
C#
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.

Parameters
NameTypeDefault
otherRect2—
other

The second rectangle for tolerance checks.

Returns bool

true when all four component comparisons pass.

IsFinitebool
# IsFinite
C#
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().

Returns bool

true when all four components are finite.

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

Parameters
NameTypeDefault
bRect2—
b

Rectangle whose bounds must also be enclosed.

Returns Rect2

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 "F2" for two decimal places; null selects the default format.

Returns string

Text containing Position and Size.

op_Equalitybool
# op_Equality
C#
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.

Parameters
NameTypeDefault
leftRect2—
rightRect2—
left

Left rectangle.

right

Right rectangle.

Returns bool

true for exact equality.

op_Inequalitybool
# op_Inequality
C#
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.

Parameters
NameTypeDefault
leftRect2—
rightRect2—
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.