Struct IntRect
- Namespace
- PAC.DataStructures
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
xRange
is empty and/oryRange
is 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
corner
IntVector2The corner diagonally opposite
oppositeCorner
.oppositeCorner
IntVector2The 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
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
rects
IntRect[]
Returns
Exceptions
- ArgumentNullException
rects
is null.- ArgumentException
rects
is empty.
BoundingRect(params IntVector2[])
Returns the smallest rect containing all the given vectors.
public static IntRect BoundingRect(params IntVector2[] vectors)
Parameters
vectors
IntVector2[]
Returns
Exceptions
- ArgumentNullException
vectors
is null.- ArgumentException
vectors
is empty.
BoundingRect(IEnumerable<IntRect>)
Returns the smallest rect containing all the given rects.
public static IntRect BoundingRect(IEnumerable<IntRect> rects)
Parameters
rects
IEnumerable<IntRect>
Returns
Exceptions
- ArgumentNullException
rects
is null.- ArgumentException
rects
is empty.
BoundingRect(IEnumerable<IntVector2>)
Returns the smallest rect containing all the given vectors.
public static IntRect BoundingRect(IEnumerable<IntVector2> vectors)
Parameters
vectors
IEnumerable<IntVector2>
Returns
Exceptions
- ArgumentNullException
vectors
is null.- ArgumentException
vectors
is 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
vector
IntVector2
Returns
- See Also
Contains(IntVector2)
Whether the point is in the rect.
public bool Contains(IntVector2 point)
Parameters
point
IntVector2
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
x
int
Returns
- See Also
ContainsY(int)
Returns whether the rect contains any points with the given y coord.
public bool ContainsY(int y)
Parameters
y
int
Returns
- See Also
Equals(IntRect)
The same as operator ==(IntRect, IntRect).
public bool Equals(IntRect other)
Parameters
other
IntRect
Returns
Equals(object)
The same as Equals(IntRect).
public override bool Equals(object obj)
Parameters
obj
object
Returns
FilterPointsInside(IEnumerable<IntVector2>)
Returns the elements of the sequence that are in this rect.
public IEnumerable<IntVector2> FilterPointsInside(IEnumerable<IntVector2> vectors)
Parameters
vectors
IEnumerable<IntVector2>
Returns
- IEnumerable<IntVector2>
A lazily-generated sequence containing the elements of
vectors
that are in this rect.
Remarks
Preserves the order of the sequence.
Exceptions
- ArgumentNullException
vectors
is 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
vectors
IEnumerable<IntVector2>
Returns
- IEnumerable<IntVector2>
A lazily-generated sequence containing the elements of
vectors
that are not in this rect.
Remarks
Preserves the order of the sequence.
Exceptions
- ArgumentNullException
vectors
is null.
- See Also
Flip(CardinalOrdinalAxis)
Returns the rect flipped across the given axis.
public IntRect Flip(CardinalOrdinalAxis axis)
Parameters
axis
CardinalOrdinalAxis
Returns
Exceptions
- ArgumentNullException
axis
is 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
other
IntRect
Returns
Exceptions
- InvalidOperationException
This rect and
other
do 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
a
andb
do not intersect.
- See Also
IsSquare(IntRect)
public static bool IsSquare(IntRect rect)
Parameters
rect
IntRect
Returns
- See Also
IsSubsetOf(IntRect)
Whether this rect is a subset of other
.
public bool IsSubsetOf(IntRect other)
Parameters
other
IntRect
Returns
- See Also
IsSupersetOf(IntRect)
Whether this rect is a superset of other
.
public bool IsSupersetOf(IntRect other)
Parameters
other
IntRect
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
rect
IntRect
Returns
- See Also
RandomPoint(Random)
Generates a uniformly random point within the rect.
public IntVector2 RandomPoint(Random random)
Parameters
random
Random
Returns
RandomSubRect(Random)
Returns the rect defined by two independently uniformly randomly generated points within the rect.
public IntRect RandomSubRect(Random random)
Parameters
random
Random
Returns
Rotate(QuadrantalAngle)
Returns the rect rotated clockwise by the given angle.
public IntRect Rotate(QuadrantalAngle angle)
Parameters
angle
QuadrantalAngle
Returns
- See Also
SetEquals(IntRect)
Whether the two rects describe the same set of points.
public bool SetEquals(IntRect other)
Parameters
other
IntRect
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
toClamp
IntRect
Returns
Remarks
Note this is only possible when toClamp
is at most as wide and at most as tall as this rect.
Exceptions
- ArgumentException
toClamp
is wider and/or taller than this rect.
Operators
operator +(IntRect, IntVector2)
Translates the rect by the given vector.
public static IntRect operator +(IntRect rect, IntVector2 vector)
Parameters
rect
IntRectvector
IntVector2
Returns
operator +(IntVector2, IntRect)
Translates the rect by the given vector.
public static IntRect operator +(IntVector2 vector, IntRect rect)
Parameters
vector
IntVector2rect
IntRect
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
rect
IntRect
Returns
- RectInt
implicit operator IntRect(RectInt)
Casts from Unity's RectInt.
public static implicit operator IntRect(RectInt rect)
Parameters
rect
RectInt
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
rect
IntRectvector
IntVector2
Returns
operator -(IntRect)
Negates each point in the rect.
public static IntRect operator -(IntRect rect)
Parameters
rect
IntRect