-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexagon.go
128 lines (110 loc) · 3.14 KB
/
hexagon.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
/*
Copyright (C) 2014-2017 Wolfger Schramm <wolfger@spearwolf.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package kiwotigo
import (
"math"
)
const startAtAngle float64 = 90
// The Hexagon represents a hexagon.
type Hexagon struct {
Row, Col uint
Left, Top float64
vertices []Vec2
CenterPoint Vec2
Region *Region
NeighborNorth *Hexagon
NeighborNorthEast *Hexagon
NeighborSouthEast *Hexagon
NeighborSouth *Hexagon
NeighborSouthWest *Hexagon
NeighborNorthWest *Hexagon
NeighborRight *Hexagon
NeighborLeft *Hexagon
}
// Neighbor returns the neighbor hexagon by index.
//
// _1_
// 2/ \0
// 3\___/5
// 4
//
func (hex *Hexagon) Neighbor(index int) *Hexagon {
if index < 0 || index > 5 {
panic("Hexagon.Neighbor index is out of range")
}
switch index {
case 0:
return hex.NeighborNorthEast
case 1:
return hex.NeighborNorth
case 2:
return hex.NeighborNorthWest
case 3:
return hex.NeighborSouthWest
case 4:
return hex.NeighborSouth
case 5:
return hex.NeighborSouthEast
}
return nil
}
// NeighborsWithRegionCount returns the number of neighbor hexagons
// which are assigned to a region.
func (hex *Hexagon) NeighborsWithRegionCount() uint {
var count uint
if hex.NeighborNorthEast != nil && hex.NeighborNorthEast.Region != nil {
count++
}
if hex.NeighborNorth != nil && hex.NeighborNorth.Region != nil {
count++
}
if hex.NeighborNorthWest != nil && hex.NeighborNorthWest.Region != nil {
count++
}
if hex.NeighborSouthEast != nil && hex.NeighborSouthEast.Region != nil {
count++
}
if hex.NeighborSouth != nil && hex.NeighborSouth.Region != nil {
count++
}
if hex.NeighborSouthWest != nil && hex.NeighborSouthWest.Region != nil {
count++
}
return count
}
func (hex *Hexagon) makePoints(width, height uint) {
hex.vertices = make([]Vec2, 6)
mx, my := float64(width)/2, float64(height)/2
lx, ly := mx-1, my-1
for i := range hex.vertices {
r := (float64(i)*(360/6) + startAtAngle) * (math.Pi / 180)
hex.vertices[i] = Vec2{math.Floor(0.5 + (math.Sin(r)*lx + mx + hex.Left)), math.Floor(0.5 + (math.Cos(r)*ly + my + hex.Top))}
}
hex.CenterPoint = Vec2{mx + hex.Left, my + hex.Top}
}
// Vertex returns the Vec2 by index.
func (hex *Hexagon) Vertex(i int) *Vec2 {
return &hex.vertices[i]
}
// NewHexagon creates a new Hexagon.
//
// Normally this is done by the HexagonGrid
// and you do not need to create hexagons by yourself.
func NewHexagon(col, row, width, height uint, left, top float64) (hex *Hexagon) {
hex = new(Hexagon)
hex.Col, hex.Row = col, row
hex.Left, hex.Top = left, top
hex.makePoints(width, height)
return
}