forked from pmcxs/hexgrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout.go
83 lines (71 loc) · 2.35 KB
/
layout.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
package hexgrid
import "math"
var (
// OrientationPointy ...
OrientationPointy = Orientation{
math.Sqrt(3.), math.Sqrt(3.) / 2., 0., 3. / 2., math.Sqrt(3.) / 3., -1. / 3., 0., 2. / 3., 0.5}
// OrientationFlat ...
OrientationFlat = Orientation{
3. / 2., 0., math.Sqrt(3.) / 2., math.Sqrt(3.), 2. / 3., 0., -1. / 3., math.Sqrt(3.) / 3., 0.}
)
type (
// Point defines a geometric point with coordinates (x,y)
Point struct {
X float64
Y float64
}
// Layout defines a data struct that holds the parameters of a layout
// of hex tiles
Layout struct {
Orientation Orientation
// Size defines the multiplication factor relative to the canonical
// hexagon, where the points are on a unit circle
Size Point
// Origin defines the center point for hexagon at 0,0
Origin Point
}
// Orientation defines the orientation of a layout of hex; pointy or flat
Orientation struct {
f0, f1, f2, f3, b0, b1, b2, b3, startAngle float64
}
)
// HexToPixel returns the center pixel for a given hexagon an a certain layout
func HexToPixel(l Layout, h Hex) Point {
M := l.Orientation
size := l.Size
origin := l.Origin
x := (M.f0*float64(h.Q) + M.f1*float64(h.R)) * size.X
y := (M.f2*float64(h.Q) + M.f3*float64(h.R)) * size.Y
return Point{x + origin.X, y + origin.Y}
}
// PixelToHex returns the corresponding hexagon axial coordinates for
// a given pixel on a certain layout
func PixelToHex(l Layout, p Point) FractionalHex {
M := l.Orientation
size := l.Size
origin := l.Origin
pt := Point{(p.X - origin.X) / size.X, (p.Y - origin.Y) / size.Y}
q := M.b0*pt.X + M.b1*pt.Y
r := M.b2*pt.X + M.b3*pt.Y
return FractionalHex{q, r, -q - r}
}
// EdgeOffset returns the edge offset of the hexago for the given
// layout and edge number, starting at the E vertex and proceeding in
// a counter-clockwise order
func EdgeOffset(l Layout, c int) Point {
M := l.Orientation
size := l.Size
angle := 2. * math.Pi * (M.startAngle - float64(c)) / 6.
return Point{size.X * math.Cos(angle), size.Y * math.Sin(angle)}
}
// Edges returns the corners of the hexagon for the given layout,
// starting at the E vertex and proceeding in a CCW order
func Edges(l Layout, h Hex) []Point {
corners := make([]Point, 0)
center := HexToPixel(l, h)
for i := 0; i < 6; i++ {
offset := EdgeOffset(l, i)
corners = append(corners, Point{center.X + offset.X, center.Y + offset.Y})
}
return corners
}