Class Area
Represents an arbitrarily-shaped 2D area. Stores and provides access to a list of each unique position in the area.
Namespace: SadRogue.Primitives
Assembly: TheSadRogue.Primitives.dll
Syntax
public class Area : Object, IReadOnlyArea, IMatchable<IReadOnlyArea>, IEnumerable<Point>, IEnumerable
Constructors
Area(IEnumerable<Point>, IEqualityComparer<Point>)
Constructor that takes initial points to add to the area.
Declaration
public Area(IEnumerable<Point> initialPoints, IEqualityComparer<Point> pointHasher = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<Point> | initialPoints | Initial points to add to the area. |
IEqualityComparer<Point> | pointHasher | A custom equality comparer/hashing algorithm to use when storing points. If not specified, it defaults to using the Equals and GetHashCode functions of the Point struct. |
Area(IEqualityComparer<Point>)
Constructor.
Declaration
public Area(IEqualityComparer<Point> pointHasher = null)
Parameters
Type | Name | Description |
---|---|---|
IEqualityComparer<Point> | pointHasher | A custom equality comparer/hashing algorithm to use when storing points. If not specified, it defaults to using the Equals and GetHashCode functions of the Point struct. |
Area(IEqualityComparer<Point>, Point[])
Constructor that takes initial points to add to the area.
Declaration
public Area(IEqualityComparer<Point> pointHasher = null, params Point[] initialPoints)
Parameters
Type | Name | Description |
---|---|---|
IEqualityComparer<Point> | pointHasher | A custom equality comparer/hashing algorithm to use when storing points. If not specified, it defaults to using the Equals and GetHashCode functions of the Point struct. |
Point[] | initialPoints | Initial points to add to the area. |
Fields
PointHasher
Hashing algorithm being used to store points added to this area.
Declaration
public readonly IEqualityComparer<Point> PointHasher
Field Value
Type | Description |
---|---|
IEqualityComparer<Point> |
Properties
Bounds
Smallest possible rectangle that encompasses every position in the area.
Declaration
public Rectangle Bounds { get; }
Property Value
Type | Description |
---|---|
Rectangle |
Count
Number of (unique) positions in the area.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
Int32 |
Item[Int32]
Returns positions from the area in the same fashion you would via a list.
Declaration
public Point this[int index] { get; }
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | Index of list to retrieve. |
Property Value
Type | Description |
---|---|
Point |
UseIndexEnumeration
Whether or not it is more efficient for this implementation to use enumeration by index, rather than generic IEnumerable, when iterating over positions using ReadOnlyAreaPositionsEnumerator.
Declaration
public bool UseIndexEnumeration { get; }
Property Value
Type | Description |
---|---|
Boolean |
Remarks
Set this to true if your indexer implementation scales well (constant time), and is relatively fast. Implementations with more complex indexers should set this to false.
The default interface implementation returns false, in order to preserve backwards compatibility with previous versions.
If you set this to false, your IEnumerable.GetEnumerator() implementations must NOT call return a ReadOnlyAreaPositionsEnumerator, as this will create an infinite loop.
Methods
Add(IReadOnlyArea)
Adds all positions in the given map area to this one.
Declaration
public void Add(IReadOnlyArea area)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyArea | area | Area containing positions to add. |
Add(Point)
Adds the given position to the list of points within the area if it is not already in the list, or does nothing otherwise.
Declaration
public void Add(Point position)
Parameters
Type | Name | Description |
---|---|---|
Point | position | The position to add. |
Remarks
Because the class uses a hash set internally to determine what points have already been added, this is an average case O(1) operation.
Add(Rectangle)
Adds all positions in the given rectangle to the area, if they are not already present.
Declaration
public void Add(Rectangle rectangle)
Parameters
Type | Name | Description |
---|---|---|
Rectangle | rectangle | Rectangle indicating which points to add. |
Add(IEnumerable<Point>)
Adds the given positions to the list of points within the area if they are not already in the list.
Declaration
public void Add(IEnumerable<Point> positions)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<Point> | positions | Positions to add to the list. |
Add(Int32, Int32)
Adds the given position to the list of points within the area if it is not already in the list, or does nothing otherwise.
Declaration
public void Add(int positionX, int positionY)
Parameters
Type | Name | Description |
---|---|---|
Int32 | positionX | X-value of the position to add. |
Int32 | positionY | Y-value of the position to add. |
Remarks
Because the class uses a hash set internally to determine what points have already been added, this is an average case O(1) operation.
Contains(IReadOnlyArea)
Returns whether or not the given area is completely contained within the current one.
Declaration
public bool Contains(IReadOnlyArea area)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyArea | area | Area to check. |
Returns
Type | Description |
---|---|
Boolean | True if the given area is completely contained within the current one, false otherwise. |
Contains(Point)
Determines whether or not the given position is within the area or not.
Declaration
public bool Contains(Point position)
Parameters
Type | Name | Description |
---|---|---|
Point | position | The position to check. |
Returns
Type | Description |
---|---|
Boolean | True if the specified position is within the area, false otherwise. |
Contains(Int32, Int32)
Determines whether or not the given position is within the area or not.
Declaration
public bool Contains(int positionX, int positionY)
Parameters
Type | Name | Description |
---|---|---|
Int32 | positionX | X-value of the position to check. |
Int32 | positionY | Y-value of the position to check. |
Returns
Type | Description |
---|---|
Boolean | True if the specified position is within the area, false otherwise. |
GetDifference(IReadOnlyArea, IReadOnlyArea, IEqualityComparer<Point>)
Gets an area containing all positions in area1
, minus those that are in
area2
.
Declaration
public static Area GetDifference(IReadOnlyArea area1, IReadOnlyArea area2, IEqualityComparer<Point> pointHasher = null)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyArea | area1 | |
IReadOnlyArea | area2 | |
IEqualityComparer<Point> | pointHasher | A custom equality comparer/hashing algorithm to use when storing points in the new Area. If not specified, it defaults to using the Equals and GetHashCode functions of the Point struct. |
Returns
Type | Description |
---|---|
Area | A area with exactly those positions in |
GetEnumerator()
Returns an enumerator that iterates through all positions in the Area, in the order they were added.
Declaration
public List<Point>.Enumerator GetEnumerator()
Returns
Type | Description |
---|---|
List.Enumerator<> | An enumerator that iterates through all the positions in the Area, in the order they were added. |
GetIntersection(IReadOnlyArea, IReadOnlyArea, IEqualityComparer<Point>)
Gets an area containing exactly those positions contained in both of the given areas.
Declaration
public static Area GetIntersection(IReadOnlyArea area1, IReadOnlyArea area2, IEqualityComparer<Point> pointHasher = null)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyArea | area1 | |
IReadOnlyArea | area2 | |
IEqualityComparer<Point> | pointHasher | A custom equality comparer/hashing algorithm to use when storing points in the new Area. If not specified, it defaults to using the Equals and GetHashCode functions of the Point struct. |
Returns
Type | Description |
---|---|
Area | An area containing exactly those positions contained in both of the given areas. |
GetUnion(IReadOnlyArea, IReadOnlyArea, IEqualityComparer<Point>)
Gets an area containing every position in one or both given areas.
Declaration
public static Area GetUnion(IReadOnlyArea area1, IReadOnlyArea area2, IEqualityComparer<Point> pointHasher = null)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyArea | area1 | |
IReadOnlyArea | area2 | |
IEqualityComparer<Point> | pointHasher | A custom equality comparer/hashing algorithm to use when storing points in the new Area. If not specified, it defaults to using the Equals and GetHashCode functions of the Point struct. |
Returns
Type | Description |
---|---|
Area | An area containing only those positions in one or both of the given areas. |
Intersects(IReadOnlyArea)
Returns whether or not the given map area intersects the current one. If you intend to determine/use the exact intersection based on this return value, it is best to instead call the GetIntersection(IReadOnlyArea, IReadOnlyArea, IEqualityComparer<Point>), and check the number of positions in the result (0 if no intersection).
Declaration
public bool Intersects(IReadOnlyArea area)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyArea | area | The area to check. |
Returns
Type | Description |
---|---|
Boolean | True if the given map area intersects the current one, false otherwise. |
Matches(IReadOnlyArea)
Compares for equality. Returns true if the two areas contain exactly the same points.
Declaration
public bool Matches(IReadOnlyArea other)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyArea | other |
Returns
Type | Description |
---|---|
Boolean | True if the areas contain exactly the same points, false otherwise. |
Remove(IReadOnlyArea)
Removes all positions in the given map area from this one.
Declaration
public void Remove(IReadOnlyArea area)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyArea | area | Area containing positions to remove. |
Remove(Point)
Removes the given position specified from the area. Particularly when the remove operation operation changes the bounds, this operation can be expensive, so if you must do multiple remove operations, it would be best to group them into 1 using Remove(IEnumerable<Point>).
Declaration
public void Remove(Point position)
Parameters
Type | Name | Description |
---|---|---|
Point | position | The position to remove. |
Remove(Rectangle)
Removes all positions in the given rectangle from this area.
Declaration
public void Remove(Rectangle rectangle)
Parameters
Type | Name | Description |
---|---|---|
Rectangle | rectangle | Rectangle containing positions to remove. |
Remove(HashSet<Point>)
Removes the given positions from the specified area.
Declaration
public void Remove(HashSet<Point> positions)
Parameters
Type | Name | Description |
---|---|---|
HashSet<Point> | positions | Positions to remove. |
Remove(IEnumerable<Point>)
Removes the given positions from the specified area.
Declaration
public void Remove(IEnumerable<Point> positions)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<Point> | positions | Positions to remove. |
Remove(Func<Point, Boolean>)
Removes positions for which the given predicate returns true from the area.
Declaration
public void Remove(Func<Point, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
Func<Point, Boolean> | predicate | Predicate returning true for positions that should be removed. |
Remove(Int32, Int32)
Removes the given position specified from the area. Particularly when the remove operation operation changes the bounds, this operation can be expensive, so if you must do multiple remove operations, it would be best to group them into 1 using Remove(IEnumerable<Point>).
Declaration
public void Remove(int positionX, int positionY)
Parameters
Type | Name | Description |
---|---|---|
Int32 | positionX | X-value of the position to remove. |
Int32 | positionY | Y-value of the position to remove. |
ToString()
Returns the string of each position in the area, in a square-bracket enclosed list, eg. [(1, 2), (3, 4), (5, 6)].
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
String | A string representation of those coordinates in the area. |
Operators
Addition(Area, Point)
Creates an area with the positions all shifted by the given vector.
Declaration
public static Area operator +(Area lhs, Point rhs)
Parameters
Type | Name | Description |
---|---|---|
Area | lhs | |
Point | rhs | Vector to add to each position in |
Returns
Type | Description |
---|---|
Area | An area with the positions all translated by the given amount in x and y directions. |