-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexagon_grid.go
128 lines (104 loc) · 3.25 KB
/
hexagon_grid.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"
)
type HexagonGrid struct {
Width, Height uint
canvasWidth, canvasHeight uint
hexagons []*Hexagon
}
func (grid *HexagonGrid) CanvasWidth() uint {
return grid.canvasWidth
}
func (grid *HexagonGrid) CanvasHeight() uint {
return grid.canvasHeight
}
func (grid *HexagonGrid) Hexagon(x, y uint) *Hexagon {
if x < grid.Width && y < grid.Height {
return grid.hexagons[y*grid.Width+x]
}
return nil
}
func (grid *HexagonGrid) setHexagon(x, y uint, hex *Hexagon) *Hexagon {
if x < grid.Width && y < grid.Height {
grid.hexagons[y*grid.Width+x] = hex
}
return hex
}
func NewHexagonGrid(width, height, hexWidth, hexHeight, paddingX, paddingY uint) (grid *HexagonGrid) {
grid = new(HexagonGrid)
grid.Width, grid.Height = width, height
grid.hexagons = make([]*Hexagon, width*height)
baseHex := NewHexagon(0, 0, hexWidth, hexHeight, 0, 0)
stepX := baseHex.Vertex(5).X - baseHex.Vertex(3).X
stepY := baseHex.Vertex(5).Y - baseHex.Vertex(1).Y
stepY1 := baseHex.Vertex(0).Y - baseHex.Vertex(1).Y
grid.canvasWidth = uint(float64(width-1)*stepX) + (width-1)*paddingX + hexWidth
grid.canvasHeight = uint(float64(height-1)*stepY) + (height-1)*paddingY + hexHeight + uint(stepY1)
var row, col uint
for ; row < height; row++ {
for col = 0; col < width; col++ {
left := math.Floor(0.5 + (float64(col)*stepX + float64(col*paddingX)))
top := float64(row)*stepY + float64(row*paddingY)
if col%2 == 1 {
top += stepY1
}
top = math.Floor(0.5 + top)
if row == 0 && col == 0 {
grid.hexagons[0] = baseHex
} else {
hex := NewHexagon(col, row, hexWidth, hexHeight, left, top)
grid.setHexagon(col, row, hex)
}
}
}
grid.connectNeighbors()
return
}
func (grid *HexagonGrid) connectNeighbors() {
var row, col uint
for ; row < grid.Height; row++ {
for col = 0; col < grid.Width; col++ {
_row := row + (col % 2)
hex := grid.Hexagon(col, row)
if col > 0 {
if _row < grid.Height {
hex.NeighborSouthWest = grid.Hexagon(col-1, _row)
}
if _row > 0 {
hex.NeighborNorthWest = grid.Hexagon(col-1, _row-1)
}
neighborLeft := grid.Hexagon(col-1, row)
hex.NeighborLeft = neighborLeft
neighborLeft.NeighborRight = hex
}
if row > 0 {
hex.NeighborNorth = grid.Hexagon(col, row-1)
}
if row < grid.Height-1 {
hex.NeighborSouth = grid.Hexagon(col, row+1)
}
if col < grid.Width-1 {
if _row > 0 {
hex.NeighborNorthEast = grid.Hexagon(col+1, _row-1)
}
if _row < grid.Height {
hex.NeighborSouthEast = grid.Hexagon(col+1, _row)
}
}
}
}
}