Class MapArea
Represents an arbitrarily-shaped area of a map. Stores and provides access to a list of each unique position considered connected.
Inheritance
Implements
Inherited Members
Namespace: GoRogue.MapGeneration
Assembly: GoRogue.dll
Syntax
[Serializable]
public class MapArea : IReadOnlyMapArea
Constructors
| Improve this Doc View SourceMapArea()
Constructor.
Declaration
public MapArea()
Properties
| Improve this Doc View SourceBounds
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 currently stored list.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Positions
List of all (unique) positions in the list.
Declaration
public IReadOnlyList<Coord> Positions { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IReadOnlyList<Coord> |
Methods
| Improve this Doc View SourceAdd(Coord)
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(Coord position)
Parameters
Type | Name | Description |
---|---|---|
Coord | 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(IReadOnlyMapArea)
Adds all coordinates in the given map area to this one.
Declaration
public void Add(IReadOnlyMapArea area)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyMapArea | area | Area containing positions to add. |
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 whose points to add. |
Add(IEnumerable<Coord>)
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<Coord> positions)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<Coord> | 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 x, int y)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | x | X-coordinate of the position to add. |
System.Int32 | y | Y-coordinate 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(Coord)
Determines whether or not the given position is considered within the area or not.
Declaration
public bool Contains(Coord position)
Parameters
Type | Name | Description |
---|---|---|
Coord | position | The position to check. |
Returns
Type | Description |
---|---|
System.Boolean | True if the specified position is within the area, false otherwise. |
Contains(IReadOnlyMapArea)
Returns whether or not the given area is completely contained within the current one.
Declaration
public bool Contains(IReadOnlyMapArea area)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyMapArea | area | Area to check. |
Returns
Type | Description |
---|---|
System.Boolean | True if the given area is completely contained within the current one, false otherwise. |
Contains(Int32, Int32)
Determines whether or not the given position is considered within the area or not.
Declaration
public bool Contains(int x, int y)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | x | X-coordinate of the position to check. |
System.Int32 | y | Y-coordinate of the position to check. |
Returns
Type | Description |
---|---|
System.Boolean | True if the specified position is within the area, false otherwise. |
Equals(Object)
Same as operator==. Returns false of obj is not a MapArea.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | Object to compare |
Returns
Type | Description |
---|---|
System.Boolean | True if the object given is a MapArea and is equal (contains the same points), false otherwise. |
Overrides
GetDifference(IReadOnlyMapArea, IReadOnlyMapArea)
Gets a MapArea containing all positions in area1
, minus those that are in
area2
.
Declaration
public static MapArea GetDifference(IReadOnlyMapArea area1, IReadOnlyMapArea area2)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyMapArea | area1 | The first MapArea. |
IReadOnlyMapArea | area2 | The second MapArea. |
Returns
Type | Description |
---|---|
MapArea | A MapArea with exactly those positions in |
GetHashCode()
Returns hash of the underlying set.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
System.Int32 | Hash code for the underlying set. |
Overrides
GetIntersection(IReadOnlyMapArea, IReadOnlyMapArea)
Gets a MapArea containing exactly those positions contained in both of the given MapAreas.
Declaration
public static MapArea GetIntersection(IReadOnlyMapArea area1, IReadOnlyMapArea area2)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyMapArea | area1 | First MapArea. |
IReadOnlyMapArea | area2 | Second MapArea. |
Returns
Type | Description |
---|---|
MapArea | A MapArea containing exactly those positions contained in both of the given MapAreas. |
GetUnion(IReadOnlyMapArea, IReadOnlyMapArea)
Gets a new MapArea containing every position in one or both given map areas.
Declaration
public static MapArea GetUnion(IReadOnlyMapArea area1, IReadOnlyMapArea area2)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyMapArea | area1 | First MapArea. |
IReadOnlyMapArea | area2 | Second MapArea. |
Returns
Type | Description |
---|---|
MapArea | A MapArea containing only those positions in one or both of the given MapAreas. |
Intersects(IReadOnlyMapArea)
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(IReadOnlyMapArea, IReadOnlyMapArea), and check the number of positions in the result (0 if no intersection).
Declaration
public bool Intersects(IReadOnlyMapArea area)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyMapArea | area | The area to check. |
Returns
Type | Description |
---|---|
System.Boolean | True if the given map area intersects the current one, false otherwise. |
RandomPosition(Func<Coord, Boolean>, IGenerator)
Gets a random position from the MapArea for which the given selector returns true. Positions are repeatedly selected until a valid one is found.
Declaration
public Coord RandomPosition(Func<Coord, bool> selector, IGenerator rng = null)
Parameters
Type | Name | Description |
---|---|---|
System.Func<Coord, System.Boolean> | selector | A function that should return true for any coordinate that is a valid selection, and false otherwise. |
Troschuetz.Random.IGenerator | rng | The rng to use. Defaults to DefaultRNG. |
Returns
Type | Description |
---|---|
Coord | A random position from within the MapArea for which the selector given returns true. |
RandomPosition(IGenerator)
Gets a random position from the MapArea.
Declaration
public Coord RandomPosition(IGenerator rng = null)
Parameters
Type | Name | Description |
---|---|---|
Troschuetz.Random.IGenerator | rng | The rng to use. Defaults to DefaultRNG. |
Returns
Type | Description |
---|---|
Coord | A random position from within the MapArea. |
Remove(Coord)
Removes the given position specified from the MapArea. Particularly when the Remove 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<Coord>).
Declaration
public void Remove(Coord position)
Parameters
Type | Name | Description |
---|---|---|
Coord | position | The position to remove. |
Remove(IReadOnlyMapArea)
Removes all positions in the given map area from this one.
Declaration
public void Remove(IReadOnlyMapArea area)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyMapArea | area | Area containing positions to remove. |
Remove(Rectangle)
Removes all positions in the given Rectangle from this MapArea.
Declaration
public void Remove(Rectangle rectangle)
Parameters
Type | Name | Description |
---|---|---|
Rectangle | rectangle | Rectangle containing positions to remove. |
Remove(HashSet<Coord>)
Removes the given positions from the specified MapArea.
Declaration
public void Remove(HashSet<Coord> positions)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.HashSet<Coord> | positions | Positions to remove. |
Remove(IEnumerable<Coord>)
Removes the given positions from the specified MapArea.
Declaration
public void Remove(IEnumerable<Coord> positions)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<Coord> | positions | Positions to remove. |
Remove(Func<Coord, Boolean>)
Removes positions for which the given predicate returns true from the MapArea.
Declaration
public void Remove(Func<Coord, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
System.Func<Coord, System.Boolean> | predicate | Predicate returning true for positions that should be removed. |
Remove(Int32, Int32)
Removes the given position specified from the MapArea. Particularly when the Remove 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<Coord>).
Declaration
public void Remove(int x, int y)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | x | X-coordinate of the position to remove. |
System.Int32 | y | Y-coordinate of the position to remove. |
ToString()
Returns the string of each position in the MapArea, in a square-bracket enclosed list, eg. [(1, 2), (3, 4), (5, 6)].
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
System.String | A string representation of those coordinates in the MapArea. |
Overrides
Operators
| Improve this Doc View SourceAddition(MapArea, Coord)
Creates a new MapArea with the Coords all shifted by the given vector.
Declaration
public static MapArea operator +(MapArea lhs, Coord rhs)
Parameters
Type | Name | Description |
---|---|---|
MapArea | lhs | MapArea. |
Coord | rhs | Coord (vector) to add. |
Returns
Type | Description |
---|---|
MapArea | A new MapArea with the positions all translated by the given amount in x and y directions. |
Equality(MapArea, MapArea)
Compares for equality. Returns true if the two MapAreas are the same reference, or if they contain exactly the same points.
Declaration
public static bool operator ==(MapArea lhs, MapArea rhs)
Parameters
Type | Name | Description |
---|---|---|
MapArea | lhs | First MapArea to compare. |
MapArea | rhs | Second MapArea to compare. |
Returns
Type | Description |
---|---|
System.Boolean | True if the MapAreas contain exactly the same points, false otherwise. |
Inequality(MapArea, MapArea)
Inequality comparison -- true if the two areas do NOT contain exactly the same points.
Declaration
public static bool operator !=(MapArea lhs, MapArea rhs)
Parameters
Type | Name | Description |
---|---|---|
MapArea | lhs | First MapArea to compare. |
MapArea | rhs | Second MapArea to compare. |
Returns
Type | Description |
---|---|
System.Boolean | True if the MapAreas do NOT contain exactly the same points, false otherwise. |