diff options
Diffstat (limited to 'src/tilearea.cpp')
-rw-r--r-- | src/tilearea.cpp | 81 |
1 files changed, 56 insertions, 25 deletions
diff --git a/src/tilearea.cpp b/src/tilearea.cpp index 7572f9f63..046e22221 100644 --- a/src/tilearea.cpp +++ b/src/tilearea.cpp @@ -18,8 +18,11 @@ * @param start the start of the area * @param end the end of the area */ -TileArea::TileArea(TileIndex start, TileIndex end) +OrthogonalTileArea::OrthogonalTileArea(TileIndex start, TileIndex end) { + assert(start < MapSize()); + assert(end < MapSize()); + uint sx = TileX(start); uint sy = TileY(start); uint ex = TileX(end); @@ -37,7 +40,7 @@ TileArea::TileArea(TileIndex start, TileIndex end) * Add a single tile to a tile area; enlarge if needed. * @param to_add The tile to add */ -void TileArea::Add(TileIndex to_add) +void OrthogonalTileArea::Add(TileIndex to_add) { if (this->tile == INVALID_TILE) { this->tile = to_add; @@ -69,7 +72,7 @@ void TileArea::Add(TileIndex to_add) * @param ta the other tile area to check against. * @return true if they intersect. */ -bool TileArea::Intersects(const TileArea &ta) const +bool OrthogonalTileArea::Intersects(const OrthogonalTileArea &ta) const { if (ta.w == 0 || this->w == 0) return false; @@ -98,7 +101,7 @@ bool TileArea::Intersects(const TileArea &ta) const * @param tile Tile to test for. * @return True if the tile is inside the area. */ -bool TileArea::Contains(TileIndex tile) const +bool OrthogonalTileArea::Contains(TileIndex tile) const { if (this->w == 0) return false; @@ -115,7 +118,7 @@ bool TileArea::Contains(TileIndex tile) const /** * Clamp the tile area to map borders. */ -void TileArea::ClampToMap() +void OrthogonalTileArea::ClampToMap() { assert(this->tile < MapSize()); this->w = min(this->w, MapSizeX() - TileX(this->tile)); @@ -123,42 +126,70 @@ void TileArea::ClampToMap() } /** - * Construct the iterator. - * @param corner1 Tile from where to begin iterating. - * @param corner2 Tile where to end the iterating. + * Create a diagonal tile area from two corners. + * @param start First corner of the area. + * @param end Second corner of the area. */ -DiagonalTileIterator::DiagonalTileIterator(TileIndex corner1, TileIndex corner2) : TileIterator(corner2), base_x(TileX(corner2)), base_y(TileY(corner2)), a_cur(0), b_cur(0) +DiagonalTileArea::DiagonalTileArea(TileIndex start, TileIndex end) : tile(start) { - assert(corner1 < MapSize()); - assert(corner2 < MapSize()); - - int dist_x = TileX(corner1) - TileX(corner2); - int dist_y = TileY(corner1) - TileY(corner2); - this->a_max = dist_x + dist_y; - this->b_max = dist_y - dist_x; + assert(start < MapSize()); + assert(end < MapSize()); /* Unfortunately we can't find a new base and make all a and b positive because * the new base might be a "flattened" corner where there actually is no single * tile. If we try anyway the result is either inaccurate ("one off" half of the * time) or the code gets much more complex; * - * We also need to increment here to have equality as marker for the end of a row or - * column. Like that it's shorter than having another if/else in operator++ - */ - if (this->a_max > 0) { - this->a_max++; + * We also need to increment/decrement a and b here to have one-past-end semantics + * for a and b, just the way the orthogonal tile area does it for w and h. */ + + this->a = TileY(end) + TileX(end) - TileY(start) - TileX(start); + this->b = TileY(end) - TileX(end) - TileY(start) + TileX(start); + if (this->a > 0) { + this->a++; } else { - this->a_max--; + this->a--; } - if (this->b_max > 0) { - this->b_max++; + if (this->b > 0) { + this->b++; } else { - this->b_max--; + this->b--; } } /** + * Does this tile area contain a tile? + * @param tile Tile to test for. + * @return True if the tile is inside the area. + */ +bool DiagonalTileArea::Contains(TileIndex tile) const +{ + int a = TileY(tile) + TileX(tile); + int b = TileY(tile) - TileX(tile); + + int start_a = TileY(this->tile) + TileX(this->tile); + int start_b = TileY(this->tile) - TileX(this->tile); + + int end_a = start_a + this->a; + int end_b = start_b + this->b; + + /* Swap if necessary, preserving the "one past end" semantics. */ + if (start_a > end_a) { + int tmp = start_a; + start_a = end_a + 1; + end_a = tmp + 1; + } + if (start_b > end_b) { + int tmp = start_b; + start_b = end_b + 1; + end_b = tmp + 1; + } + + return (a >= start_a && a < end_a && b >= start_b && b < end_b); +} + +/** * Move ourselves to the next tile in the rectangle on the map. */ TileIterator &DiagonalTileIterator::operator++() |