/// <summary> /// Adds a new tile. /// </summary> /// <param name="tile"></param> /// <returns></returns> public void Add(Tile tile) { if (tile.Zoom < _range.Zoom && _range.Zoom - tile.Zoom < 3) { // add this tile to all the subtiles. var subTiles = tile.GetSubTiles(_range.Zoom); foreach (var subTile in subTiles) { if (_range.Contains(subTile)) { // ok, this tile is in this range. this.Add(subTile.Id, tile); } } } else if (tile.Zoom > _range.Zoom && tile.Zoom - _range.Zoom < 3) { // find the tile that this subtile belongs to. foreach (var rangedTile in _range) { if (rangedTile.Overlaps(tile)) { // add this this tile here. this.Add(rangedTile.Id, tile); // this tile can only belong in one place. return; } } } if (_range.Contains(tile)) { // this tile is already the correct zoom. this.Add(tile.Id, tile); } }
public bool IsOverlappedBy(IEnumerable <Tile> tiles) { Dictionary <int, HashSet <Tile> > dictionary = new Dictionary <int, HashSet <Tile> >(); foreach (Tile tile in tiles) { if (tile.Zoom <= this.Zoom) { if (tile.Overlaps(this)) { return(true); } } else { HashSet <Tile> tileSet; if (!dictionary.TryGetValue(tile.Zoom, out tileSet)) { tileSet = new HashSet <Tile>(); dictionary.Add(tile.Zoom, tileSet); } tileSet.Add(tile); } } foreach (KeyValuePair <int, HashSet <Tile> > keyValuePair in dictionary) { TileRange subTiles = this.GetSubTiles(keyValuePair.Key); int num = 0; foreach (Tile tile in keyValuePair.Value) { if (subTiles.Contains(tile)) { ++num; } } if (subTiles.Count == keyValuePair.Value.Count) { return(true); } } return(false); }
/// <summary> /// Move to the next tile. /// </summary> /// <returns></returns> public bool MoveNext() { if (_current == null) { // start with the center tile. int centerX = (int)System.Math.Floor((_range.XMax + _range.XMin) / 2.0); int centerY = (int)System.Math.Ceiling((_range.YMax + _range.YMin) / 2.0); _current = new Tile(centerX, centerY, _range.Zoom); _enumeratedTiles.Add(_current); return(true); } // check if there are more tiles to be enumerated. if (_range.Count <= _enumeratedTiles.Count) { // no more tiles left. return(false); } // try to move in the desired direction. Tile next = null; while (next == null) { // try until a valid tile is found. switch (_direction) { case DirectionEnum.Up: // up next = new Tile(_current.X, _current.Y - 1, _range.Zoom); if (_enumeratedTiles.Contains(next)) { // moving up does not work, try to move left. _direction = DirectionEnum.Left; next = null; } else { // moved up, try right. _direction = DirectionEnum.Right; } break; case DirectionEnum.Left: // left next = new Tile(_current.X - 1, _current.Y, _range.Zoom); if (_enumeratedTiles.Contains(next)) { // moving left does not work, try to move down. _direction = DirectionEnum.Down; next = null; } else { // moved left, try up. _direction = DirectionEnum.Up; } break; case DirectionEnum.Down: // down next = new Tile(_current.X, _current.Y + 1, _range.Zoom); if (_enumeratedTiles.Contains(next)) { // moving down does not work, try to move right. _direction = DirectionEnum.Right; next = null; } else { // moved down, try left. _direction = DirectionEnum.Left; } break; case DirectionEnum.Right: // right next = new Tile(_current.X + 1, _current.Y, _range.Zoom); if (_enumeratedTiles.Contains(next)) { // moving right does not work, try to move up. _direction = DirectionEnum.Up; next = null; } else { // moved right, try down. _direction = DirectionEnum.Down; } break; } // test if the next is in range. if (next != null && !_range.Contains(next)) { // not in range, do not enumerate but move to next. _current = next; // pretend the next has been enumerated. next = null; } } // ok, next was found. _current = next; _enumeratedTiles.Add(_current); return(true); }