Struct IntRect
A non-empty rectangular region of integer coordinates.
public readonly struct IntRect : IReadOnlyContains<IntVector2>, IReadOnlyCollection<IntVector2>, IEnumerable<IntVector2>, IEnumerable, IEquatable<IntRect>, ISetComparable<IntRect>
- Implements
- Inherited Members
- Extension Methods
Remarks
When we say an IntVector2 is in the rect, we mean that whole grid square is in the rect.
(0, 0), (0, 4), (3, 0), (3, 4) is a 4x5 rectangle:
(0, 4) -> # # # # <- (3, 4)
# # # #
# # # #
# # # #
(0, 0) -> # # # # <- (3, 0)
The enumerator iterates over the points in the rect, starting with the bottom row, read left to right, then the next row, etc.
This ordering is useful since it matches the expected order of pixels in Unity's Texture2D.SetPixels(Color[]).
Constructors
IntRect(IntRange, IntRange)
Creates a rect with the given ranges of x and y coordinates.
public IntRect(IntRange xRange, IntRange yRange)
Parameters
Remarks
The x and y ranges must be non-empty, as IntRects are non-empty.
Exceptions
- ArgumentException
xRangeis empty and/oryRangeis empty.
IntRect(IntVector2, IntVector2)
Creates an IntRect with the two given points as opposite corners (inclusive) of the rect.
public IntRect(IntVector2 corner, IntVector2 oppositeCorner)
Parameters
cornerIntVector2The corner diagonally opposite
oppositeCorner.oppositeCornerIntVector2The corner diagonally opposite
corner.
Remarks
corner = (3, 0) and oppositeCorner = (0, 4), the rect will have corners (0, 0), (0, 4), (3, 0), (3, 4):
(0, 4) -> # # # # <- (3, 4)
# # # #
# # # #
# # # #
(0, 0) -> # # # # <- (3, 0)
Fields
bottomLeft
The coordinates of the bottom-left point in the rect (inclusive).
public readonly IntVector2 bottomLeft
Field Value
- See Also
topRight
The coordinates of the top-right point in the rect (inclusive).
public readonly IntVector2 topRight
Field Value
- See Also
Properties
Count
The number of points in the rect.
public int Count { get; }
Property Value
bottomRight
The coordinates of the bottom-right point in the rect (inclusive).
public IntVector2 bottomRight { get; }
Property Value
- See Also
height
The number of integer points the rect covers vertically.
public int height { get; }
Property Value
- See Also
isSquare
public bool isSquare { get; }
Property Value
- See Also
maxX
The maximum x coordinate of points in the rect.
public int maxX { get; }
Property Value
- See Also
maxY
The maximum y coordinate of points in the rect.
public int maxY { get; }
Property Value
- See Also
minX
The minimum x coordinate of points in the rect.
public int minX { get; }
Property Value
- See Also
minY
The minimum y coordinate of points in the rect.
public int minY { get; }
Property Value
- See Also
size
public IntVector2 size { get; }
Property Value
- See Also
topLeft
The coordinates of the top-left point in the rect (inclusive).
public IntVector2 topLeft { get; }
Property Value
- See Also
width
The number of integer points the rect covers horizontally.
public int width { get; }
Property Value
- See Also
xRange
The inclusive range of x coordinates in the rect, in increasing order.
public IntRange xRange { get; }
Property Value
- See Also
yRange
The inclusive range of y coordinates in the rect, in increasing order.
public IntRange yRange { get; }
Property Value
Methods
BorderContains(IntVector2)
Whether the point is in the rect and has at least one adjacent (up/down/left/right) point not in the rect.
public bool BorderContains(IntVector2 point)
Parameters
pointIntVector2
Returns
- See Also
BorderContains(int, int)
Whether the point is in the rect and has at least one adjacent (up/down/left/right) point not in the rect.
public bool BorderContains(int x, int y)
Parameters
Returns
- See Also
BorderContainsX(int)
public bool BorderContainsX(int x)
Parameters
xint
Returns
- See Also
BorderContainsY(int)
public bool BorderContainsY(int y)
Parameters
yint
Returns
- See Also
BoundingRect(IntRect, IntRect)
Returns the smallest rect containing both the given rects.
public static IntRect BoundingRect(IntRect a, IntRect b)
Parameters
Returns
BoundingRect(params IntRect[])
Returns the smallest rect containing all the given rects.
public static IntRect BoundingRect(params IntRect[] rects)
Parameters
rectsIntRect[]
Returns
Exceptions
- ArgumentNullException
rectsis null.- ArgumentException
rectsis empty.
BoundingRect(params IntVector2[])
Returns the smallest rect containing all the given vectors.
public static IntRect BoundingRect(params IntVector2[] vectors)
Parameters
vectorsIntVector2[]
Returns
Exceptions
- ArgumentNullException
vectorsis null.- ArgumentException
vectorsis empty.
BoundingRect(IEnumerable<IntRect>)
Returns the smallest rect containing all the given rects.
public static IntRect BoundingRect(IEnumerable<IntRect> rects)
Parameters
rectsIEnumerable<IntRect>
Returns
Exceptions
- ArgumentNullException
rectsis null.- ArgumentException
rectsis empty.
BoundingRect(IEnumerable<IntVector2>)
Returns the smallest rect containing all the given vectors.
public static IntRect BoundingRect(IEnumerable<IntVector2> vectors)
Parameters
vectorsIEnumerable<IntVector2>
Returns
Exceptions
- ArgumentNullException
vectorsis null.- ArgumentException
vectorsis empty.
Clamp(IntVector2)
Returns the point in the rect that is closest to the given vector. Equivalently, it clamps the vector's x coord to be in the rect's range of x coords, and clamps the vector's y coord to be in the rect's range of y coords.
public IntVector2 Clamp(IntVector2 vector)
Parameters
vectorIntVector2
Returns
- See Also
Contains(IntVector2)
Whether the point is in the rect.
public bool Contains(IntVector2 point)
Parameters
pointIntVector2
Returns
- See Also
Contains(int, int)
Whether the point ( is in the rect.x, y)
public bool Contains(int x, int y)
Parameters
Returns
- See Also
ContainsX(int)
Returns whether the rect contains any points with the given x coord.
public bool ContainsX(int x)
Parameters
xint
Returns
- See Also
ContainsY(int)
Returns whether the rect contains any points with the given y coord.
public bool ContainsY(int y)
Parameters
yint
Returns
- See Also
Equals(IntRect)
The same as operator ==(IntRect, IntRect).
public bool Equals(IntRect other)
Parameters
otherIntRect
Returns
Equals(object)
The same as Equals(IntRect).
public override bool Equals(object obj)
Parameters
objobject
Returns
FilterPointsInside(IEnumerable<IntVector2>)
Returns the elements of the sequence that are in this rect.
public IEnumerable<IntVector2> FilterPointsInside(IEnumerable<IntVector2> vectors)
Parameters
vectorsIEnumerable<IntVector2>
Returns
- IEnumerable<IntVector2>
A lazily-generated sequence containing the elements of
vectorsthat are in this rect.
Remarks
Preserves the order of the sequence.
Exceptions
- ArgumentNullException
vectorsis null.
- See Also
FilterPointsOutside(IEnumerable<IntVector2>)
Returns the elements of the sequence that are outside this rect.
public IEnumerable<IntVector2> FilterPointsOutside(IEnumerable<IntVector2> vectors)
Parameters
vectorsIEnumerable<IntVector2>
Returns
- IEnumerable<IntVector2>
A lazily-generated sequence containing the elements of
vectorsthat are not in this rect.
Remarks
Preserves the order of the sequence.
Exceptions
- ArgumentNullException
vectorsis null.
- See Also
Flip(CardinalOrdinalAxis)
Returns the rect flipped across the given axis.
public IntRect Flip(CardinalOrdinalAxis axis)
Parameters
axisCardinalOrdinalAxis
Returns
Exceptions
- ArgumentNullException
axisis null.
- See Also
GetEnumerator()
Iterates over the points in the rect, starting with the bottom row, read left to right, then the next row, etc.
public IEnumerator<IntVector2> GetEnumerator()
Returns
Remarks
The ordering of this enumerator is useful since it matches the expected order of pixels in Unity's Texture2D.SetPixels(Color[]).
GetHashCode()
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.
Intersection(IntRect)
Returns the set intersection of the two rects.
public IntRect Intersection(IntRect other)
Parameters
otherIntRect
Returns
Exceptions
- InvalidOperationException
This rect and
otherdo not intersect.
- See Also
Intersection(IntRect, IntRect)
Returns the set intersection of the two rects.
public static IntRect Intersection(IntRect a, IntRect b)
Parameters
Returns
Exceptions
- InvalidOperationException
aandbdo not intersect.
- See Also
IsSquare(IntRect)
public static bool IsSquare(IntRect rect)
Parameters
rectIntRect
Returns
- See Also
IsSubsetOf(IntRect)
Whether this rect is a subset of other.
public bool IsSubsetOf(IntRect other)
Parameters
otherIntRect
Returns
- See Also
IsSupersetOf(IntRect)
Whether this rect is a superset of other.
public bool IsSupersetOf(IntRect other)
Parameters
otherIntRect
Returns
- See Also
Overlap(IntRect, IntRect)
Whether the two rects have any points in common.
public static bool Overlap(IntRect a, IntRect b)
Parameters
Returns
- See Also
Overlaps(IntRect)
Whether the two rects have any points in common.
public bool Overlaps(IntRect rect)
Parameters
rectIntRect
Returns
- See Also
RandomPoint(Random)
Generates a uniformly random point within the rect.
public IntVector2 RandomPoint(Random random)
Parameters
randomRandom
Returns
RandomSubRect(Random)
Returns a rect uniformly at random from the set of all (weak) sub-rects.
public IntRect RandomSubRect(Random random)
Parameters
randomRandom
Returns
Rotate(QuadrantalAngle)
Returns the rect rotated clockwise by the given angle.
public IntRect Rotate(QuadrantalAngle angle)
Parameters
angleQuadrantalAngle
Returns
- See Also
SetEquals(IntRect)
Whether the two rects describe the same set of points.
public bool SetEquals(IntRect other)
Parameters
otherIntRect
Returns
Remarks
The is the same as operator ==(IntRect, IntRect).
ToString()
Represents the rect as a string in the form "((bottom-left x coord, bottom-left y coord), (top-right x coord, top-right y coord))".
public override string ToString()
Returns
TranslateClamp(IntRect)
Translates toClamp so it is a subset of this rect. Chooses the smallest such translation.
public IntRect TranslateClamp(IntRect toClamp)
Parameters
toClampIntRect
Returns
Remarks
Note this is only possible when toClamp is at most as wide and at most as tall as this rect.
Exceptions
- ArgumentException
toClampis wider and/or taller than this rect.
XCentreIs(int)
For rects with an odd width, this returns whether x is the centre x coord of the rect.
For rects with an even width, this returns whether x is one of the two centre x coords of the rect.
public bool XCentreIs(int x)
Parameters
xint
Returns
YCentreIs(int)
For rects with an odd height, this returns whether y is the centre y coord of the rect.
For rects with an even height, this returns whether y is one of the two centre y coords of the rect.
public bool YCentreIs(int y)
Parameters
yint
Returns
Operators
operator +(IntRect, IntVector2)
Translates the rect by the given vector.
public static IntRect operator +(IntRect rect, IntVector2 vector)
Parameters
rectIntRectvectorIntVector2
Returns
operator +(IntVector2, IntRect)
Translates the rect by the given vector.
public static IntRect operator +(IntVector2 vector, IntRect rect)
Parameters
vectorIntVector2rectIntRect
Returns
operator ==(IntRect, IntRect)
Whether the two rects describe the same set of points.
public static bool operator ==(IntRect a, IntRect b)
Parameters
Returns
Remarks
This is just the default struct comparison (comparing fields).
- See Also
implicit operator RectInt(IntRect)
Casts to Unity's RectInt.
public static implicit operator RectInt(IntRect rect)
Parameters
rectIntRect
Returns
- RectInt
implicit operator IntRect(RectInt)
Casts from Unity's RectInt.
public static implicit operator IntRect(RectInt rect)
Parameters
rectRectInt
Returns
operator !=(IntRect, IntRect)
Whether the two rects describe different sets of points.
public static bool operator !=(IntRect a, IntRect b)
Parameters
Returns
Remarks
This is just the default struct comparison (comparing fields).
- See Also
operator -(IntRect, IntVector2)
Translates the rect by the given vector.
public static IntRect operator -(IntRect rect, IntVector2 vector)
Parameters
rectIntRectvectorIntVector2
Returns
operator -(IntRect)
Negates each point in the rect.
public static IntRect operator -(IntRect rect)
Parameters
rectIntRect