-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHexagon.py
executable file
·100 lines (86 loc) · 3.13 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
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
import math
# Point Class
class Point:
def __init__(self,x,y):
self.x, self.y = x,y
def __str__(self):
return 'Point({self.x},{self.y})'.format(self=self)
# Hexagon Class
# This class is used to create a hexagon
# The hexagon is created by creating a list of points
class Hexagon:
# Constructor
# Input: center Point(x,y), size, type of hexagon (pointy or flat)
def __init__(self,center,size,type='pointy'):
if type != 'pointy' and type != 'flat':
raise Exception("Type values available are 'pointy' and 'flat'")
self.c,self.s,self.type = center,size,type
# Calculate one corner of the hexagon
def corner(self,i):
deg = 30 if self.type == 'pointy' else 0
angle_deg = 60 * i - deg
angle_rad = math.pi/180 * angle_deg
return Point(self.c.x + self.s * math.cos(angle_rad), self.c.y + self.s * math.sin(angle_rad))
# Calculate all the corners of the hexagon
def points(self):
x,y=[],[]
for i in range(1,8):
x.append(self.corner(i).x)
y.append(self.corner(i).y)
return x,y
def top_right_hex_center(self):
a,b = (self.c.x + (math.sqrt(3)*self.s)/2,self.c.y+self.s*1.5)
if self.type == 'pointy':
return Point(a,b)
elif self.type == 'flat':
return Point(b,a)
def bottom_right_hex_center(self):
a,b = (self.c.x + (math.sqrt(3)*self.s)/2,self.c.y-self.s*1.5)
if self.type == 'pointy':
return Point(a,b)
elif self.type == 'flat':
return Point(b,a)
def bottom_left_hex_center(self):
a,b=(self.c.x - (math.sqrt(3)*self.s)/2,self.c.y-self.s*1.5)
if self.type == 'pointy':
return Point(a,b)
elif self.type == 'flat':
return Point(b,a)
def top_left_hex_center(self):
a,b=(self.c.x - (math.sqrt(3)*self.s)/2,self.c.y+self.s*1.5)
if self.type == 'pointy':
return Point(a,b)
elif self.type == 'flat':
return Point(b,a)
def right_hex_center(self):
a,b=(self.c.x + (math.sqrt(3)*self.s),self.c.y)
if self.type == 'pointy':
return Point(a,b)
elif self.type == 'flat':
return Point(b,a)
def left_hex_center(self):
a,b=(self.c.x - (math.sqrt(3)*self.s),self.c.y)
if self.type == 'pointy':
return Point(a,b)
elif self.type == 'flat':
return Point(b,a)
def __str__(self):
return 'Hexagon({c},{p})'.format(c=self.c,p=self.points())
def __repr__(self):
return self.__str__()
class HexagonGrid:
def __init__(self,x,y,size,t):
self.x=x
self.y=y
self.size=size
self.t=t
self.hexagons=[]
self.create_hexagons()
def create_hexagons(self):
for i in range(self.x):
self.hexagons.append([])
for j in range(self.y):
p = self.hexagons[i][j-1].right_hex_center() if j>0 else Point(i,j)
self.hexagons[i].append(Hexagon(p,self.size,self.t))
def __getitem__(self,i):
return self.hexagons[i]