Table of Contents

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. For example, the rect with corners (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

xRange IntRange

The range of x coordinates.

yRange IntRange

The range of y coordinates.

Remarks

The x and y ranges must be non-empty, as IntRects are non-empty.

Exceptions

ArgumentException

xRange is empty and/or yRange 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 IntVector2

The corner diagonally opposite oppositeCorner.

oppositeCorner IntVector2

The corner diagonally opposite corner.

Remarks

For example, with 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

IntVector2
See Also

topRight

The coordinates of the top-right point in the rect (inclusive).

public readonly IntVector2 topRight

Field Value

IntVector2
See Also

Properties

Count

The number of points in the rect.

public int Count { get; }

Property Value

int

bottomRight

The coordinates of the bottom-right point in the rect (inclusive).

public IntVector2 bottomRight { get; }

Property Value

IntVector2
See Also

height

The number of integer points the rect covers vertically.

public int height { get; }

Property Value

int
See Also

isSquare

Whether the rect is a square - i.e. the width and height are equal.

public bool isSquare { get; }

Property Value

bool
See Also

maxX

The maximum x coordinate of points in the rect.

public int maxX { get; }

Property Value

int
See Also

maxY

The maximum y coordinate of points in the rect.

public int maxY { get; }

Property Value

int
See Also

minX

The minimum x coordinate of points in the rect.

public int minX { get; }

Property Value

int
See Also

minY

The minimum y coordinate of points in the rect.

public int minY { get; }

Property Value

int
See Also

size

The vector (width, height).

public IntVector2 size { get; }

Property Value

IntVector2
See Also

topLeft

The coordinates of the top-left point in the rect (inclusive).

public IntVector2 topLeft { get; }

Property Value

IntVector2
See Also

width

The number of integer points the rect covers horizontally.

public int width { get; }

Property Value

int
See Also

xRange

The inclusive range of x coordinates in the rect, in increasing order.

public IntRange xRange { get; }

Property Value

IntRange
See Also

yRange

The inclusive range of y coordinates in the rect, in increasing order.

public IntRange yRange { get; }

Property Value

IntRange

Methods

BoundingRect(IntRect, IntRect)

Returns the smallest rect containing both the given rects.

public static IntRect BoundingRect(IntRect a, IntRect b)

Parameters

a IntRect
b IntRect

Returns

IntRect

BoundingRect(params IntRect[])

Returns the smallest rect containing all the given rects.

public static IntRect BoundingRect(params IntRect[] rects)

Parameters

rects IntRect[]

Returns

IntRect

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

IntRect

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

IntRect

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

IntRect

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

IntVector2
See Also

Contains(IntVector2)

Whether the point is in the rect.

public bool Contains(IntVector2 point)

Parameters

point IntVector2

Returns

bool
See Also

Contains(int, int)

Whether the point (x, y) is in the rect.

public bool Contains(int x, int y)

Parameters

x int
y int

Returns

bool
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

bool
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

bool
See Also

Equals(IntRect)

public bool Equals(IntRect other)

Parameters

other IntRect

Returns

bool

Equals(object)

The same as Equals(IntRect).

public override bool Equals(object obj)

Parameters

obj object

Returns

bool

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

IntRect

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

IEnumerator<IntVector2>

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

IntRect

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

a IntRect
b IntRect

Returns

IntRect

Exceptions

InvalidOperationException

a and b do not intersect.

See Also

IsSquare(IntRect)

Whether the rect is a square - i.e. the width and height are equal.

public static bool IsSquare(IntRect rect)

Parameters

rect IntRect

Returns

bool
See Also

IsSubsetOf(IntRect)

Whether this rect is a subset of other.

public bool IsSubsetOf(IntRect other)

Parameters

other IntRect

Returns

bool
See Also

IsSupersetOf(IntRect)

Whether this rect is a superset of other.

public bool IsSupersetOf(IntRect other)

Parameters

other IntRect

Returns

bool
See Also

Overlap(IntRect, IntRect)

Whether the two rects have any points in common.

public static bool Overlap(IntRect a, IntRect b)

Parameters

a IntRect
b IntRect

Returns

bool
See Also

Overlaps(IntRect)

Whether the two rects have any points in common.

public bool Overlaps(IntRect rect)

Parameters

rect IntRect

Returns

bool
See Also

RandomPoint(Random)

Generates a uniformly random point within the rect.

public IntVector2 RandomPoint(Random random)

Parameters

random Random

Returns

IntVector2

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

IntRect

Rotate(QuadrantalAngle)

Returns the rect rotated clockwise by the given angle.

public IntRect Rotate(QuadrantalAngle angle)

Parameters

angle QuadrantalAngle

Returns

IntRect
See Also

SetEquals(IntRect)

Whether the two rects describe the same set of points.

public bool SetEquals(IntRect other)

Parameters

other IntRect

Returns

bool

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

string

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

IntRect

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 IntRect
vector IntVector2

Returns

IntRect

operator +(IntVector2, IntRect)

Translates the rect by the given vector.

public static IntRect operator +(IntVector2 vector, IntRect rect)

Parameters

vector IntVector2
rect IntRect

Returns

IntRect

operator ==(IntRect, IntRect)

Whether the two rects describe the same set of points.

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

Parameters

a IntRect
b IntRect

Returns

bool

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

IntRect

operator !=(IntRect, IntRect)

Whether the two rects describe different sets of points.

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

Parameters

a IntRect
b IntRect

Returns

bool

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 IntRect
vector IntVector2

Returns

IntRect

operator -(IntRect)

Negates each point in the rect.

public static IntRect operator -(IntRect rect)

Parameters

rect IntRect

Returns

IntRect

See Also