3 # A generic model for a tri-hexaflexagon
5 # Copyright (C) 2018 Antonio Ospite <ao2@ao2.it>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from math import cos, sin, pi
23 class Triangle(object):
24 def __init__(self, hexagon, index):
25 self.hexagon = hexagon
29 def calc_plan_coordinates(radius, i, j):
30 apothem = radius * cos(pi / 3.)
31 side = 2. * radius * sin(pi / 3.)
35 xoffset = (j + 1) * width / 2.
36 yoffset = (i + (((i + j + 1) % 2) + 1) / 3.) * height
38 return xoffset, yoffset
40 def get_backface_index(self):
41 # The backfaces have the triangles in the reverse rotational order
44 def get_angle_in_plan(self):
45 """The angle of a triangle in the hexaflexagon plan."""
46 return - ((self.index) % 2) * pi / 3.
48 def get_angle_in_plan_relative_to_hexagon(self):
49 """"Get the angle of the triangle in the plan relative to the rotation
50 of the same triangle in the hexagon."""
51 # The explicit formula for this angle would be:
53 # pi + pi / 6 + (((self.index + 1) % 6) // 2) * pi * 2 / 3
55 # The meaning of the part regarding the index is the following:
56 # - rotate the indices by 1
57 # - group by 2 (because couples of triangles move together in the
59 # - multiply the group by a rotation factor
61 # The explicit formula shows clearly that triangles move in groups of
64 # However, use an implicit form for robustness, so that if the other
65 # angle functions change this one can be left untouched.
66 return self.get_angle_in_hexagon() - self.get_angle_in_plan()
68 def get_angle_in_backface_relative_to_hexagon(self):
70 """"Get the angle of the triangle in the backface relative to the
71 rotation of the same triangle in the hexagon."""
73 backface_triangle_index = self.get_backface_index()
74 # group triangles in couples
75 group = (((backface_triangle_index + 1) % 6) // 2)
76 return pi + pi * 2 / 3 * group
78 def get_angle_in_hexagon(self):
79 """Get the angle of the triangle in the hexagons.
81 NOTE: the angle is rotated by pi to have the first triangle with the
82 base on the bottom."""
83 return pi + pi / 6. + self.index * pi / 3.
86 return "%d,%d" % (self.hexagon.index, self.index)
89 class Hexagon(object):
90 def __init__(self, index):
94 triangle = Triangle(self, i)
95 self.triangles.append(triangle)
100 output += str(self.triangles[i])
106 class TriHexaflexagon(object):
109 for i in range(0, 3):
111 self.hexagons.append(hexagon)
113 # A plan is described by a mapping of the triangles in the hexagons,
114 # repositioned on a 2d grid.
116 # In the map below, the grid has two rows, each element of the grid is
117 # a pair (h, t), where 'h' is the index of the hexagon, and 't' is the
118 # index of the triangle in that hexagon.
120 [(0, 5), (1, 4), (1, 3), (2, 2), (2, 1), (0, 2), (0, 1), (1, 0), (1, 5)],
121 [(2, 4), (2, 3), (0, 4), (0, 3), (1, 2), (1, 1), (2, 0), (2, 5), (0, 0)]
124 # Preallocate a bi-dimensional array for an inverse mapping, this is
125 # useful to retrieve the position in the plan given a triangle.
126 self.plan_map_inv = [[-1 for t in h.triangles] for h in self.hexagons]
129 for i, plan_map_row in enumerate(plan_map):
131 for j, mapping in enumerate(plan_map_row):
132 hexagon_index, triangle_index = mapping
133 hexagon = self.hexagons[hexagon_index]
134 triangle = hexagon.triangles[triangle_index]
135 plan_row.append(triangle)
137 self.plan_map_inv[hexagon_index][triangle_index] = (i, j)
139 self.plan.append(plan_row)
141 def get_triangle_plan_position(self, triangle):
142 return self.plan_map_inv[triangle.hexagon.index][triangle.index]
147 for row in self.plan:
149 output += "%s\t" % str(triangle)
156 trihexaflexagon = TriHexaflexagon()
157 print(trihexaflexagon)
160 if __name__ == "__main__":