From a4cda4ce208a0601bbd9cd900e3b51bfb3ac7de2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Fri, 21 Mar 2014 13:17:42 +0100 Subject: [PATCH] fix Tile::parent() for negative x coords --- include/llmr/map/tile.hpp | 4 ++-- src/map/tile.cpp | 15 +++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/include/llmr/map/tile.hpp b/include/llmr/map/tile.hpp index 2ae1a64fe..0857cf17b 100644 --- a/include/llmr/map/tile.hpp +++ b/include/llmr/map/tile.hpp @@ -18,7 +18,7 @@ public: int8_t z = 0; int32_t x = 0, y = 0; - inline ID(uint8_t z, uint32_t x, uint32_t y) + inline ID(int8_t z, int32_t x, int32_t y) : z(z), x(x), y(y) { } @@ -31,7 +31,7 @@ public: return z == rhs.z && x == rhs.x && y == rhs.y; } - ID parent(int32_t z) const; + ID parent(int8_t z) const; void normalize(); ID normalized() const; std::forward_list children(int32_t z) const; diff --git a/src/map/tile.cpp b/src/map/tile.cpp index 163aa60af..e619c32bf 100644 --- a/src/map/tile.cpp +++ b/src/map/tile.cpp @@ -8,15 +8,14 @@ Tile::Tile(const ID& id) : id(id) { } -Tile::ID Tile::ID::parent(int32_t parent_z) const { +Tile::ID Tile::ID::parent(int8_t parent_z) const { assert(parent_z < z); - ID pos(z, x, y); - while (pos.z > parent_z) { - --pos.z; - pos.x = floor(pos.x / 2); - pos.y = floor(pos.y / 2); - } - return pos; + int32_t dim = pow(2, z - parent_z); + return Tile::ID{ + static_cast(parent_z), + static_cast(floor((float)x / dim)), + static_cast(y / dim) + }; } std::forward_list Tile::ID::children(int32_t child_z) const {