8a700698204b2e9096dd2112cb24891fb90a6af9
[flexagon-toolkit.git] / src / flexagon / tritetraflexagon.py
1 #!/usr/bin/env python
2 #
3 # A generic model for a tri-tetraflexagon
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 pi
21
22
23 class Tile(object):
24     def __init__(self, square, index):
25         self.square = square
26         self.index = index
27
28     @staticmethod
29     def calc_plan_coordinates(side, i, j):
30         xoffset = side / 2 + j * side
31         yoffset = side / 2 + i * side
32
33         return xoffset, yoffset
34
35     @staticmethod
36     def calc_angle_in_plan(i, j):
37         """The angle of a tile in the tetraflexagon plan."""
38         return pi * (i > 1)
39
40     def __str__(self):
41         return "%d,%d" % (self.square.index, self.index)
42
43
44 class Square(object):
45     def __init__(self, index):
46         self.index = index
47         self.tiles = []
48         for i in range(4):
49             tile = Tile(self, i)
50             self.tiles.append(tile)
51
52     def __str__(self):
53         output = ""
54         for i in range(0, 4):
55             output += str(self.tiles[i])
56             output += "\t"
57
58         return output
59
60
61 class TriTetraflexagon(object):
62     def __init__(self):
63         self.squares = []
64         for i in range(0, 3):
65             square = Square(i)
66             self.squares.append(square)
67
68         # A plan is described by a mapping of the tiles in the squares,
69         # repositioned on a 2d grid.
70         #
71         # In the map below, the grid has two rows, each element of the grid is
72         # a pair (s, t), where 's' is the index of the square, and 't' is the
73         # index of the tile in that square.
74         plan_map = [
75             [(2, 0), (2, 1), (0, 1), None,   None],
76             [None,   None,   (0, 3), (1, 2), (1, 3)],
77             [None,   None,   (2, 3), (2, 2), (0, 2)],
78             [(0, 0), (1, 1), (1, 0), None,   None],
79         ]
80
81         # Preallocate a bi-dimensional array for an inverse mapping, this is
82         # useful to retrieve the position in the plan given a tile.
83         self.plan_map_inv = [[-1 for t in h.tiles] for h in self.squares]
84
85         self.plan = []
86         for i, plan_map_row in enumerate(plan_map):
87             plan_row = []
88             for j, mapping in enumerate(plan_map_row):
89                 if mapping:
90                     square_index, tile_index = mapping
91                     square = self.squares[square_index]
92                     tile = square.tiles[tile_index]
93                     self.plan_map_inv[square_index][tile_index] = (i, j)
94                 else:
95                     tile = None
96
97                 plan_row.append(tile)
98
99             self.plan.append(plan_row)
100
101     def get_tile_plan_position(self, tile):
102         return self.plan_map_inv[tile.square.index][tile.index]
103
104     def __str__(self):
105         output = ""
106
107         for row in self.plan:
108             for tile in row:
109                 output += "%s\t" % str(tile)
110             output += "\n"
111
112         return output
113
114
115 def test():
116     tritetraflexagon = TriTetraflexagon()
117     print(tritetraflexagon)
118
119
120 if __name__ == "__main__":
121     test()