diff --git a/src/tilematrix_type.hpp b/src/tilematrix_type.hpp index ad22a86..f4a43ae 100644 --- a/src/tilematrix_type.hpp +++ b/src/tilematrix_type.hpp @@ -98,16 +98,14 @@ class TileMatrix { uint tile_y = (TileY(tile) / N) * N; uint w = N, h = N; - if (tile_x >= extend * N) { - tile_x -= extend * N; - w += extend * N; - } - if (tile_y >= extend * N) { - tile_y -= extend * N; - h += extend * N; - } - if (tile_x + w < MapSizeX() - extend * N) w += extend * N; - if (tile_y + h < MapSizeY() - extend * N) h += extend * N; + w += min(extend * N, tile_x); + h += min(extend * N, tile_y); + + tile_x -= min(extend * N, tile_x); + tile_y -= min(extend * N, tile_y); + + w += min(extend * N, MapSizeX() - tile_x - w); + h += min(extend * N, MapSizeY() - tile_y - h); return TileArea(TileXY(tile_x, tile_y), w, h); }