forked from pmcxs/hexgrid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraction.go
More file actions
42 lines (35 loc) · 862 Bytes
/
Copy pathfraction.go
File metadata and controls
42 lines (35 loc) · 862 Bytes
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
package hexgrid
import "math"
// FractionHex provides a more precise representation for hexagons when precision is required.
// It's also represented in Cube Coordinates
type FractionalHex struct {
Q float64
R float64
S float64
}
func NewFractionalHex(q, r float64) FractionalHex {
return FractionalHex{Q: q, R: r, S: -q - r}
}
// Round returns a 'rounded' FractionalHex, returning a Regular Hex
func (h FractionalHex) Round() Hex {
roundToInt := func(a float64) int {
if a < 0 {
return int(a - 0.5)
}
return int(a + 0.5)
}
q := roundToInt(h.Q)
r := roundToInt(h.R)
s := roundToInt(h.S)
qDiff := math.Abs(float64(q) - h.Q)
rDiff := math.Abs(float64(r) - h.R)
sDiff := math.Abs(float64(s) - h.S)
if qDiff > rDiff && qDiff > sDiff {
q = -r - s
} else if rDiff > sDiff {
r = -q - s
} else {
s = -q - r
}
return Hex{q, r, s}
}