Class Rectangle
Represents an axis-aligned rectangle defined by its left, top, right, and bottom edges. Immutable: all operations return new instances. Provides convenience properties such as width, height, and midpoint coordinates, as well as methods for creation, translation, scaling, containment checks, intersection, union, inflation/deflation (uniform and asymmetric), and normalization.
public sealed class Rectangle : IBounds, ISize, IEquatable<Rectangle>
- Inheritance
-
Rectangle
- Implements
- Inherited Members
Fields
- Empty
An empty rectangle positioned at the origin (0,0) with zero width and height.
Properties
- Bottom
Gets the vertical position of the bottom edge of the rectangle.
- Height
Gets the height of the rectangle, calculated as
Bottom - Top
.
- Left
Gets the horizontal position of the left edge of the rectangle.
- MidX
Gets the horizontal midpoint of the rectangle.
- MidY
Gets the vertical midpoint of the rectangle.
- Right
Gets the horizontal position of the right edge of the rectangle.
- Top
Gets the vertical position of the top edge of the rectangle.
- Width
Gets the width of the rectangle, calculated as
Right - Left
.
Methods
- Contains(IBounds)
Determines whether this rectangle completely contains another bounds object.
- Contains(IPoint)
Determines whether the specified IPoint lies inside the rectangle (inclusive of edges).
- Contains(double, double)
Determines whether the specified point lies inside the rectangle (inclusive of edges).
- Deconstruct(out double, out double, out double, out double)
Deconstructs the rectangle into its edge coordinates. Enables tuple-style deconstruction:
var (l, t, r, b) = rect;
- Deflate(double)
Returns a new rectangle contracted equally on all sides.
- Deflate(double, double)
Returns a new rectangle contracted by the specified amounts on each axis.
- Expand(double, double)
Returns a new rectangle expanded by the specified amounts in both directions.
- FromCoordinates(double, double, double, double)
Creates a new rectangle from its edge coordinates.
- FromSize(double, double, double, double)
Creates a new rectangle from its top-left coordinate and dimensions.
- Inflate(double)
Returns a new rectangle expanded equally on all sides.
- Inflate(double, double)
Returns a new rectangle expanded by the specified amounts on each axis.
- Intersect(IBounds)
Returns the intersection of this rectangle and another bounds object.
- IntersectsWith(IBounds)
Determines whether this rectangle intersects with another bounds object.
- Normalize()
Returns a new rectangle with ordered edges such that Left ≤ Right and Top ≤ Bottom.
- Scale(double, double)
Returns a new rectangle scaled by the specified factors relative to the origin (0,0).
- Translate(double, double)
Returns a new rectangle translated (moved) by the specified offsets along the X and Y axes.
- Union(IBounds)
Returns the smallest rectangle that contains both this rectangle and another bounds object.
Operators
- operator +(Point, Rectangle)
Translates a rectangle by a point offset.
- operator +(Rectangle, Point)
Translates a rectangle by a point offset.
- operator /(Rectangle, double)
Uniformly scales a rectangle relative to the origin by dividing by a scalar.
- operator ==(Rectangle, Rectangle)
Determines whether two rectangles have identical edges.
- operator !=(Rectangle, Rectangle)
Determines whether two rectangles differ in any edge.
- operator *(Rectangle, double)
Uniformly scales a rectangle relative to the origin.
- operator *(double, Rectangle)
Uniformly scales a rectangle relative to the origin.
- operator -(Rectangle, Point)
Translates a rectangle by the negation of a point offset.