Table of Contents

Struct Rectangle2

Namespace
Thunder.UnitsNET.Vectors.Geometry
Assembly
Thunder.UnitsNET.Vectors.Geometry.dll

A positioned, optionally-rotated rectangle in 2D space, defined by its centre, dimensions (width × height), and orientation.

Rectangle2 is a readonly record struct: it is value-type, immutable, and supports equality by value. All computed properties (corners, area, etc.) are derived on demand from the three stored fields — no extra memory is used.

The axis-aligned case (no rotation) is the default. Rotation is expressed as a Direction2 that points in the "right" direction of the rectangle. At zero rotation (Zero), "right" is East (+X) and "up" is North (+Y), giving the familiar axis-aligned layout.

public readonly record struct Rectangle2 : IEquatable<Rectangle2>
Implements
Inherited Members
Extension Methods

Examples

// Axis-aligned rectangle centred at the origin, 10 m wide × 6 m tall
var r = new Rectangle2(LengthPoint2.FromMeters(0, 0), LengthVector2.FromMeters(10, 6));

bool inside  = r.DoesContain(LengthPoint2.FromMeters(3, 2));     // true
bool outside = r.DoesContain(LengthPoint2.FromMeters(6, 4));     // false
Length dist  = r.GetDistanceFrom(LengthPoint2.FromMeters(8, 0)); // 3 m

// Rotated 45°
var rotated = new Rectangle2(
    LengthPoint2.FromMeters(0, 0),
    LengthVector2.FromMeters(4, 2),
    Direction2.FromDegrees(45));

Constructors

Rectangle2(LengthPoint2, LengthVector2)

Constructs an axis-aligned Rectangle2 (zero rotation).

public Rectangle2(LengthPoint2 center, LengthVector2 dimensions)

Parameters

center LengthPoint2

The centre of the rectangle.

dimensions LengthVector2

Width (X) and height (Y).

Rectangle2(LengthPoint2, LengthVector2, Direction2)

Constructs a Rectangle2 with an explicit rotation.

public Rectangle2(LengthPoint2 center, LengthVector2 dimensions, Direction2 rotation)

Parameters

center LengthPoint2

The centre of the rectangle.

dimensions LengthVector2

Width (X) and height (Y). Both should be non-negative.

rotation Direction2

The orientation of the rectangle.

Properties

Area

The area of the rectangle (Width × Height).

public Area Area { get; }

Property Value

Area

AspectRatio

The aspect ratio of the rectangle (Width / Height).

public double AspectRatio { get; }

Property Value

double

BottomLeft

The bottom-left corner of the rectangle (respects rotation).

public LengthPoint2 BottomLeft { get; }

Property Value

LengthPoint2

BottomRight

The bottom-right corner of the rectangle (respects rotation).

public LengthPoint2 BottomRight { get; }

Property Value

LengthPoint2

Center

The centre point of the rectangle in world space.

public LengthPoint2 Center { get; init; }

Property Value

LengthPoint2

Corners

All four corners in order: [TopLeft, TopRight, BottomLeft, BottomRight].

public IReadOnlyList<LengthPoint2> Corners { get; }

Property Value

IReadOnlyList<LengthPoint2>

Diagonal

The diagonal length (√(Width² + Height²)), the distance between opposite corners.

public Length Diagonal { get; }

Property Value

Length

Dimensions

The dimensions of the rectangle: X = width (along the rectangle's right axis), Y = height (along the rectangle's up axis). Both should be non-negative.

public LengthVector2 Dimensions { get; init; }

Property Value

LengthVector2

Height

The height of the rectangle (Y dimension).

public Length Height { get; }

Property Value

Length

IsSquare

Returns true if the rectangle is a square (Width == Height).

public bool IsSquare { get; }

Property Value

bool

MiddleBottom

Midpoint of the bottom edge.

public LengthPoint2 MiddleBottom { get; }

Property Value

LengthPoint2

MiddleLeft

Midpoint of the left edge.

public LengthPoint2 MiddleLeft { get; }

Property Value

LengthPoint2

MiddleRight

Midpoint of the right edge.

public LengthPoint2 MiddleRight { get; }

Property Value

LengthPoint2

MiddleTop

Midpoint of the top edge.

public LengthPoint2 MiddleTop { get; }

Property Value

LengthPoint2

Perimeter

The perimeter of the rectangle (2 × (Width + Height)).

public Length Perimeter { get; }

Property Value

Length

Rotation

The orientation of the rectangle. Zero (0°) means axis-aligned: "right" points East and "up" points North.

public Direction2 Rotation { get; init; }

Property Value

Direction2

TopLeft

The top-left corner of the rectangle (respects rotation).

"Top" means in the rectangle's up direction, "left" means against the right direction. For axis-aligned rectangles this is the conventional (min-X, max-Y) corner.

public LengthPoint2 TopLeft { get; }

Property Value

LengthPoint2

TopRight

The top-right corner of the rectangle (respects rotation).

public LengthPoint2 TopRight { get; }

Property Value

LengthPoint2

Width

The width of the rectangle (X dimension).

public Length Width { get; }

Property Value

Length

Methods

Contains(LengthPoint2)

Returns true if point is inside or on the boundary of this rectangle, correctly handling rotated rectangles.

The test projects the point into the rectangle's local coordinate space (aligned with its width and height axes), then checks whether the projected coordinates are within the half-extents.

public bool Contains(LengthPoint2 point)

Parameters

point LengthPoint2

The world-space point to test.

Returns

bool

true if the point is inside or on the rectangle.

Examples

var rect = new Rectangle2(LengthPoint2.FromMeters(0, 0), LengthVector2.FromMeters(4, 2));
rect.Contains(LengthPoint2.FromMeters(1, 0.5));  // true  — inside
rect.Contains(LengthPoint2.FromMeters(3, 0));    // false — outside (half-width is 2)

DistanceTo(LengthPoint2)

Returns the shortest distance from point to the rectangle's boundary. Returns UnitsNet.Length.Zero if the point is inside or on the rectangle.

This is the canonical DistanceTo method. The earlier GetDistanceFrom(LengthPoint2) method is equivalent and is kept for backwards compatibility.

public Length DistanceTo(LengthPoint2 point)

Parameters

point LengthPoint2

The world-space query point.

Returns

Length

Distance to the nearest edge or corner, or zero if already inside.

Examples

var rect = new Rectangle2(LengthPoint2.FromMeters(0, 0), LengthVector2.FromMeters(4, 2));
rect.DistanceTo(LengthPoint2.FromMeters(5, 0)); // 3 m  (outside, right of rect)
rect.DistanceTo(LengthPoint2.FromMeters(1, 0)); // 0 m  (inside)

DoesContain(LengthPoint2)

Returns true if point lies inside or on the boundary of this rectangle (including rotated cases).

Algorithm: transform the point into the rectangle's local coordinate frame by projecting it onto the "right" and "up" axes. In local space the rectangle is axis-aligned with extents ±Width/2 and ±Height/2.

public bool DoesContain(LengthPoint2 point)

Parameters

point LengthPoint2

The world-space point to test.

Returns

bool

true if the point is inside or on the rectangle.

FromCorners(LengthPoint2, LengthPoint2)

Creates an axis-aligned Rectangle2 from its top-left and bottom-right corners.

public static Rectangle2 FromCorners(LengthPoint2 topLeft, LengthPoint2 bottomRight)

Parameters

topLeft LengthPoint2

The top-left corner (min-X, max-Y).

bottomRight LengthPoint2

The bottom-right corner (max-X, min-Y).

Returns

Rectangle2

An axis-aligned Rectangle2.

FromTopLeft(LengthPoint2, LengthVector2)

Creates an axis-aligned Rectangle2 from its top-left corner and dimensions.

The centre is computed as (topLeft.X + width/2, topLeft.Y - height/2).

public static Rectangle2 FromTopLeft(LengthPoint2 topLeft, LengthVector2 dimensions)

Parameters

topLeft LengthPoint2

The top-left corner (min-X, max-Y).

dimensions LengthVector2

Width (X) and height (Y).

Returns

Rectangle2

An axis-aligned Rectangle2.

GetBoundingRectangle()

Returns the smallest axis-aligned bounding rectangle that completely contains this rectangle.

For an unrotated rectangle, the bounding rectangle has the same dimensions. For a rotated rectangle, the bounding box is enlarged to fully enclose all four rotated corners.

public Rectangle2 GetBoundingRectangle()

Returns

Rectangle2

An axis-aligned Rectangle2 bounding this rectangle.

GetClosestPoint(LengthPoint2)

Returns the point on the rectangle's boundary (or interior) that is nearest to point. If the point is inside the rectangle, the point itself is returned.

The algorithm projects point into the rectangle's local coordinate space, clamps each local coordinate to the half-extents, then projects the clamped point back to world space.

public LengthPoint2 GetClosestPoint(LengthPoint2 point)

Parameters

point LengthPoint2

The world-space query point.

Returns

LengthPoint2

The nearest point on or inside the rectangle.

Examples

var rect = new Rectangle2(LengthPoint2.FromMeters(0, 0), LengthVector2.FromMeters(4, 2));
// Point at (5, 0) — nearest point is the right edge at (2, 0)
LengthPoint2 closest = rect.GetClosestPoint(LengthPoint2.FromMeters(5, 0));
// closest ≈ (2, 0)

GetDistanceFrom(LengthPoint2)

Returns the shortest distance from point to the rectangle's boundary. Returns zero if the point is inside or on the rectangle.

For a rotated rectangle the point is first transformed into local rectangle space, then the distance is computed as for an axis-aligned rectangle.

public Length GetDistanceFrom(LengthPoint2 point)

Parameters

point LengthPoint2

The world-space point.

Returns

Length

Distance to nearest edge; UnitsNet.Length.Zero if inside.

MoveCenterTo(LengthPoint2)

Returns a new Rectangle2 with its centre moved to newCenter, preserving all other properties.

public Rectangle2 MoveCenterTo(LengthPoint2 newCenter)

Parameters

newCenter LengthPoint2

The new centre position.

Returns

Rectangle2

A translated Rectangle2 centred at newCenter.

Examples

var moved = shape.MoveCenterTo(LengthPoint2.FromMeters(5, 0));

Overlaps(Rectangle2)

Returns true if this rectangle overlaps another rectangle. Correctly handles rotated (oriented bounding box) rectangles using the Separating Axis Theorem (SAT): if no separating axis exists among the four face normals (two per rectangle), the shapes overlap.

public bool Overlaps(Rectangle2 other)

Parameters

other Rectangle2

The rectangle to test against.

Returns

bool

true if the rectangles share any area or touch.

Examples

var a = new Rectangle2(LengthPoint2.FromMeters(0, 0), LengthVector2.FromMeters(4, 4));
var b = new Rectangle2(LengthPoint2.FromMeters(3, 0), LengthVector2.FromMeters(4, 4));
a.Overlaps(b); // true — they share the strip x = [1, 3]

ToString()

Returns the fully qualified type name of this instance.

public override string ToString()

Returns

string

The fully qualified type name.

TryGetManifold(Capsule2, out CollisionManifold)

Tests whether this rectangle overlaps other and, if so, returns the collision manifold describing the penetration.

public bool TryGetManifold(Capsule2 other, out CollisionManifold manifold)

Parameters

other Capsule2

The capsule to test against.

manifold CollisionManifold

When this method returns true, contains the collision data.

Returns

bool

true if the shapes penetrate (overlap strictly greater than zero).

TryGetManifold(Polygon2, out CollisionManifold)

Tests whether this rectangle overlaps other and, if so, returns the collision manifold describing the penetration.

public bool TryGetManifold(Polygon2 other, out CollisionManifold manifold)

Parameters

other Polygon2

The polygon to test against.

manifold CollisionManifold

When this method returns true, contains the collision data.

Returns

bool

true if the shapes penetrate (overlap strictly greater than zero).

TryGetManifold(Rectangle2, out CollisionManifold)

Tests whether this rectangle overlaps other and, if so, returns the collision manifold describing the penetration.

public bool TryGetManifold(Rectangle2 other, out CollisionManifold manifold)

Parameters

other Rectangle2

The other rectangle to test against.

manifold CollisionManifold

When this method returns true, contains the collision data: penetration depth, contact normal (pointing from other toward this rectangle), and the contact point. Otherwise the default value.

Returns

bool

true if the rectangles penetrate (overlap strictly greater than zero); false if they are separated or only touching.

TryGetManifold(Triangle2, out CollisionManifold)

Tests whether this rectangle overlaps other and, if so, returns the collision manifold describing the penetration.

public bool TryGetManifold(Triangle2 other, out CollisionManifold manifold)

Parameters

other Triangle2

The triangle to test against.

manifold CollisionManifold

When this method returns true, contains the collision data.

Returns

bool

true if the shapes penetrate (overlap strictly greater than zero).

Operators

operator +(Rectangle2, LengthVector2)

Translates the rectangle by a displacement vector. Rotation and dimensions are unchanged.

public static Rectangle2 operator +(Rectangle2 rect, LengthVector2 displacement)

Parameters

rect Rectangle2

The rectangle to translate.

displacement LengthVector2

The displacement to add to the centre.

Returns

Rectangle2

A new Rectangle2 with the shifted centre.

operator +(LengthVector2, Rectangle2)

Translates the rectangle (commutative overload).

public static Rectangle2 operator +(LengthVector2 displacement, Rectangle2 rect)

Parameters

displacement LengthVector2

The displacement to add.

rect Rectangle2

The rectangle to translate.

Returns

Rectangle2

A new Rectangle2 with the shifted centre.

v0.7.0 ▼