-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhex.go
235 lines (207 loc) · 4.36 KB
/
hex.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package hex
import (
"fmt"
"math"
"github.com/erinpentecost/hex/internal"
)
// Hex is a coordinate defined axially.
//
// [Q,R,S]
type Hex struct {
Q int64
R int64
}
// S is the implicit additional coordinate when using cubic coordinate system.
func (h Hex) S() int64 {
return -1 * (h.Q + h.R)
}
// ToHexFractional returns the fractional hex that is the center of this hex.
func (h Hex) ToHexFractional() HexFractional {
return HexFractional{
Q: float64(h.Q),
R: float64(h.R),
}
}
// Origin returns a new hex with origin (0,0) coordinates.
func Origin() Hex {
return Hex{
Q: 0,
R: 0,
}
}
// Direction returns a new hex coord offset from the origin
// in the given direction, which is a number from 0 to 5, inclusive.
// Positive Q axis is in the 0 direction.
// Positive R axis is in the 5 direction.
func Direction(direction int) Hex {
d := BoundFacing(direction)
switch d {
case 0:
return Hex{
Q: 1,
R: 0,
}
case 1:
return Hex{
Q: 1,
R: -1,
}
case 2:
return Hex{
Q: 0,
R: -1,
}
case 3:
return Hex{
Q: -1,
R: 0,
}
case 4:
return Hex{
Q: -1,
R: 1,
}
case 5:
return Hex{
Q: 0,
R: 1,
}
}
panic("should never get here.")
}
// Add combines two hexes.
func (h Hex) Add(x Hex) Hex {
o := Hex{
Q: x.Q + h.Q,
R: x.R + h.R,
}
return o
}
// Subtract combines two hexes.
func (h Hex) Subtract(x Hex) Hex {
o := Hex{
Q: h.Q - x.Q,
R: h.R - x.R,
}
return o
}
// Multiply scales a hex by a scalar value.
func (h Hex) Multiply(k int64) Hex {
o := Hex{
Q: h.Q * k,
R: h.R * k,
}
return o
}
func lerpFloat(a, b, t float64) float64 {
return a*(1.0-t) + b*t
}
func lerpInt(a int64, b int64, t float64) float64 {
return lerpFloat(float64(a), float64(b), t)
}
// LineTo returns all hexes in a line from point x to point b, inclusive.
// The order of elements is a line as you would expect.
func (h Hex) LineTo(x Hex) []Hex {
n := h.DistanceTo(x)
line := make([]Hex, 0)
step := 1.0 / math.Max(float64(n), 1.0)
for i := int64(0); i <= n; i++ {
line = append(line, LerpHex(h, x, step*float64(i)))
}
return line
}
// LerpHex finds a point between a and b weighted by t.
// See https://en.wikipedia.org/wiki/Linear_interpolation
func LerpHex(a Hex, b Hex, t float64) Hex {
hf := HexFractional{
lerpInt(a.Q, b.Q, t),
lerpInt(a.R, b.R, t),
}
return hf.ToHex()
}
func absInt(k int64) int64 {
if k > 0 {
return k
}
return -1 * k
}
// Length gets the length of the hex to the grid origin.
//
// This is the Manhattan Distance.
func (h Hex) Length() int64 {
return (absInt(h.Q) + absInt(h.R) + absInt(h.S())) / 2
}
// DistanceTo returns the distance between two hexes.
//
// This is the Manhattan Distance.
func (h Hex) DistanceTo(x Hex) int64 {
return h.Subtract(x).Length()
}
// Center returns the hex at the center of mass of the given points.
func Center(h ...Hex) HexFractional {
if len(h) == 0 {
return OriginFractional()
}
c := h[0]
for _, e := range h[1:] {
c = c.Add(e)
}
cf := c.ToHexFractional()
return cf.Multiply(1.0 / float64(len(h)))
}
// Neighbor returns the neighbor in the given directon.
func (h Hex) Neighbor(direction int) Hex {
d := Direction(direction)
return h.Add(d)
}
// Neighbors returns the neighbors.
func (h Hex) Neighbors() []Hex {
n := make([]Hex, 7)
for i := 0; i <= 6; i++ {
n[i] = h.Neighbor(i)
}
return n
}
// Transform applies a matrix transformation on the hex.
//
// Translation by tr,tq,ts:
//
// [[1,0,0,tr]
//
// [0,1,0,tq]
//
// [0,0,1,ts]
//
// [0,0,0,1]] // homogenous coords. ignored.
func (h Hex) Transform(t [4][4]int64) Hex {
p := Hex{
Q: t[0][0]*h.Q + t[0][1]*h.R + t[0][2]*h.S() + t[0][3],
R: t[1][0]*h.Q + t[1][1]*h.R + t[1][2]*h.S() + t[1][3],
}
/*
// No need to transform S since it's a derived field.
s := t[2][0]*h.Q + t[2][1]*h.R + t[2][2]*h.S() + t[2][3]
if p.S() != s {
panic("transformation matrix is bad")
}
*/
return p
}
func (h Hex) Rotate(pivot Hex, direction int) Hex {
d := BoundFacing(direction)
if d == 0 {
return h
}
if (pivot == Hex{}) {
return h.Transform(internal.RotationMatrixes[d])
}
return h.Subtract(pivot).Transform(internal.RotationMatrixes[d]).Add(pivot)
}
// BoundFacing maps the whole number set to 0-5.
func BoundFacing(facing int) int {
return internal.BoundFacing(facing)
}
// ToString converts the hex to a string.
func (h Hex) String() string {
return fmt.Sprintf("{%v, %v, %v}", h.Q, h.R, h.S())
}