/// <summary> /// Gets a new MapArea containing exactly every position in one or both given map areas. /// </summary> /// <param name="area1">First MapArea.</param> /// <param name="area2">Second MapArea.</param> /// <returns>A MapArea containing only those positions in one or both of the given MapAreas.</returns> public static MapArea GetUnion(MapArea area1, MapArea area2) { var retVal = new MapArea(); retVal.Add(area1); retVal.Add(area2); return(retVal); }
private MapArea visit(Coord position) { 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> /// Creates a new MapArea with the Coords all shifted by the given vector. /// </summary> /// <param name="lhs">MapArea.</param> /// <param name="rhs">Coord (vector) to add.</param> /// <returns> /// A new MapArea with the positions all translated by the given amount in x and y directions. /// </returns> public static MapArea operator +(MapArea lhs, Coord rhs) { var retVal = new MapArea(); foreach (var pos in lhs.Positions) { retVal.Add(pos + rhs); } return(retVal); }
/// <summary> /// Gets a MapArea containing all positions in <paramref name="area1"/>, minus those that are in /// <paramref name="area2"/>. /// </summary> /// <param name="area1">The first MapArea.</param> /// <param name="area2">The second MapArea.</param> /// <returns>A MapArea with exactly those positions in <paramref name="area1"/> that are NOT in /// <paramref name="area2"/>.</returns> public static MapArea GetDifference(IReadOnlyMapArea area1, IReadOnlyMapArea area2) { var retVal = new MapArea(); foreach (var pos in area1.Positions) { if (area2.Contains(pos)) { continue; } retVal.Add(pos); } return(retVal); }
/// <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); }