载入中...
搜索中...
未找到
HexCoordinates.cpp
浏览该文件的文档.
2
3namespace eve::hexmap {
4
6 const float diameter = HexMetrics::innerDiameter();
7 if (diameter <= 0.f) return HexCoordinates{};
8 const float fx = position.x / diameter;
9 const float fy = -fx;
10 const float offset = position.z / (HexMetrics::outerRadius() * 3.f);
11
12 const float ax = fx - offset;
13 const float ay = fy - offset;
14
15 std::int32_t ix = static_cast<std::int32_t>(std::lround(ax));
16 std::int32_t iy = static_cast<std::int32_t>(std::lround(ay));
17 std::int32_t iz = static_cast<std::int32_t>(std::lround(-ax - ay));
18
19 if (ix + iy + iz != 0) {
20 const float dx = std::fabs(ax - static_cast<float>(ix));
21 const float dy = std::fabs(ay - static_cast<float>(iy));
22 const float dz = std::fabs(-ax - ay - static_cast<float>(iz));
23 if (dx > dy && dx > dz)
24 ix = -iy - iz;
25 else if (dz > dy)
26 iz = -ix - iy;
27 }
28 return HexCoordinates{ix, iz};
29}
30
31} // namespace eve::hexmap
int ax
Definition CaveMesh.cpp:113
int ay
Definition CaveMesh.cpp:113
Axial hex coordinates and world-space conversion.
size_t offset
std::array< float, 3 > position
float dz
float dy
float dx
static constexpr float outerRadius() noexcept
Radius of a hex cell in world units.
Definition HexMetrics.h:174
static constexpr float innerDiameter() noexcept
Width of one hex column.
Definition HexMetrics.h:178
Axial coordinates of one hex cell.
static HexCoordinates fromWorldPosition(HexVec3 position) noexcept
Converts a world-space XZ position back to cell coordinates.
Minimal 3-component float vector used by the hex mesh builders.
Definition HexMetrics.h:18