载入中...
搜索中...
未找到
HexCoordinates.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
7#include "hexmap/HexMetrics.h"
8
9#include <cmath>
10#include <cstdint>
11
12namespace eve::hexmap {
13
23 std::int32_t x = 0;
24 std::int32_t z = 0;
25
27 constexpr HexCoordinates() noexcept = default;
29 constexpr HexCoordinates(std::int32_t xValue, std::int32_t zValue) noexcept : x(xValue), z(zValue) {}
30
32 [[nodiscard]] constexpr std::int32_t y() const noexcept { return -x - z; }
33
34 [[nodiscard]] constexpr bool operator==(const HexCoordinates& other) const noexcept {
35 return x == other.x && z == other.z;
36 }
38 [[nodiscard]] constexpr bool operator!=(const HexCoordinates& other) const noexcept { return !(*this == other); }
39
45 [[nodiscard]] constexpr HexCoordinates step(HexDirection direction, std::int32_t distance = 1) const noexcept {
46 switch (direction) {
47 case HexDirection::NE: return HexCoordinates{x, z + distance};
48 case HexDirection::E: return HexCoordinates{x + distance, z};
49 case HexDirection::SE: return HexCoordinates{x + distance, z - distance};
50 case HexDirection::SW: return HexCoordinates{x, z - distance};
51 case HexDirection::W: return HexCoordinates{x - distance, z};
52 case HexDirection::NW: return HexCoordinates{x - distance, z + distance};
53 }
54 return *this;
55 }
56
58 [[nodiscard]] constexpr std::int32_t distanceTo(const HexCoordinates& other) const noexcept {
59 const std::int32_t dx = x - other.x;
60 const std::int32_t dy = y() - other.y();
61 const std::int32_t dz = z - other.z;
62 const auto absi = [](std::int32_t v) { return v < 0 ? -v : v; };
63 return (absi(dx) + absi(dy) + absi(dz)) / 2;
64 }
65
67 [[nodiscard]] constexpr float hexX() const noexcept {
68 const std::int32_t half = floorDiv2(z);
69 return static_cast<float>(x + half) + ((z & 1) == 0 ? 0.f : 0.5f);
70 }
71
73 [[nodiscard]] constexpr float hexZ() const noexcept { return static_cast<float>(z) * HexMetrics::kOuterToInner; }
74
76 [[nodiscard]] constexpr std::int32_t chunkColumn() const noexcept {
77 return (x + floorDiv2(z)) / HexMetrics::kChunkSizeX;
78 }
79
81 [[nodiscard]] constexpr std::int32_t chunkRow() const noexcept { return z / HexMetrics::kChunkSizeZ; }
82
84 [[nodiscard]] static constexpr HexCoordinates fromOffset(std::int32_t offsetX, std::int32_t offsetZ) noexcept {
85 return HexCoordinates{offsetX - floorDiv2(offsetZ), offsetZ};
86 }
87
89 [[nodiscard]] constexpr std::int32_t offsetX() const noexcept { return x + floorDiv2(z); }
90
92 [[nodiscard]] constexpr std::int32_t offsetZ() const noexcept { return z; }
93
105 [[nodiscard]] static HexVec3 toWorldPosition(HexCoordinates coordinates) noexcept {
106 const float diameter = HexMetrics::innerDiameter();
107 const float fx = static_cast<float>(coordinates.x) + 0.5f * static_cast<float>(coordinates.z);
108 return HexVec3{fx * diameter, 0.f, static_cast<float>(coordinates.z) * HexMetrics::rowSpacing()};
109 }
110
120 [[nodiscard]] static HexCoordinates fromWorldPosition(HexVec3 position) noexcept;
121
122private:
124 [[nodiscard]] static constexpr std::int32_t floorDiv2(std::int32_t value) noexcept {
125 return value >= 0 ? value / 2 : -(((-value) + 1) / 2);
126 }
127};
128
129} // namespace eve::hexmap
double value
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
#define EVENGINE_API_WORLD
Definition Export.h:109
Pointy-top hex metrics, directions and vertex helpers.
float v
std::array< float, 3 > position
float distance
RoadLaneDirection direction
float dz
float dy
float dx
float offsetX
HexDirection
Hex facing directions, counter-clockwise from north-east.
Definition HexMetrics.h:59
Axial coordinates of one hex cell.
static HexVec3 toWorldPosition(HexCoordinates coordinates) noexcept
World-space centre of a cell before elevation and perturbation.
constexpr std::int32_t chunkRow() const noexcept
Chunk row this cell belongs to.
constexpr bool operator!=(const HexCoordinates &other) const noexcept
Operator !=.
static constexpr HexCoordinates fromOffset(std::int32_t offsetX, std::int32_t offsetZ) noexcept
Converts an odd-row offset pair into axial coordinates.
constexpr std::int32_t offsetZ() const noexcept
Offset Z (row) of this cell inside the rectangular grid.
constexpr std::int32_t distanceTo(const HexCoordinates &other) const noexcept
Cube distance to another cell (wrapping is not supported).
constexpr float hexX() const noexcept
Hex-space X: east-west neighbour spacing is 1.
constexpr std::int32_t offsetX() const noexcept
Offset X of this cell inside the rectangular grid.
constexpr bool operator==(const HexCoordinates &other) const noexcept
constexpr std::int32_t y() const noexcept
Cube Y component (-x - z).
constexpr HexCoordinates step(HexDirection direction, std::int32_t distance=1) const noexcept
Returns the neighbouring coordinates one step in direction.
constexpr float hexZ() const noexcept
Hex-space Z: row spacing is outerToInner.
constexpr HexCoordinates() noexcept=default
Hex coordinates.
constexpr std::int32_t chunkColumn() const noexcept
Chunk column this cell belongs to.
Minimal 3-component float vector used by the hex mesh builders.
Definition HexMetrics.h:18