Change how a trihexaflexagon is drawn
[flexagon-toolkit.git] / src / flexagon / trihexaflexagon.py
1 #!/usr/bin/env python
2 #
3 # A generic model for a tri-hexaflexagon
4 #
5 # Copyright (C) 2018  Antonio Ospite <ao2@ao2.it>
6 #
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.
11 #
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.
16 #
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/>.
19
20 from math import cos, sin, pi
21
22
23 class Triangle(object):
24     def __init__(self, hexagon, index):
25         self.hexagon = hexagon
26         self.index = index
27
28     @staticmethod
29     def calc_plan_coordinates(radius, i, j):
30         apothem = radius * cos(pi / 3.)
31         side = 2. * radius * sin(pi / 3.)
32         width = side
33         height = apothem * 3.
34
35         xoffset = (j + 1) * width / 2.
36         yoffset = (i + (((i + j + 1) % 2) + 1) / 3.) * height
37
38         return xoffset, yoffset
39
40     def get_angle_in_plan(self):
41         """The angle of a triangle in the hexaflexagon plan."""
42         return - ((self.index) % 2) * pi / 3.
43
44     def get_angle_in_plan_relative_to_hexagon(self):
45         """"Get the angle of the triangle in the plan relative to the rotation
46         of the same triangle in the hexagon."""
47         return ((self.index + 5) % 6 // 2) * pi * 2 / 3 - pi / 6
48
49     def get_angle_in_hexagon(self):
50         """Get the angle of the triangle in the hexagons.
51
52         NOTE: the angle is rotated by pi to have the first triangle with the
53         base on the bottom."""
54         return pi + pi / 6. + self.index * pi / 3.
55
56     def __str__(self):
57         return "%d,%d" % (self.hexagon.index, self.index)
58
59
60 class Hexagon(object):
61     def __init__(self, index):
62         self.index = index
63         self.triangles = []
64         for i in range(6):
65             triangle = Triangle(self, i)
66             self.triangles.append(triangle)
67
68     def __str__(self):
69         output = ""
70         for i in range(0, 6):
71             output += str(self.triangles[i])
72             output += "\t"
73
74         return output
75
76
77 class TriHexaflexagon(object):
78     def __init__(self):
79         self.hexagons = []
80         for i in range(0, 3):
81             hexagon = Hexagon(i)
82             self.hexagons.append(hexagon)
83
84         # A plan is described by a mapping of the triangles in the hexagons,
85         # repositioned on a 2d grid.
86         #
87         # In the map below, the grid has two rows, each element of the grid is
88         # a pair (h, t), where 'h' is the index of the hexagon, and 't' is the
89         # index of the triangle in that hexagon.
90         plan_map = [
91             [(0, 5), (1, 4), (1, 3), (2, 2), (2, 1), (0, 2), (0, 1), (1, 0), (1, 5)],
92             [(2, 4), (2, 3), (0, 4), (0, 3), (1, 2), (1, 1), (2, 0), (2, 5), (0, 0)]
93         ]
94
95         # Preallocate a bi-dimensional array for an inverse mapping, this is
96         # useful to retrieve the position in the plan given a triangle.
97         self.plan_map_inv = [[-1 for t in h.triangles] for h in self.hexagons]
98
99         self.plan = []
100         for i, plan_map_row in enumerate(plan_map):
101             plan_row = []
102             for j, mapping in enumerate(plan_map_row):
103                 hexagon_index, triangle_index = mapping
104                 hexagon = self.hexagons[hexagon_index]
105                 triangle = hexagon.triangles[triangle_index]
106                 plan_row.append(triangle)
107
108                 self.plan_map_inv[hexagon_index][triangle_index] = (i, j)
109
110             self.plan.append(plan_row)
111
112     def get_triangle_plan_position(self, triangle):
113         return self.plan_map_inv[triangle.hexagon.index][triangle.index]
114
115     def __str__(self):
116         output = ""
117
118         for row in self.plan:
119             for triangle in row:
120                 output += "%s\t" % str(triangle)
121             output += "\n"
122
123         return output
124
125
126 def test():
127     trihexaflexagon = TriHexaflexagon()
128     print(trihexaflexagon)
129
130
131 if __name__ == "__main__":
132     test()