-
Notifications
You must be signed in to change notification settings - Fork 2
/
triangle.hpp
157 lines (134 loc) · 3.25 KB
/
triangle.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#pragma once
#include <array>
#include <utility>
#include <cmath>
#include <algorithm>
namespace Triangle {
const float Side = 173.20508075689f;
const float Height = 150.f;
const float HeightK = 0.86692549378f;
const float Center = 0.5773502f;
struct Pos
{
int x, y, s;
};
inline bool operator==(const Pos& lhs, const Pos& rhs)
{
return lhs.x == rhs.x
&& lhs.y == rhs.y
&& lhs.s == rhs.s;
}
inline bool operator!=(const Pos& lhs, const Pos& rhs)
{
return lhs.x != rhs.x
&& lhs.y != rhs.y
&& lhs.s != rhs.s;
}
using Vec2 = std::pair<float, float>;
inline Vec2 gridToWorld(
const Pos& pos,
float worldspaceTriangleSide,
float worldspaceTriangleHeight)
{
return Vec2{
pos.x * worldspaceTriangleSide + pos.y * worldspaceTriangleSide / 2.f,
pos.y * worldspaceTriangleHeight
};
}
inline Vec2 gridToWorld(const Pos& pos)
{
return Vec2{
pos.x * Side + pos.y * Side / 2.f,
pos.y * Height
};
}
inline Pos worldToGrid(
const Vec2& pos,
float worldspaceTriangleSide,
float worldspaceTriangleHeight)
{
float y = std::get<1>(pos) / worldspaceTriangleHeight;
float x = std::get<0>(pos) / worldspaceTriangleSide - y / 2.f;
bool s = (x - floor(x) + y - floor(y)) > 1.f;
return Pos{int(x), int(y), int(s)};
}
inline Pos worldToGrid(const Vec2& pos)
{
float y = std::get<1>(pos) / Height;
float x = std::get<0>(pos) / Side - y / 2.f;
bool s = (x - floor(x) + y - floor(y)) > 1.f;
return Pos{int(x), int(y), int(s)};
}
inline int toIndex(const Pos& pos, int w)
{
return pos.x * 2 + pos.y * w * 2 + pos.s;
}
inline int toIndex(int x, int y, int s, int w)
{
return x * 2 + y * w * 2 + s;
}
inline Pos fromIndex(int i, int w)
{
return Pos{(i / 2) % w, (i / 2) / w, i % 2};
}
inline int anotherSide(int s)
{
return (s == 0) ? 1 : 0;
}
inline std::array<Pos, 3> adjacentCoords(const Pos& pos)
{
int x = pos.x;
int y = pos.y;
int s = pos.s;
int x1 = x;
int y1 = y;
int s1 = anotherSide(s);
int x2 = s ? (x + 1) : x;
int y2 = s ? y : (y - 1);
int s2 = anotherSide(s);
int x3 = s ? x : (x - 1);
int y3 = s ? (y + 1) : y;
int s3 = anotherSide(s);
return std::array<Pos, 3>{
Pos{x1, y1, s1},
Pos{x2, y2, s2},
Pos{x3, y3, s3}
};
}
inline std::pair<int, int> gridToIntersectionPos(int i, int w)
{
return std::make_pair(i % (w + 1), i / (w + 1));
}
inline int intersectionPosToIndex(int x, int y, int w)
{
return x + y * (w + 1);
}
inline int distance(const Pos& a, const Pos& b)
{
auto dx = a.x - b.x;
auto dy = a.y - b.y;
auto ds = a.s - b.s;
return abs(dx) + abs(dy) + abs(dx + dy + ds);
}
inline int triangleDistanceDiagonal(const Pos& a, const Pos& b)
{
auto dx = a.x - b.x;
auto dy = a.y - b.y;
auto ds = a.s - b.s;
auto adx = abs(dx);
auto ady = abs(dy);
auto adz = abs(dx + dy + ds);
auto m = std::min({adx, ady, adz});
return adx + ady + adz - m;
}
inline int triangleDistanceFullDiagonal(const Pos& a, const Pos& b)
{
auto dx = a.x - b.x;
auto dy = a.y - b.y;
auto ds = a.s - b.s;
auto adx = abs(dx);
auto ady = abs(dy);
auto adz = abs(dx + dy + ds);
return std::max({adx, ady, adz});
}
}