-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex_math.py
45 lines (37 loc) · 1.74 KB
/
hex_math.py
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
# hexagonal coordinates
# three coordinates are used in 2 dimensional space
# r - radius, measured in tiles along one of six spokes.
# a * 60 = angle. a selects which spoke to transverse starting with 0 being north, and rotating counter-clockwise
# b - beta - moving at an angle of 120 degrees from "a", and counts in the python iterator range(r)
# for example, team A is at coordinates (rmax, 0, b) for b in range(2).
# then teams B and C are at (rmax, 2, b) and (rmax, 4, b).
# there is redundancy since the coordinates are over determined.
# for example, (rmax, 0, rmax) = (rmax, 1, 0).
# for example, (0, 0, 0), (rmax, 1, 0), (rmax, 2, 0) form an equal lateral triangle.
# tiles are numbered from 1 which is at hex location (0, 0, 0) which is at cx, cy.
# tile 1 is at
from math import pi, sin, cos, sqrt
t_height = 100
# trig functions using hexagonal angle (modulo 6)
def hsin(alpha): return sin(alpha * 60 * pi / 180)
def hcos(alpha): return cos(alpha * 60 * pi / 180)
def tile_generation(rmax, t_height):
result = []
tile = 0
for r in range(rmax):
for alpha in range(6 if r > 0 else 1):
brange = r if r > 0 else 1
#brange = 1 # testing
for b in range(brange):
x = t_height * (-r * hsin(alpha) - b * hsin(alpha + 2))
y = t_height * (-r * hcos(alpha) - b * hcos(alpha + 2))
result.append((tile, (r, alpha, b), (x, y)))
tile += 1
return result
if __name__ == "__main__":
hex_tile_spec = tile_generation(4, t_height)
for tile_spec in hex_tile_spec:
tile, rab, xy = tile_spec
r, alpha, b, x, y = rab + xy
length = sqrt(x**2 + y**2)
print(f'{tile}\t({r}, {alpha}, {b})\t({x:.2f}, {y:.2f}), len={length:.2f}')