Show / Hide Table of Contents

Class Area

Represents an arbitrarily-shaped 2D area. Stores and provides access to a list of each unique position in the area.

Inheritance
object
Area
Implements
IReadOnlyArea
IMatchable<IReadOnlyArea>
IEnumerable<Point>
IEnumerable
Inherited Members
object.GetType()
object.MemberwiseClone()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: SadRogue.Primitives
Assembly: TheSadRogue.Primitives.dll
Syntax
public class Area : IReadOnlyArea, IMatchable<IReadOnlyArea>, IEnumerable<Point>, IEnumerable

Constructors

View Source

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.

View Source

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.

View Source

Area(IEqualityComparer<Point>?, params 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

View Source

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

View Source

Bounds

Smallest possible rectangle that encompasses every position in the area.

Declaration
public Rectangle Bounds { get; }
Property Value
Type Description
Rectangle
View Source

Count

Number of (unique) positions in the area.

Declaration
public int Count { get; }
Property Value
Type Description
int
View Source

this[int]

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
int index

Index of list to retrieve.

Property Value
Type Description
Point
View Source

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
bool
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

View Source

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.

View Source

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.

View Source

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.

View Source

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.

View Source

Add(int, int)

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
int positionX

X-value of the position to add.

int 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.

View Source

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
bool

True if the given area is completely contained within the current one, false otherwise.

View Source

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
bool

True if the specified position is within the area, false otherwise.

View Source

Contains(int, int)

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
int positionX

X-value of the position to check.

int positionY

Y-value of the position to check.

Returns
Type Description
bool

True if the specified position is within the area, false otherwise.

View Source

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 area1 that are NOT in area2.

View Source

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<Point>.Enumerator

An enumerator that iterates through all the positions in the Area, in the order they were added.

View Source

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.

View Source

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.

View Source

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
bool

True if the given map area intersects the current one, false otherwise.

View Source

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
bool

True if the areas contain exactly the same points, false otherwise.

View Source

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.

View Source

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.

View Source

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.

View Source

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.

View Source

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.

View Source

Remove(Func<Point, bool>)

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, bool> predicate

Predicate returning true for positions that should be removed.

View Source

Remove(int, int)

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
int positionX

X-value of the position to remove.

int positionY

Y-value of the position to remove.

View Source

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.

Overrides
object.ToString()

Operators

View Source

operator +(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 lhs.

Returns
Type Description
Area

An area with the positions all translated by the given amount in x and y directions.

Implements

IReadOnlyArea
IMatchable<T>
IEnumerable<T>
IEnumerable

Extension Methods

PropertyChangedEventHelpers.SafelySetProperty<TObject, TProperty>(TObject, ref TProperty, TProperty, EventHandler<ValueChangedEventArgs<TProperty>>?)
PropertyChangedEventHelpers.SafelySetProperty<TObject, TProperty>(TObject, ref TProperty, TProperty, EventHandler<ValueChangedEventArgs<TProperty>>?, EventHandler<ValueChangedEventArgs<TProperty>>?)
ReadOnlyAreaExtensions.FastEnumerator(IReadOnlyArea)
ReadOnlyAreaExtensions.PerimeterPositions(IReadOnlyArea, AdjacencyRule)
ToStringExtensions.ExtendToString<T>(IEnumerable<T>, string, Func<T, string>?, string, string)
  • View Source
In this article
Back to top Generated by DocFX