RectangleF

References
[TypeConverter(RectangleFConverterInternal)] public struct RectangleF : IEquatable<RectangleF>
Represents a floating point rectangle with a location (X, Y) and size (Width, Height) components.
Remarks: A rectangle is defined by a location (X, Y) and a size (Width, Height). The width and/or height can be negative.

Constructors

RectangleF

#
public RectangleF(float x, float y, float width, float height)
Initializes a new instance of the Rectangle class with the specified x, y, width, and height
x (float)X co-ordinate for the location of the rectangle
y (float)Y co-ordinate for the location of the rectangle
width (float)Width of the rectangle
height (float)Height of the rectangle

RectangleF

#
public RectangleF(PointF location, SizeF size)
Initializes a new instance of the Rectangle class with the specified location and size

RectangleF

#
public RectangleF(PointF start, PointF end)
Initializes a new instance of the Rectangle class with two points
start (PointF)Starting point of the rectangle
end (PointF)Ending point of the rectangle

RectangleF

#
public RectangleF(Rectangle rectangle)
Initilizes a new instance of the RectangleF class with the specified rectangle.

RectangleF

#
public RectangleF(SizeF size)
Initializes a new instance of the Rectangle class with X, Y co-ordinates at 0,0 and the specified size

Fields

Empty

#
public static readonly RectangleF Empty
Gets an empty rectangle with zero X, Y, Width, and Height components

Properties

Bottom

#
public float Bottom { get; set; }
Gets or sets the logical bottom of this rectangle (Y + Height if Height is positive, Y + 1 if negative)
Remarks: This differs from the InnerBottom in that this will return the co-ordinate adjacent to the bottom edge of the rectangle, whereas InnerBottom returns the co-ordinate that is inside the rectangle

BottomLeft

#
public PointF BottomLeft { get; set; }
Gets or sets the point at the Bottom and Left of the rectangle

BottomRight

#
public PointF BottomRight { get; set; }
Gets or sets the point at the Bottom and Right of the rectangle

Center

#
public PointF Center { get; set; }
Gets or sets the rectangle's center position

EndLocation

#
public PointF EndLocation { get; set; }
Gets the ending location of this rectangle
Remarks: This gets/sets the product of the Location + Size. If the Width or Height of this rectangle is positive, then the X/Y of the returned location will be minus 1 so as to be inside of the rectangle's bounds.

InnerBottom

#
public float InnerBottom { get; set; }
Gets or sets the bottom of the rectangle that is inside the bounds
Remarks: Similar to Bottom but inside the rectangle's bounds instead of just below the bottom

InnerBottomLeft

#
public PointF InnerBottomLeft { get; set; }
Gets or sets the point at the InnerBottom and Left of the rectangle
Remarks: Similar to BottomLeft but inside the rectangle's bounds instead of just below the bottom

InnerBottomRight

#
public PointF InnerBottomRight { get; set; }
Gets or sets the point at the InnerBottom and InnerRight of the rectangle
Remarks: Similar to BottomRight but inside the rectangle's bounds instead of just to the right and bottom

InnerRight

#
public float InnerRight { get; set; }
Gets or sets the right of the rectangle that is inside the bounds
Remarks: Similar to Right but inside the rectangle's bounds instead of just to the right

InnerTopRight

#
public PointF InnerTopRight { get; set; }
Gets or sets the point at the Top and InnerRight of the rectangle
Remarks: Similar to TopRight but inside the rectangle's bounds instead of just to the right

IsEmpty

#
public bool IsEmpty { get; }
Gets a value indicating that the Size of this rectangle is empty (either the width or height are zero)

IsZero

#
public bool IsZero { get; }
Gets a value indicating that both the and Size of this rectangle are zero
Remarks: The X, Y, Width, and Height components of this rectangle must be zero for this to return true.

Left

#
public float Left { get; set; }
Gets or sets the logical left of this rectangle (X co-ordinate if Width is positive, X + Width if negative)

Location

#
public PointF Location { get; set; }
Gets the location of this rectangle

MiddleBottom

#
public PointF MiddleBottom { get; set; }
Gets or sets the point at the MiddleX and Bottom of the rectangle

MiddleLeft

#
public PointF MiddleLeft { get; set; }
Gets or sets the point at the Left and MiddleY of the rectangle

MiddleRight

#
public PointF MiddleRight { get; set; }
Gets or sets the point at the Right and MiddleY of the rectangle

MiddleTop

#
public PointF MiddleTop { get; set; }
Gets or sets the point at the MiddleX and Top of the rectangle

MiddleX

#
public float MiddleX { get; set; }
Gets or sets the rectangle's middle horizontal position

MiddleY

#
public float MiddleY { get; set; }
Gets or sets the rectangle's middle vertical position

Top

#
public float Top { get; set; }
Gets or sets the logical top of this rectangle (Y co-ordinate if Height is positive, Y + Height if negative)
Remarks: This is always the logical top, where if the Height is positive it will adjust the Y co-ordinate. If the Height of the rectangle is negative, then this will adjust the Height when setting the value.

TopLeft

#
public PointF TopLeft { get; set; }
Gets or sets the point at the Top and Left of the rectangle

TopRight

#
public PointF TopRight { get; set; }
Gets or sets the point at the Top and Right of the rectangle

Methods

Align

#
public static RectangleF Align(RectangleF rectangle, float gridWidth, float gridHeight)
Aligns the rectangle to a grid of the specified gridWidth and gridHeight
Remarks: This will align the top, left, right, and bottom to a grid by inflating each edge to the next grid line.
rectangle (RectangleF)Rectangle to align
gridWidth (float)Grid width
gridHeight (float)Grid height
A new Rectangle aligned to the grid

Align

#
public static RectangleF Align(RectangleF rectangle, SizeF gridSize)
Aligns the rectangle to a grid of the specified gridSize
Remarks: This will align the top, left, right, and bottom to a grid by inflating each edge to the next grid line.
rectangle (RectangleF)Rectangle to align
gridSize (SizeF)Size of the grid to align the rectangle to
A new Rectangle aligned to the grid

Align

#
public void Align(float gridWidth, float gridHeight)
Aligns the rectangle to a grid of the specified gridWidth and gridHeight
Remarks: This will align the top, left, right, and bottom to a grid by inflating each edge to the next grid line.
gridWidth (float)Grid width
gridHeight (float)Grid height

Align

#
public void Align(SizeF gridSize)
Aligns the rectangle to a grid of the specified gridSize
Remarks: This will align the top, left, right, and bottom to a grid by inflating each edge to the next grid line.
gridSize (SizeF)Size of the grid to align the rectangle to

Contains

#
public bool Contains(float x, float y)
Gets a value indicating that the specified x and y co-ordinates are within the bounds of this rectangle
x (float)X co-ordinate to test
y (float)Y co-ordinate to test
True if the rectangle contains the x and y co-ordinates, false otherwise

Contains

#
public bool Contains(PointF point)
Gets a value indicating that the specified point is within the bounds of this rectangle
point (PointF)Point to test
True if the point is within the bounds of this rectangle, false if it is outside the bounds

Contains

#
public bool Contains(RectangleF rectangle)
Gets a value indicating that the specified rectangle is entirely contained within the bounds of this rectangle
rectangle (RectangleF)Rectangle to test if it is contained within this instance

Distance

#
public static SizeF Distance(RectangleF rect, PointF point)
Calculates the distance between the specified point and rect.
Remarks: This calculates the horizontal and vertical distance of any of the edges of the rectangle to the specified point.
rect (RectangleF)Rectangle for the source.
point (PointF)Point to determine the distance from the rectangle.

Distance

#
public static SizeF Distance(RectangleF rect1, RectangleF rect2)
Calculates the distance between two rectangles.
Remarks: This calculates the horizontal and vertical distance of any of the edges of each rectangle. If the rectangles intersect, the distance will be empty.
rect1 (RectangleF)First rectangle to compare
rect2 (RectangleF)Second rectangle to compare

Equals

#
public override bool Equals(object obj)
Compares this rectangle to an object for equality
obj (object)Object to compare with
True if the obj is a Rectangle and is equal to this instance, false otherwise
ValueType.Equals

Equals

#
public bool Equals(RectangleF other)
Compares this rectangle with the specified other rectangle
other (RectangleF)Other rectangle to compare with
True if the other rectangle is equal to this instance, false otherwise

FromCenter

#
public static RectangleF FromCenter(PointF center, SizeF size)
Creates a new instance of a RectangleF with a specified center and size
center (PointF)Center of the rectangle
size (SizeF)Size of the rectangle
A new instance of a RectangleF with the specified center and size

FromSides

#
public static RectangleF FromSides(float left, float top, float right, float bottom)
Creates a new instance of a RectangleF from the values of the left, top, right and bottom sides
left (float)Left side of the rectangle to create
top (float)Top of the rectangle to create
right (float)Right side of the rectangle to create
bottom (float)Bottom of the rectangle to create
A new instance of a RectangleF with values for the Left, Top, Right, and Bottom sides

GetHashCode

#
public override int GetHashCode()
Gets the hash code for this rectangle

Inflate

#
public static RectangleF Inflate(RectangleF rectangle, float width, float height)
Inflates all dimensions of this rectangle by the specified width and height
Remarks: This inflates the rectangle in all dimensions by the specified width and height. The resulting rectangle will be increased in width and height twice that of the specified size, and the center will be in the same location. A negative width and/or height can be passed in to deflate the rectangle.
rectangle (RectangleF)Rectangle to inflate
width (float)Width to inflate the left and right of the rectangle by
height (float)Height to inflate the top and bottom of the rectangle by
A new rectangle inflated by the specified width and height

Inflate

#
public static RectangleF Inflate(RectangleF rectangle, SizeF size)
Inflates all dimensions of the rectangle by the specified size
Remarks: This inflates the rectangle in all dimensions by the width and height specified by size. The resulting rectangle will be increased in width and height twice that of the specified size, and the center will be in the same location. A negative width and/or height can be passed in to deflate the rectangle.
rectangle (RectangleF)Rectangle to inflate
size (SizeF)Size to inflate the rectangle by
A new rectangle inflated by the specified width and height

Inflate

#
public void Inflate(float width, float height)
Inflates all dimensions of this rectangle by the specified width and height
Remarks: This inflates the rectangle in all dimensions by the specified width and height. The resulting rectangle will be increased in width and height twice that of the specified size, and the center will be in the same location. A negative width and/or height can be passed in to deflate the rectangle.
width (float)Width to inflate the left and right of the rectangle by
height (float)Height to inflate the top and bottom of the rectangle by

Inflate

#
public void Inflate(SizeF size)
Inflates all dimensions of this rectangle by the specified size
Remarks: This inflates the rectangle in all dimensions by the width and height specified by size. The resulting rectangle will be increased in width and height twice that of the specified size, and the center will be in the same location. A negative width and/or height can be passed in to deflate the rectangle.
size (SizeF)Size to inflate the rectangle by

Inset

#
public static RectangleF Inset(RectangleF rectangle, PaddingF padding)
Returns a new rectangle that is inset by the specified padding
rectangle (RectangleF)Rectangle to inset
padding (PaddingF)Padding to inset by
A new rectangle inset by the specified padding

Inset

#
public void Inset(PaddingF padding)
Insets the rectangle by the specified padding.

Intersect

#
public static RectangleF Intersect(RectangleF rect1, RectangleF rect2)
Intersect the two specified rectangles
rect1 (RectangleF)First rectangle to intersect
rect2 (RectangleF)Second rectangle to intersect
A new RectangleF with the intersection of the two rectangles

Intersect

#
public void Intersect(RectangleF rectangle)
Intersect the rectangle with the specified rectangle

Intersects

#
public bool Intersects(RectangleF rectangle)
Gets a value indicating that the specified rectangle overlaps this rectangle
rectangle (RectangleF)Rectangle to test for intersection/overlap
True if the rectangle overlaps this instance, false otherwise

Normalize

#
public void Normalize()
Normalizes the rectangle so both the Width and Height are positive, without changing the location of the rectangle
Remarks: Rectangles can have negative widths/heights, which means that the starting location will not always be at the top left corner. Normalizing the rectangle will ensure that the X and Y co-ordinates of the rectangle are at the top left.

Offset

#
public static RectangleF Offset(RectangleF rectangle, float x, float y)
Offsets the location of the rectangle by the specified x and y values
rectangle (RectangleF)Rectangle to offset
x (float)Horizontal offset to move the rectangle
y (float)Vertical offset to move the rectangle
A new Rectangle with the offset location

Offset

#
public static RectangleF Offset(RectangleF rectangle, PointF point)
Offsets the location of the rectangle by the X and Y values of the specified point
rectangle (RectangleF)Rectangle to offset
point (PointF)Point with values to offset the rectangle
A new Rectangle with the offset location

Offset

#
public static RectangleF Offset(RectangleF rectangle, SizeF size)
Offsets the location of the rectangle by the specified size
rectangle (RectangleF)Rectangle to inflate
size (SizeF)Width and Height to move the rectangle
A new Rectangle with the offset location

Offset

#
public void Offset(float x, float y)
Offsets the location of the rectangle by the specified x and y values
x (float)Horizontal offset to move the rectangle
y (float)Vertical offset to move the rectangle

Offset

#
public void Offset(PointF point)
Offsets the location of the rectangle by the X and Y values of the specified point

Offset

#
public void Offset(SizeF size)
Offsets the location of the rectangle by the specified size

Restrict

#
public static Rectangle Restrict(Rectangle rectangle, Rectangle restrict)
Restricts the rectangle to be within the restrict rectangle
Remarks: This ensures that rectangle's bounds fall within the bounds of the specified restrict rectangle It is useful to ensure that the rectangle does not exceed certain limits (e.g. for drawing)
rectangle (Rectangle)Rectangle to restrict
restrict (Rectangle)Rectangle to restrict to
A new rectangle restricted to the restrict bounds

Restrict

#
public void Restrict(PointF point, SizeF size)
Restricts the rectangle to be within the specified point and size
Remarks: This is a shortcut for Restrict(RectangleF)
point (PointF)Minimum location for the rectangle
size (SizeF)Maximum size for the rectangle

Restrict

#
public void Restrict(RectangleF rectangle)
Restricts the rectangle to be within the specified rectangle
Remarks: This ensures that the current rectangle's bounds fall within the bounds of the specified rectangle. It is useful to ensure that the rectangle does not exceed certain limits (e.g. for drawing)
rectangle (RectangleF)Rectangle to restrict this instance to

Restrict

#
public void Restrict(SizeF size)
Restricts the rectangle to be within the specified size at an X,Y location of 0, 0

ToString

#
public override string ToString()
Converts this rectangle to a string

Union

#
public static RectangleF Union(RectangleF rect1, RectangleF rect2)
Combines two rectangles into one rectangle that encompasses both
rect1 (RectangleF)First rectangle to union
rect2 (RectangleF)Second rectangle to union
A new RectangleF that encompasses both rect1 and rect2

Union

#
public void Union(RectangleF rectangle)
Union the rectangle into this instance to encompass both rectangles

Operators

OperationResultDescription
RectangleF+PaddingFRectangleFReturns a rectangle inset by the specified padding
RectangleF+PointFRectangleFAdds the offset to the specified rectangle, moving its location
RectangleF+SizeFRectangleFAdds the offset to the specified rectangle, moving its location
RectangleF/floatRectangleFDivides all X, Y, Width, Height components of the rectangle by a factor factor
RectangleF/SizeFRectangleFDivides the specified rectangle by the Width and Height of size
RectangleF==RectangleFboolCompares two rectangles for equality
RectangleF!=RectangleFboolCompares two rectangles for inequality
RectangleF*floatRectangleFMultiplies all X, Y, Width, Height components of the rectangle by a factor
RectangleF*SizeFRectangleFMultiplies the specified rectangle by the Width and Height of size
RectangleF-PaddingFRectangleFReturns a rectangle expanded by the specified padding
RectangleF-PointFRectangleFSubtracts the offset from the specified rectangle, moving its location
RectangleF-SizeFRectangleFSubtracts the offset from the specified rectangle, moving its location

Convertors

OperationResultDescription
implicitRectangleRectangleFImplicit conversion from a Rectangle to a RectangleF