trihexaflexagon: improve get_angle_in_plan_relative_to_hexagon()
[flexagon-toolkit.git] / src / flexagon / trihexaflexagon.py
index d79875e..7bd4f72 100755 (executable)
@@ -39,19 +39,34 @@ class Triangle(object):
 
     def get_angle_in_plan(self):
         """The angle of a triangle in the hexaflexagon plan."""
 
     def get_angle_in_plan(self):
         """The angle of a triangle in the hexaflexagon plan."""
-        return - ((self.index + 1) % 2) * pi / 3.
+        return - ((self.index) % 2) * pi / 3.
 
     def get_angle_in_plan_relative_to_hexagon(self):
         """"Get the angle of the triangle in the plan relative to the rotation
         of the same triangle in the hexagon."""
 
     def get_angle_in_plan_relative_to_hexagon(self):
         """"Get the angle of the triangle in the plan relative to the rotation
         of the same triangle in the hexagon."""
-        return ((self.index + 4) % 6 // 2) * pi * 2. / 3.
+        # The explicit formula for this angle would be:
+        #
+        #    pi + pi / 6 + (((self.index + 1) % 6) // 2) * pi * 2 / 3
+        #
+        # The meaning of the part regarding the index is the following:
+        #   - rotate the indices by 1
+        #   - group by 2 (because couples of triangles move together in the
+        #     plan)
+        #   - multiply the group by a rotation factor
+        #
+        # The explicit formula shows clearly that triangles move in groups of
+        # 2 in the plan.
+        #
+        # However, use an implicit form for robustness, so that if the other
+        # angle functions change this one can be left untouched.
+        return self.get_angle_in_hexagon() - self.get_angle_in_plan()
 
     def get_angle_in_hexagon(self):
         """Get the angle of the triangle in the hexagons.
 
         NOTE: the angle is rotated by pi to have the first triangle with the
         base on the bottom."""
 
     def get_angle_in_hexagon(self):
         """Get the angle of the triangle in the hexagons.
 
         NOTE: the angle is rotated by pi to have the first triangle with the
         base on the bottom."""
-        return pi + self.index * pi / 3.
+        return pi + pi / 6. + self.index * pi / 3.
 
     def __str__(self):
         return "%d,%d" % (self.hexagon.index, self.index)
 
     def __str__(self):
         return "%d,%d" % (self.hexagon.index, self.index)
@@ -88,8 +103,8 @@ class TriHexaflexagon(object):
         # a pair (h, t), where 'h' is the index of the hexagon, and 't' is the
         # index of the triangle in that hexagon.
         plan_map = [
         # a pair (h, t), where 'h' is the index of the hexagon, and 't' is the
         # index of the triangle in that hexagon.
         plan_map = [
-            [(0, 0), (1, 5), (1, 4), (2, 3), (2, 2), (0, 3), (0, 2), (1, 1), (1, 0)],
-            [(2, 5), (2, 4), (0, 5), (0, 4), (1, 3), (1, 2), (2, 1), (2, 0), (0, 1)]
+            [(0, 5), (1, 4), (1, 3), (2, 2), (2, 1), (0, 2), (0, 1), (1, 0), (1, 5)],
+            [(2, 4), (2, 3), (0, 4), (0, 3), (1, 2), (1, 1), (2, 0), (2, 5), (0, 0)]
         ]
 
         # Preallocate a bi-dimensional array for an inverse mapping, this is
         ]
 
         # Preallocate a bi-dimensional array for an inverse mapping, this is