Table of Contents

Struct Rectangle

Namespace
Microsoft.Xna.Framework
Assembly
MonoGame.Framework.dll

Describes a 2D-rectangle.

[DataContract]
public struct Rectangle : IEquatable<Rectangle>
Implements
Inherited Members

Constructors

Rectangle(Point, Point)

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

public Rectangle(Point location, Point size)

Parameters

location Point

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

size Point

The width and height of the created Rectangle.

Rectangle(int, int, int, int)

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

public Rectangle(int x, int y, int width, int height)

Parameters

x int

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

y int

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

width int

The width of the created Rectangle.

height int

The height of the created Rectangle.

Fields

Height

The height of this Rectangle.

[DataMember]
public int Height

Field Value

int

Width

The width of this Rectangle.

[DataMember]
public int Width

Field Value

int

X

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

[DataMember]
public int X

Field Value

int

Y

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

[DataMember]
public int Y

Field Value

int

Properties

Bottom

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

public int Bottom { get; }

Property Value

int

Center

A Point located in the center of this Rectangle.

public Point Center { get; }

Property Value

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.

public static Rectangle Empty { get; }

Property Value

Rectangle

IsEmpty

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

public bool IsEmpty { get; }

Property Value

bool

Left

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

public int Left { get; }

Property Value

int

Location

The top-left coordinates of this Rectangle.

public Point Location { get; set; }

Property Value

Point

Right

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

public int Right { get; }

Property Value

int

Size

The width-height coordinates of this Rectangle.

public Point Size { get; set; }

Property Value

Point

Top

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

public int Top { get; }

Property Value

int

Methods

Contains(Point)

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

public bool Contains(Point value)

Parameters

value Point

The coordinates to check for inclusion in this Rectangle.

Returns

bool

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

Contains(ref Point, out bool)

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

public void Contains(ref Point value, out bool result)

Parameters

value Point

The coordinates to check for inclusion in this Rectangle.

result bool

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.

public bool Contains(Rectangle value)

Parameters

value Rectangle

The Rectangle to check for inclusion in this Rectangle.

Returns

bool

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

Contains(ref Rectangle, out bool)

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

public void Contains(ref Rectangle value, out bool result)

Parameters

value Rectangle

The Rectangle to check for inclusion in this Rectangle.

result bool

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.

public bool Contains(Vector2 value)

Parameters

value Vector2

The coordinates to check for inclusion in this Rectangle.

Returns

bool

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

Contains(ref Vector2, out bool)

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

public void Contains(ref Vector2 value, out bool result)

Parameters

value Vector2

The coordinates to check for inclusion in this Rectangle.

result bool

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

Contains(int, int)

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

public bool Contains(int x, int y)

Parameters

x int

The x coordinate of the point to check for containment.

y int

The y coordinate of the point to check for containment.

Returns

bool

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

Contains(float, float)

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

public bool Contains(float x, float y)

Parameters

x float

The x coordinate of the point to check for containment.

y float

The y coordinate of the point to check for containment.

Returns

bool

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

Deconstruct(out int, out int, out int, out int)

Deconstruction method for Rectangle.

public void Deconstruct(out int x, out int y, out int width, out int height)

Parameters

x int
y int
width int
height int

Equals(Rectangle)

Compares whether current instance is equal to specified Rectangle.

public bool Equals(Rectangle other)

Parameters

other Rectangle

The Rectangle to compare.

Returns

bool

true if the instances are equal; false otherwise.

Equals(object)

Compares whether current instance is equal to specified object.

public override bool Equals(object obj)

Parameters

obj object

The object to compare.

Returns

bool

true if the instances are equal; false otherwise.

GetHashCode()

Gets the hash code of this Rectangle.

public override int GetHashCode()

Returns

int

Hash code of this Rectangle.

Inflate(int, int)

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

public void Inflate(int horizontalAmount, int verticalAmount)

Parameters

horizontalAmount int

Value to adjust the left and right edges.

verticalAmount int

Value to adjust the top and bottom edges.

Inflate(float, float)

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

public void Inflate(float horizontalAmount, float verticalAmount)

Parameters

horizontalAmount float

Value to adjust the left and right edges.

verticalAmount float

Value to adjust the top and bottom edges.

Intersect(Rectangle, Rectangle)

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

public static Rectangle Intersect(Rectangle value1, Rectangle value2)

Parameters

value1 Rectangle

The first Rectangle.

value2 Rectangle

The second Rectangle.

Returns

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.

public static void Intersect(ref Rectangle value1, ref Rectangle value2, out Rectangle result)

Parameters

value1 Rectangle

The first Rectangle.

value2 Rectangle

The second Rectangle.

result Rectangle

Overlapping region of the two rectangles as an output parameter.

Intersects(Rectangle)

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

public bool Intersects(Rectangle value)

Parameters

value Rectangle

The other rectangle for testing.

Returns

bool

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

Intersects(ref Rectangle, out bool)

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

public void Intersects(ref Rectangle value, out bool result)

Parameters

value Rectangle

The other rectangle for testing.

result bool

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

Offset(Point)

Changes the Location of this Rectangle.

public void Offset(Point amount)

Parameters

amount Point

The x and y components to add to this Rectangle.

Offset(Vector2)

Changes the Location of this Rectangle.

public void Offset(Vector2 amount)

Parameters

amount Vector2

The x and y components to add to this Rectangle.

Offset(int, int)

Changes the Location of this Rectangle.

public void Offset(int offsetX, int offsetY)

Parameters

offsetX int

The x coordinate to add to this Rectangle.

offsetY int

The y coordinate to add to this Rectangle.

Offset(float, float)

Changes the Location of this Rectangle.

public void Offset(float offsetX, float offsetY)

Parameters

offsetX float

The x coordinate to add to this Rectangle.

offsetY float

The y coordinate to add to this Rectangle.

ToString()

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

public override string ToString()

Returns

string

string representation of this Rectangle.

Union(Rectangle, Rectangle)

Creates a new Rectangle that completely contains two other rectangles.

public static Rectangle Union(Rectangle value1, Rectangle value2)

Parameters

value1 Rectangle

The first Rectangle.

value2 Rectangle

The second Rectangle.

Returns

Rectangle

The union of the two rectangles.

Union(ref Rectangle, ref Rectangle, out Rectangle)

Creates a new Rectangle that completely contains two other rectangles.

public static void Union(ref Rectangle value1, ref Rectangle value2, out Rectangle result)

Parameters

value1 Rectangle

The first Rectangle.

value2 Rectangle

The second Rectangle.

result Rectangle

The union of the two rectangles as an output parameter.

Operators

operator ==(Rectangle, Rectangle)

Compares whether two Rectangle instances are equal.

public static bool operator ==(Rectangle a, Rectangle b)

Parameters

a Rectangle

Rectangle instance on the left of the equal sign.

b Rectangle

Rectangle instance on the right of the equal sign.

Returns

bool

true if the instances are equal; false otherwise.

operator !=(Rectangle, Rectangle)

Compares whether two Rectangle instances are not equal.

public static bool operator !=(Rectangle a, Rectangle b)

Parameters

a Rectangle

Rectangle instance on the left of the not equal sign.

b Rectangle

Rectangle instance on the right of the not equal sign.

Returns

bool

true if the instances are not equal; false otherwise.