private MapArea visit(Coord position) { // Don't bother allocating a MapArea, because the starting point isn't valid. if (!Map[position]) { return(null); } var stack = new Stack <Coord>(); var area = new MapArea(); stack.Push(position); while (stack.Count != 0) { position = stack.Pop(); if (visited[position.X, position.Y] || !Map[position]) // Already visited, or not part of any mapArea { continue; } area.Add(position); visited[position.X, position.Y] = true; foreach (var c in AdjacencyMethod.Neighbors(position)) { if (c.X < 0 || c.Y < 0 || c.X >= Map.Width || c.Y >= Map.Height) // Out of bounds, thus not actually a neighbor { continue; } if (Map[c] && !visited[c.X, c.Y]) { stack.Push(c); } } } return(area); }
/// <summary> /// Gets a MapArea containing exactly those positions in both of the given MapAreas. /// </summary> /// <param name="area1">First MapArea.</param> /// <param name="area2">Second MapArea.</param> /// <returns>A MapArea containing exactly those positions in both of the given MapAreas.</returns> public static MapArea GetIntersection(MapArea area1, MapArea area2) { var retVal = new MapArea(); if (!area1.Bounds.Intersects(area2.Bounds)) { return(retVal); } if (area1.Count > area2.Count) { Utility.Swap(ref area1, ref area2); } foreach (var pos in area1.Positions) { if (area2.Contains(pos)) { retVal.Add(pos); } } return(retVal); }