Table of Contents

Method IsConnected

Namespace
Gamelogic.Grids2
Assembly
Gamelogic.Grids2.dll

IsConnected<TPoint>(IGrid<TPoint>, IEnumerable<TPoint>, Func<TPoint, IEnumerable<TPoint>>)

The set of points is connected if there is a path from one point to each other point in the set. A path exists between two points if isConnected returns true for the two points, or there exists a third point that has a path to both.

public static bool IsConnected<TPoint>(IGrid<TPoint> grid, IEnumerable<TPoint> points, Func<TPoint, IEnumerable<TPoint>> getConnectedCells)

Parameters

grid IGrid<TPoint>

The grid on which to do the check.

points IEnumerable<TPoint>

The set of points to check. It is assumed all points are in the grid.

getConnectedCells Func<TPoint, IEnumerable<TPoint>>

The function to use to get the connected cells of the grid.

Returns

bool

Returns true if the collection of points are connected, false otherwise.

Type Parameters

TPoint

The type of point of the grid that this algorithm takes.

Examples

Checks whether the list of points are connected by color.

IsConnected(grid, points, (p, q) => grid[p].Color == grid[q].Color)

Remarks

Only points inside the given grid are considered. If this set is empty, by convention, false is returned.

The getConnectedCells function can also be considered a function that returns the neighbors of a point. The function getConnectedCells must be symmetric in the sense that if p is in the set of points connected to q, then q is in the set of points connected to p. The function need not filter points that lie outside the grid, this is done by this algorithm.

Exceptions

ArgumentNullException

grid, points or getConnectedCells is null.

IsConnected<TPoint, TCell>(IGrid<TPoint, TCell>, TPoint, TPoint, Func<TPoint, IEnumerable<TPoint>>, Func<TPoint, TPoint, bool>)

The set is connected if the set of points are neighbor-connected, and isNeighborsConnected return true for each two neighbors in the set.Two points are connected if they are neighbors, or one point has a neighbor that is neighbor-connected with the other point.

Another way to put this is, this function returns true if there is a set that connects point1 to point2.

public static bool IsConnected<TPoint, TCell>(IGrid<TPoint, TCell> grid, TPoint point1, TPoint point2, Func<TPoint, IEnumerable<TPoint>> getAllNeighbors, Func<TPoint, TPoint, bool> isNeighborsConnected)

Parameters

grid IGrid<TPoint, TCell>

The grid on which to do the check

point1 TPoint

The first point to check.

point2 TPoint

The second point to check.

getAllNeighbors Func<TPoint, IEnumerable<TPoint>>

The function to use to get all neighbors of the grid.

isNeighborsConnected Func<TPoint, TPoint, bool>

The function to use to check whether two neighbors are connected.

Returns

bool

Returns true if the two points are in a connected set.

Type Parameters

TPoint

The type of point of the grid that this algorithm takes.

TCell

The type of cell of the grid that this algorithm takes.

Examples

Checks whether the two points are connected by color.

IsConnected(grid, p1, p2, (p, q) => grid[p].Color == grid[q].Color)