Rectangle
ReferencesRepresents a 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
Rectangle
#Initializes a new instance of the Rectangle class with the specified
x, y, width, and heightRectangle
#Initializes a new instance of a Rectangle class with truncated values of the specified floating-point
rectangleParameters:
| rectangle (RectangleF) | RectangleF to initialize the X and Y values of the new instance with |
Fields
Empty
#Gets an empty rectangle with zero X, Y, Width, and Height components
Remarks: Useful when you want a rectangle no size or location.
Properties
Bottom
#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
#BottomRight
#EndLocation
#Gets the ending location of this rectangle
InnerBottom
#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
#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
#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
#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
#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
#Gets a value indicating that the Size of this rectangle is empty (either the width or height are zero)
IsZero
#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
#Gets or sets the logical left of this rectangle (X co-ordinate if Width is positive, X + Width if negative)
MiddleBottom
#MiddleLeft
#MiddleRight
#MiddleTop
#Right
#Gets or sets the logical right of this rectangle (X + Width if Width is positive, X + 1 if negative)
Remarks: This differs from the InnerRight in that this will return the co-ordinate adjacent to the right edge of the rectangle, whereas InnerRight returns the co-ordinate that is inside the rectangle
Top
#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
#TopRight
#Methods
Align
#Aligns the
rectangle to a grid of the specified gridWidth and gridHeightAlign
#Aligns the
rectangle to a grid of the specified gridSizeAlign
#Aligns the rectangle to a grid of the specified
gridWidth and gridHeightRemarks: This will align the top, left, right, and bottom to a grid by inflating each edge to the next grid line.
Align
#Aligns the rectangle to a grid of the specified
gridSizeRemarks: This will align the top, left, right, and bottom to a grid by inflating each edge to the next grid line.
Parameters:
| gridSize (Size) | Size of the grid to align the rectangle to |
Ceiling
#Converts a floating point
rectangle to an integral Rectangle by getting the smallest integral value of X, Y, Width, and HeightRemarks: This is used to get an integral rectangle that bounds the floating point rectangle completely. E.g. if a RectangleF has (X,Y,W,H) values of 0.2, 0.9, 1.1, 1.9, it would return a new integral rectangle with values 0, 0, 2, 2.
Parameters:
| rectangle (RectangleF) | Rectangle to get the ceiling |
Returns: A new instance of a Rectangle with truncated X, Y and a ceiling Width and Height values
Contains
#Gets a value indicating that the specified
x and y co-ordinates are within the bounds of this rectangleReturns: True if the rectangle contains the x and y co-ordinates, false otherwise
Contains
#Gets a value indicating that the specified
point is within the bounds of this rectangleParameters:
| point (Point) | Point to test |
Returns: True if the point is within the bounds of this rectangle, false if it is outside the bounds
Contains
#Gets a value indicating that the specified
rectangle is entirely contained within the bounds of this rectangleParameters:
| rectangle (Rectangle) | Rectangle to test if it is contained within this instance |
Equals
#Compares this rectangle to an object for equality
Parameters:
| obj (object) | Object to compare with |
Returns: True if the
obj is a Rectangle and is equal to this instance, false otherwiseOverrides: ValueType.Equals
Equals
#Compares this rectangle with the specified
other rectangleParameters:
| other (Rectangle) | Other rectangle to compare with |
Returns: True if the
other rectangle is equal to this instance, false otherwiseFromCenter
#Creates a new instance of a Rectangle with a specified
center and sizeReturns: A new instance of a Rectangle with the specified center and size
FromSides
#Creates a new instance of a RectangleF from the values of the
left, top, right and bottom sidesGetHashCode
#Gets the hash code for this rectangle
Returns: Hash code value for this rectangle
Overrides: ValueType.GetHashCode
Inflate
#Inflates all dimensions of this
rectangle by the specified width and heightRemarks: 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.Parameters:
| rectangle (Rectangle) | Rectangle to inflate |
| width (int) | Width to inflate the left and right of the rectangle by |
| height (int) | Height to inflate the top and bottom of the rectangle by |
Returns: A new rectangle inflated by the specified width and height
Inflate
#Inflates all dimensions of the
rectangle by the specified sizeRemarks: 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.Returns: A new rectangle inflated by the specified width and height
Inflate
#Inflates all dimensions of this rectangle by the specified
width and heightRemarks: 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.Inflate
#Inflates all dimensions of this rectangle by the specified
sizeRemarks: 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.Parameters:
| size (Size) | Size to inflate the rectangle by |
Inset
#Returns a new rectangle that is inset by the specified
paddingReturns: A new rectangle inset by the specified padding
Inset
#Insets the rectangle by the specified padding.
Parameters:
| padding (Padding) | Padding to inset this rectangle by |
Intersect
#Intersect the two specified rectangles
Intersect
#Intersect the rectangle with the specified
rectangleParameters:
| rectangle (Rectangle) | Rectangle to intersect with |
Intersects
#Gets a value indicating that the specified
rectangle overlaps this rectangleParameters:
| rectangle (Rectangle) | Rectangle to test for intersection/overlap |
Returns: 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
Offset
#Offsets the location of the
rectangle by the specified x and y valuesOffset
#Offsets the location of the
rectangle by the X and Y values of the specified pointOffset
#Offsets the location of the
rectangle by the specified sizeOffset
#Offsets the location of the rectangle by the specified
x and y valuesOffset
#Offsets the location of the rectangle by the X and Y values of the specified
pointParameters:
| point (Point) | Point with values to offset the rectangle |
Offset
#Offsets the location of the rectangle by the specified
sizeParameters:
| size (Size) | Width and Height to move the rectangle |
Restrict
#Restricts the
rectangle to be within the restrict rectangleRemarks: 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)Returns: A new rectangle restricted to the restrict bounds
Restrict
#Restricts the
rectangle to be within the specified sizeRemarks: This ensures that
rectangle's bounds fall within the bounds of the specified size It is useful to ensure that the rectangle does not exceed certain limits (e.g. for drawing)Returns: A new rectangle restricted to the restrict bounds
Restrict
#Restricts the rectangle to be within the specified
location and sizeRemarks: This is a shortcut for Restrict(Rectangle)
Restrict
#Restricts the rectangle to be within the specified
rectangleRemarks: 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)Parameters:
| rectangle (Rectangle) | Rectangle to restrict this instance to |
Restrict
#Restricts the rectangle to be within the specified
size at an X,Y location of 0, 0Remarks: This is a shortcut for Restrict(Rectangle)
Parameters:
| size (Size) | Maxiumum size for the rectangle |
Round
#Converts a floating point
rectangle to an integral Rectangle by rounding the X, Y, Width, and Height.Parameters:
| rectangle (RectangleF) | Rectangle to round |
Returns: A new instance of a Rectangle with rounded X, Y, Width, and Height values
ToString
#Converts this rectangle to a string
Returns: String representation of this rectangle
Overrides: ValueType.ToString
Truncate
#Converts a floating point
rectangle to an integral Rectangle by truncating the X, Y, Width, and Height valuesParameters:
| rectangle (RectangleF) | Rectangle to truncate |
Returns: A new instance of a Rectangle with truncated X, Y, Width, and Height values
Union
#Combines two rectangles into one rectangle that encompasses both
Returns: A new Rectangle that encompasses both
rect1 and rect2Operators
| Operation | Result | Description | ||
|---|---|---|---|---|
| Rectangle | + | Padding | Rectangle | Returns a rectangle inset by the specified padding |
| Rectangle | + | Point | Rectangle | Adds the offset to the specified rectangle, moving its location |
| Rectangle | + | Size | Rectangle | Adds the offset to the specified rectangle, moving its location |
| Rectangle | / | int | Rectangle | Divides all X, Y, Width, Height components of the rectangle by a factor factor |
| Rectangle | / | Size | Rectangle | Divides the specified rectangle by the Width and Height of size |
| Rectangle | == | Rectangle | bool | Compares two rectangles for equality |
| Rectangle | != | Rectangle | bool | Compares two rectangles for inequality |
| Rectangle | * | int | Rectangle | Multiplies all X, Y, Width, Height components of the rectangle by a factor |
| Rectangle | * | Size | Rectangle | Multiplies the specified rectangle by the Width and Height of size |
| Rectangle | - | Padding | Rectangle | Returns a rectangle expanded by the specified padding |
| Rectangle | - | Point | Rectangle | Subtracts the offset from the specified rectangle, moving its location |
| Rectangle | - | Size | Rectangle | Subtracts the offset from the specified rectangle, moving its location |
Convertors
| Operation | Result | Description | ||
|---|---|---|---|---|
| explicit | RectangleF | Rectangle | Implicit conversion from a RectangleF to a Rectangle | |