/// <summary> /// Adds all coordinates in the given map area to this one. /// </summary> /// <param name="area">Area containing positions to add.</param> public void Add(IReadOnlyMapArea area) { foreach (var pos in area.Positions) { Add(pos); } }
/// <summary> /// 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 <see cref="MapArea.GetIntersection(IReadOnlyMapArea, IReadOnlyMapArea)"/>, and /// check the number of positions in the result (0 if no intersection). /// </summary> /// <param name="area">The area to check.</param> /// <returns>True if the given map area intersects the current one, false otherwise.</returns> public bool Intersects(IReadOnlyMapArea area) { if (!area.Bounds.Intersects(Bounds)) { return(false); } if (Count <= area.Count) { foreach (var pos in Positions) { if (area.Contains(pos)) { return(true); } } return(false); } foreach (var pos in area.Positions) { if (Contains(pos)) { return(true); } } return(false); }
/// <summary> /// Gets a new MapArea containing 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(IReadOnlyMapArea area1, IReadOnlyMapArea area2) { var retVal = new MapArea(); retVal.Add(area1); retVal.Add(area2); 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> /// Returns whether or not the given area is completely contained within the current one. /// </summary> /// <param name="area">Area to check.</param> /// <returns> /// True if the given area is completely contained within the current one, false otherwise. /// </returns> public bool Contains(IReadOnlyMapArea area) { if (!Bounds.Contains(area.Bounds)) { return(false); } foreach (var pos in area.Positions) { if (!Contains(pos)) { return(false); } } return(true); }
/// <summary> /// Determines the two points in the given map areas that are closest to each other, and returns /// them as a tuple of two Coords. The first Coord is the position in area1 to use, the second /// Coord is the position in area2 to use. /// </summary> /// <param name="area1">First MapArea to connect.</param> /// <param name="area2">Second MapArea to connect.</param> /// <returns> /// A tuple containing the coordinates from each MapArea to connect -- the first item in the /// tuple is the Coord in area1, the second is the Coord in area2. /// </returns> public Tuple <Coord, Coord> SelectConnectionPoints(IReadOnlyMapArea area1, IReadOnlyMapArea area2) { Coord c1 = Coord.NONE; Coord c2 = Coord.NONE; double minDist = double.MaxValue; foreach (var point1 in area1.Positions) { foreach (var point2 in area2.Positions) { double distance = DistanceCalculation.Calculate(point1, point2); if (distance < minDist) { c1 = point1; c2 = point2; minDist = distance; } } } return(new Tuple <Coord, Coord>(c1, c2)); }
/// <summary> /// Gets a MapArea containing exactly those positions contained 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 contained in both of the given MapAreas.</returns> public static MapArea GetIntersection(IReadOnlyMapArea area1, IReadOnlyMapArea 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); }
/// <summary> /// Removes all positions in the given map area from this one. /// </summary> /// <param name="area">Area containing positions to remove.</param> public void Remove(IReadOnlyMapArea area) => Remove(area.Positions);
/// <summary> /// Selects and returns a random point from each map area's positions list. /// </summary> /// <param name="area1">First map area to connect.</param> /// <param name="area2">Second area to connect</param> /// <returns>A tuple containing the selected positions.</returns> public Tuple <Coord, Coord> SelectConnectionPoints(IReadOnlyMapArea area1, IReadOnlyMapArea area2) => new Tuple <Coord, Coord>(area1.Positions.RandomItem(rng), area2.Positions.RandomItem(rng));
/// <summary> /// Selects and returns a the center point of the bounding rectangle for each map area's /// positions list. /// </summary> /// <param name="area1">First map area to connect.</param> /// <param name="area2">First map area to connect.</param> /// <returns>A tuple representing the center of the bounding box of each map area.</returns> public Tuple <Coord, Coord> SelectConnectionPoints(IReadOnlyMapArea area1, IReadOnlyMapArea area2) => new Tuple <Coord, Coord>(area1.Bounds.Center, area2.Bounds.Center);