RectangleD Struct

Represents an axis-aligned rectangle with sub-pixel precision (see PointD for the coordinate system).

public readonly struct RectangleD : IEquatable<RectangleD>
Implements

Constructors

NameDescription
RectangleD(double, double, double, double)Initializes a new instance of the RectangleD struct.

RectangleD(double, double, double, double)

RectangleD.cs

Initializes a new instance of the RectangleD struct.

public RectangleD(double x, double y, double width, double height)

Parameters

x double
The left coordinate.
y double
The top coordinate.
width double
The width. Must be zero or positive, and finite.
height double
The height. Must be zero or positive, and finite.

Exceptions

ArgumentOutOfRangeException
width or height is negative or not a finite number.

Properties

NameDescription
BottomGets the bottom coordinate (Y + Height).
CenterGets the point in the middle of the rectangle.
EmptyGets an empty rectangle located at the origin.
HeightGets the height.
IsEmptyGets a value indicating whether the rectangle has no area.
LeftGets the left coordinate.
LocationGets the top-left corner.
RightGets the right coordinate (X + Width).
TopGets the top coordinate.
WidthGets the width.
XGets the left coordinate.
YGets the top coordinate.

Gets the bottom coordinate (Y + Height).

public double Bottom { get; }

Property value

double

Gets the point in the middle of the rectangle.

public PointD Center { get; }

Property value

PointD

Gets an empty rectangle located at the origin.

public static RectangleD Empty { get; }

Property value

RectangleD

Gets the height.

public double Height { get; }

Property value

double

Gets a value indicating whether the rectangle has no area.

public bool IsEmpty { get; }

Property value

bool

Gets the left coordinate.

public double Left { get; }

Property value

double

Gets the top-left corner.

public PointD Location { get; }

Property value

PointD

Gets the top coordinate.

public double Top { get; }

Property value

double

Gets the width.

public double Width { get; }

Property value

double

Gets the left coordinate.

public double X { get; }

Property value

double

Gets the top coordinate.

public double Y { get; }

Property value

double

Methods

NameDescription
Contains(PointD)Gets a value indicating whether a point is inside the rectangle. The left and top sides are inside, the right and bottom sides are not.
Contains(RectangleD)Gets a value indicating whether another rectangle lies entirely within this one.
Equals(RectangleD)Indicates whether the current object is equal to another object of the same type.
Equals(object?)Indicates whether this instance and a specified object are equal.
FromLTRB(double, double, double, double)Creates a rectangle from its edges.
FromRectangle(Rectangle)Converts a rectangle of pixels to the area those pixels cover.
GetHashCode()Returns the hash code for this instance.
Inflate(double)Moves each side of the rectangle away from its center by the same distance, or toward it when the distance is negative.
Inflate(double, double)Moves each side of the rectangle away from its center, or toward it when the distance is negative. A side does not move past the center: a rectangle reduced by more than half its size has no width or no height.
Intersect(RectangleD, RectangleD)Gets the part two rectangles have in common.
IntersectsWith(RectangleD)Gets a value indicating whether two rectangles have some area in common.
Offset(double, double)Moves the rectangle.
ToString()Returns the rectangle formatted as {X},{Y} {Width}x{Height}.
Union(RectangleD, RectangleD)Gets the smallest rectangle containing two rectangles. A rectangle without area counts for its position: the bounds of a horizontal line have no height and still extend the result.

Contains overloads

Contains(PointD)

RectangleD.cs

Gets a value indicating whether a point is inside the rectangle. The left and top sides are inside, the right and bottom sides are not.

public bool Contains(PointD point)
Parameters
point PointD
The point.
Returns

bool true if the point is inside.

Contains(RectangleD)

RectangleD.cs

Gets a value indicating whether another rectangle lies entirely within this one.

public bool Contains(RectangleD rectangle)
Parameters
rectangle RectangleD
The other rectangle.
Returns

bool true if no part of rectangle is outside this rectangle.

Equals overloads

Equals(RectangleD)

RectangleD.cs

Indicates whether the current object is equal to another object of the same type.

public bool Equals(RectangleD other)
Parameters
other RectangleD
An object to compare with this object.
Returns

bool true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

RectangleD.cs

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)
Parameters
obj object?
The object to compare with the current instance.
Returns

bool true if obj and this instance are the same type and represent the same value; otherwise, false.

FromLTRB(double, double, double, double)

RectangleD.cs

Creates a rectangle from its edges.

public static RectangleD FromLTRB(double left, double top, double right, double bottom)

Parameters

left double
The left coordinate.
top double
The top coordinate.
right double
The right coordinate. Must not be less than left.
bottom double
The bottom coordinate. Must not be less than top.

Returns

RectangleD The rectangle.

Exceptions

ArgumentOutOfRangeException
The bounds are inverted, or the size is not a finite number.

FromRectangle(Rectangle)

RectangleD.cs

Converts a rectangle of pixels to the area those pixels cover.

public static RectangleD FromRectangle(Rectangle rectangle)

Parameters

rectangle Rectangle
The rectangle of pixels.

Returns

RectangleD The area.

GetHashCode()

RectangleD.cs

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int A 32-bit signed integer that is the hash code for this instance.

Inflate overloads

Inflate(double)

RectangleD.cs

Moves each side of the rectangle away from its center by the same distance, or toward it when the distance is negative.

public RectangleD Inflate(double distance)
Parameters
distance double
The distance every side moves.
Returns

RectangleD The resized rectangle, around the same center.

Exceptions
ArgumentOutOfRangeException
The size of the result is not a finite number.

Inflate(double, double)

RectangleD.cs

Moves each side of the rectangle away from its center, or toward it when the distance is negative. A side does not move past the center: a rectangle reduced by more than half its size has no width or no height.

public RectangleD Inflate(double dx, double dy)
Parameters
dx double
The distance the left and the right sides move.
dy double
The distance the top and the bottom sides move.
Returns

RectangleD The resized rectangle, around the same center.

Exceptions
ArgumentOutOfRangeException
The size of the result is not a finite number.

Intersect(RectangleD, RectangleD)

RectangleD.cs

Gets the part two rectangles have in common.

public static RectangleD Intersect(RectangleD a, RectangleD b)

Parameters

a RectangleD
The first rectangle.
b RectangleD
The second rectangle.

Returns

RectangleD The common part, or RectangleD.Empty when the rectangles have no area in common.

IntersectsWith(RectangleD)

RectangleD.cs

Gets a value indicating whether two rectangles have some area in common.

public bool IntersectsWith(RectangleD rectangle)

Parameters

rectangle RectangleD
The other rectangle.

Returns

bool true if the rectangles overlap; rectangles that only touch do not.

Offset(double, double)

RectangleD.cs

Moves the rectangle.

public RectangleD Offset(double dx, double dy)

Parameters

dx double
The horizontal distance.
dy double
The vertical distance.

Returns

RectangleD The moved rectangle.

ToString()

RectangleD.cs

Returns the rectangle formatted as {X},{Y} {Width}x{Height}.

public override string ToString()

Returns

string A string representation of the rectangle.

Union(RectangleD, RectangleD)

RectangleD.cs

Gets the smallest rectangle containing two rectangles. A rectangle without area counts for its position: the bounds of a horizontal line have no height and still extend the result.

public static RectangleD Union(RectangleD a, RectangleD b)

Parameters

a RectangleD
The first rectangle.
b RectangleD
The second rectangle.

Returns

RectangleD The rectangle containing both.

Operators

NameDescription
operator ==(RectangleD, RectangleD)Compares two rectangles for equality.
implicit operator RectangleD(Rectangle)Converts a rectangle of pixels to the area those pixels cover. The conversion is lossless.
operator !=(RectangleD, RectangleD)Compares two rectangles for inequality.

operator ==(RectangleD, RectangleD)

RectangleD.cs

Compares two rectangles for equality.

public static bool operator ==(RectangleD left, RectangleD right)

Parameters

left RectangleD
The first rectangle.
right RectangleD
The second rectangle.

Returns

bool true if both rectangles are equal; otherwise false.

implicit operator RectangleD(Rectangle)

RectangleD.cs

Converts a rectangle of pixels to the area those pixels cover. The conversion is lossless.

public static implicit operator RectangleD(Rectangle rectangle)

Parameters

rectangle Rectangle
The rectangle of pixels.

Returns

RectangleD

operator !=(RectangleD, RectangleD)

RectangleD.cs

Compares two rectangles for inequality.

public static bool operator !=(RectangleD left, RectangleD right)

Parameters

left RectangleD
The first rectangle.
right RectangleD
The second rectangle.

Returns

bool true if the rectangles differ; otherwise false.