-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhexagon.py
66 lines (58 loc) · 1.65 KB
/
hexagon.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import math
def calculate_polygons(startx, starty, endx, endy, radius):
# calculate side length given radius
sl = (2 * radius) * math.tan(math.pi / 6)
# calculate radius for a given side-length
# (a * (math.cos(math.pi / 6) / math.sin(math.pi / 6)) / 2)
# calculate coordinates of the hexagon points
# sin(30)
p = sl * 0.5
b = sl * math.cos(math.radians(30))
w = b * 2
h = 2 * sl
# offset start and end coordinates by hex widths and heights to guarantee coverage
startx = startx - w
starty = starty - h
endx = endx + w
endy = endy + h
origx = startx
origy = starty
# offsets for moving along and up rows
xoffset = b
yoffset = 3 * p
polygons = []
row = 1
counter = 0
while starty < endy:
if row % 2 == 0:
startx = origx + xoffset
else:
startx = origx
while startx < endx:
p1x = startx
p1y = starty + p
p2x = startx
p2y = starty + (3 * p)
p3x = startx + b
p3y = starty + h
p4x = startx + w
p4y = starty + (3 * p)
p5x = startx + w
p5y = starty + p
p6x = startx + b
p6y = starty
poly = [
(p1x, p1y),
(p2x, p2y),
(p3x, p3y),
(p4x, p4y),
(p5x, p5y),
(p6x, p6y),
(p1x, p1y)]
polygons.append(poly)
counter += 1
startx += w
starty += yoffset
row += 1
return polygons
print(calculate_polygons(1,1, 4, 4, 3))