3 # A generic model for a tri-tetraflexagon
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/>.
24 def __init__(self, square, index):
29 def calc_plan_coordinates(side, i, j):
30 xoffset = side / 2 + j * side
31 yoffset = side / 2 + i * side
33 return xoffset, yoffset
36 def calc_angle_in_plan(i, j):
37 """The angle of a tile in the tetraflexagon plan."""
41 return "%d,%d" % (self.square.index, self.index)
45 def __init__(self, index):
50 self.tiles.append(tile)
55 output += str(self.tiles[i])
61 class TriTetraflexagon(object):
66 self.squares.append(square)
68 # A plan is described by a mapping of the tiles in the squares,
69 # repositioned on a 2d grid.
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.
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],
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]
86 for i, plan_map_row in enumerate(plan_map):
88 for j, mapping in enumerate(plan_map_row):
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)
99 self.plan.append(plan_row)
101 def get_tile_plan_position(self, tile):
102 return self.plan_map_inv[tile.square.index][tile.index]
107 for row in self.plan:
109 output += "%s\t" % str(tile)
116 tritetraflexagon = TriTetraflexagon()
117 print(tritetraflexagon)
120 if __name__ == "__main__":