Back to monogame.net
Show / Hide Table of Contents

Struct Rectangle

Describes a 2D-rectangle.

Implements
IEquatable<Rectangle>
Namespace: Microsoft.Xna.Framework
Assembly: MonoGame.Framework.dll
Syntax
public struct Rectangle

Constructors

Rectangle(Point, Point)

Creates a new instance of Rectangle struct, with the specified location and size.

Declaration
public Rectangle(Point location, Point size)
Parameters
Type Name Description
Point location

The x and y coordinates of the top-left corner of the created Rectangle.

Point size

The width and height of the created Rectangle.

Rectangle(Int32, Int32, Int32, Int32)

Creates a new instance of Rectangle struct, with the specified position, width, and height.

Declaration
public Rectangle(int x, int y, int width, int height)
Parameters
Type Name Description
Int32 x

The x coordinate of the top-left corner of the created Rectangle.

Int32 y

The y coordinate of the top-left corner of the created Rectangle.

Int32 width

The width of the created Rectangle.

Int32 height

The height of the created Rectangle.

Fields

Height

The height of this Rectangle.

Declaration
public int Height
Field Value
Type Description
Int32

Width

The width of this Rectangle.

Declaration
public int Width
Field Value
Type Description
Int32

X

The x coordinate of the top-left corner of this Rectangle.

Declaration
public int X
Field Value
Type Description
Int32

Y

The y coordinate of the top-left corner of this Rectangle.

Declaration
public int Y
Field Value
Type Description
Int32

Properties

Bottom

Returns the y coordinate of the bottom edge of this Rectangle.

Declaration
public readonly int Bottom { get; }
Property Value
Type Description
Int32

Center

A Point located in the center of this Rectangle.

Declaration
public readonly Point Center { get; }
Property Value
Type Description
Point
Remarks

If Width or Height is an odd number, the center point will be rounded down.

Empty

Returns a Rectangle with X=0, Y=0, Width=0, Height=0.

Declaration
public static readonly Rectangle Empty { get; }
Property Value
Type Description
Rectangle

IsEmpty

Whether or not this Rectangle has a Width and Height of 0, and a Location of (0, 0).

Declaration
public readonly bool IsEmpty { get; }
Property Value
Type Description
Boolean

Left

Returns the x coordinate of the left edge of this Rectangle.

Declaration
public readonly int Left { get; }
Property Value
Type Description
Int32

Location

The top-left coordinates of this Rectangle.

Declaration
public Point Location { get; set; }
Property Value
Type Description
Point

Right

Returns the x coordinate of the right edge of this Rectangle.

Declaration
public readonly int Right { get; }
Property Value
Type Description
Int32

Size

The width-height coordinates of this Rectangle.

Declaration
public Point Size { get; set; }
Property Value
Type Description
Point

Top

Returns the y coordinate of the top edge of this Rectangle.

Declaration
public readonly int Top { get; }
Property Value
Type Description
Int32

Methods

Contains(Point)

Gets whether or not the provided Point lies within the bounds of this Rectangle.

Declaration
public bool Contains(Point value)
Parameters
Type Name Description
Point value

The coordinates to check for inclusion in this Rectangle.

Returns
Type Description
Boolean

true if the provided Point lies inside this Rectangle; false otherwise.

Contains(ref Point, out Boolean)

Gets whether or not the provided Point lies within the bounds of this Rectangle.

Declaration
public void Contains(ref Point value, out bool result)
Parameters
Type Name Description
Point value

The coordinates to check for inclusion in this Rectangle.

Boolean result

true if the provided Point lies inside this Rectangle; false otherwise. As an output parameter.

Contains(Rectangle)

Gets whether or not the provided Rectangle lies within the bounds of this Rectangle.

Declaration
public bool Contains(Rectangle value)
Parameters
Type Name Description
Rectangle value

The Rectangle to check for inclusion in this Rectangle.

Returns
Type Description
Boolean

true if the provided Rectangle's bounds lie entirely inside this Rectangle; false otherwise.

Contains(ref Rectangle, out Boolean)

Gets whether or not the provided Rectangle lies within the bounds of this Rectangle.

Declaration
public void Contains(ref Rectangle value, out bool result)
Parameters
Type Name Description
Rectangle value

The Rectangle to check for inclusion in this Rectangle.

Boolean result

true if the provided Rectangle's bounds lie entirely inside this Rectangle; false otherwise. As an output parameter.

Contains(Vector2)

Gets whether or not the provided Vector2 lies within the bounds of this Rectangle.

Declaration
public bool Contains(Vector2 value)
Parameters
Type Name Description
Vector2 value

The coordinates to check for inclusion in this Rectangle.

Returns
Type Description
Boolean

true if the provided Vector2 lies inside this Rectangle; false otherwise.

Contains(ref Vector2, out Boolean)

Gets whether or not the provided Vector2 lies within the bounds of this Rectangle.

Declaration
public void Contains(ref Vector2 value, out bool result)
Parameters
Type Name Description
Vector2 value

The coordinates to check for inclusion in this Rectangle.

Boolean result

true if the provided Vector2 lies inside this Rectangle; false otherwise. As an output parameter.

Contains(Int32, Int32)

Gets whether or not the provided coordinates lie within the bounds of this Rectangle.

Declaration
public bool Contains(int x, int y)
Parameters
Type Name Description
Int32 x

The x coordinate of the point to check for containment.

Int32 y

The y coordinate of the point to check for containment.

Returns
Type Description
Boolean

true if the provided coordinates lie inside this Rectangle; false otherwise.

Contains(Single, Single)

Gets whether or not the provided coordinates lie within the bounds of this Rectangle.

Declaration
public bool Contains(float x, float y)
Parameters
Type Name Description
Single x

The x coordinate of the point to check for containment.

Single y

The y coordinate of the point to check for containment.

Returns
Type Description
Boolean

true if the provided coordinates lie inside this Rectangle; false otherwise.

Deconstruct(out Int32, out Int32, out Int32, out Int32)

Deconstruction method for Rectangle.

Declaration
public void Deconstruct(out int x, out int y, out int width, out int height)
Parameters
Type Name Description
Int32 x
Int32 y
Int32 width
Int32 height

Equals(Rectangle)

Compares whether current instance is equal to specified Rectangle.

Declaration
public bool Equals(Rectangle other)
Parameters
Type Name Description
Rectangle other

The Rectangle to compare.

Returns
Type Description
Boolean

true if the instances are equal; false otherwise.

Equals(Object)

Compares whether current instance is equal to specified .

Declaration
public override bool Equals(object obj)
Parameters
Type Name Description
Object obj

The to compare.

Returns
Type Description
Boolean

true if the instances are equal; false otherwise.

GetHashCode()

Gets the hash code of this Rectangle.

Declaration
public override int GetHashCode()
Returns
Type Description
Int32

Hash code of this Rectangle.

Inflate(Int32, Int32)

Adjusts the edges of this Rectangle by specified horizontal and vertical amounts.

Declaration
public void Inflate(int horizontalAmount, int verticalAmount)
Parameters
Type Name Description
Int32 horizontalAmount

Value to adjust the left and right edges.

Int32 verticalAmount

Value to adjust the top and bottom edges.

Inflate(Single, Single)

Adjusts the edges of this Rectangle by specified horizontal and vertical amounts.

Declaration
public void Inflate(float horizontalAmount, float verticalAmount)
Parameters
Type Name Description
Single horizontalAmount

Value to adjust the left and right edges.

Single verticalAmount

Value to adjust the top and bottom edges.

Intersect(Rectangle, Rectangle)

Creates a new Rectangle that contains overlapping region of two other rectangles.

Declaration
public static Rectangle Intersect(Rectangle value1, Rectangle value2)
Parameters
Type Name Description
Rectangle value1

The first Rectangle.

Rectangle value2

The second Rectangle.

Returns
Type Description
Rectangle

Overlapping region of the two rectangles.

Intersect(ref Rectangle, ref Rectangle, out Rectangle)

Creates a new Rectangle that contains overlapping region of two other rectangles.

Declaration
public static void Intersect(ref Rectangle value1, ref Rectangle value2, out Rectangle result)
Parameters
Type Name Description
Rectangle value1

The first Rectangle.

Rectangle value2

The second Rectangle.

Rectangle result

Overlapping region of the two rectangles as an output parameter.

Intersects(Rectangle)

Gets whether or not the other Rectangle intersects with this rectangle.

Declaration
public bool Intersects(Rectangle value)
Parameters
Type Name Description
Rectangle value

The other rectangle for testing.

Returns
Type Description
Boolean

true if other Rectangle intersects with this rectangle; false otherwise.

Intersects(ref Rectangle, out Boolean)

Gets whether or not the other Rectangle intersects with this rectangle.

Declaration
public void Intersects(ref Rectangle value, out bool result)
Parameters
Type Name Description
Rectangle value

The other rectangle for testing.

Boolean result

true if other Rectangle intersects with this rectangle; false otherwise. As an output parameter.

Offset(Point)

Changes the Location of this Rectangle.

Declaration
public void Offset(Point amount)
Parameters
Type Name Description
Point amount

The x and y components to add to this Rectangle.

Offset(Vector2)

Changes the Location of this Rectangle.

Declaration
public void Offset(Vector2 amount)
Parameters
Type Name Description
Vector2 amount

The x and y components to add to this Rectangle.

Offset(Int32, Int32)

Changes the Location of this Rectangle.

Declaration
public void Offset(int offsetX, int offsetY)
Parameters
Type Name Description
Int32 offsetX

The x coordinate to add to this Rectangle.

Int32 offsetY

The y coordinate to add to this Rectangle.

Offset(Single, Single)

Changes the Location of this Rectangle.

Declaration
public void Offset(float offsetX, float offsetY)
Parameters
Type Name Description
Single offsetX

The x coordinate to add to this Rectangle.

Single offsetY

The y coordinate to add to this Rectangle.

ToString()

Returns a representation of this Rectangle in the format: {X:[X] Y:[Y] Width:[Width] Height:[Height]}

Declaration
public override string ToString()
Returns
Type Description
String

representation of this Rectangle.

Union(Rectangle, Rectangle)

Creates a new Rectangle that completely contains two other rectangles.

Declaration
public static Rectangle Union(Rectangle value1, Rectangle value2)
Parameters
Type Name Description
Rectangle value1

The first Rectangle.

Rectangle value2

The second Rectangle.

Returns
Type Description
Rectangle

The union of the two rectangles.

Union(ref Rectangle, ref Rectangle, out Rectangle)

Creates a new Rectangle that completely contains two other rectangles.

Declaration
public static void Union(ref Rectangle value1, ref Rectangle value2, out Rectangle result)
Parameters
Type Name Description
Rectangle value1

The first Rectangle.

Rectangle value2

The second Rectangle.

Rectangle result

The union of the two rectangles as an output parameter.

Operators

Equality(Rectangle, Rectangle)

Compares whether two Rectangle instances are equal.

Declaration
public static bool operator ==(Rectangle a, Rectangle b)
Parameters
Type Name Description
Rectangle a

Rectangle instance on the left of the equal sign.

Rectangle b

Rectangle instance on the right of the equal sign.

Returns
Type Description
Boolean

true if the instances are equal; false otherwise.

Inequality(Rectangle, Rectangle)

Compares whether two Rectangle instances are not equal.

Declaration
public static bool operator !=(Rectangle a, Rectangle b)
Parameters
Type Name Description
Rectangle a

Rectangle instance on the left of the not equal sign.

Rectangle b

Rectangle instance on the right of the not equal sign.

Returns
Type Description
Boolean

true if the instances are not equal; false otherwise.

Implements

IEquatable<>
In This Article
Back to top

© 2012 Microsoft Corporation. All rights reserved.

© The MonoGame Team.