From eb27362020b7aaba087a54e6f32818e52453ea8b Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Thu, 28 Jun 2018 13:35:51 +0200 Subject: [PATCH] svg_hexaflexagon_editor: draw also the backfaces Draw also the hexagons backfaces, to make it easier to visualize the symmetry when folding the flexagon. --- src/flexagon/hexaflexagon_diagram.py | 25 +++++++++++++++++++++++++ src/flexagon/trihexaflexagon.py | 14 ++++++++++++++ src/svg_hexaflexagon_editor.py | 19 +++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/src/flexagon/hexaflexagon_diagram.py b/src/flexagon/hexaflexagon_diagram.py index 4bb029b..7b09c94 100755 --- a/src/flexagon/hexaflexagon_diagram.py +++ b/src/flexagon/hexaflexagon_diagram.py @@ -43,6 +43,10 @@ class HexaflexagonDiagram(object): self.plan_origin = (self.x_border * 2. + self.hexagon_radius / 2., self.x_border + self.triangle_radius / 3.) + # The offset of the backfaces relative to the first hexagon + self.backfaces_offsets = (0, + (self.hexagon_radius + self.x_border) * 2) + self.hexagons_color_map = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] def _init_centers(self): @@ -66,6 +70,15 @@ class HexaflexagonDiagram(object): def get_triangle_center(self, triangle): return self.triangles_centers[triangle.hexagon.index][triangle.index] + def get_triangle_center_in_backfaces(self, triangle): + """Get the center of this triangle but in the backface""" + x0, y0 = self.backfaces_offsets + + backface_triangle_index = triangle.get_backface_index() + x, y = self.triangles_centers[triangle.hexagon.index][backface_triangle_index] + + return x0 + x, y0 + y + def get_triangle_center_in_plan(self, triangle): x0, y0 = self.plan_origin i, j = self.hexaflexagon.get_triangle_plan_position(triangle) @@ -91,6 +104,18 @@ class HexaflexagonDiagram(object): return self.backend.calc_rotate_translate_transform(src_x, src_y, dest_x, dest_y, theta) + def get_triangle_backfaces_transform(self, triangle): + """Calculate the transformation matrix from a triangle in an hexagon to + the correspondent triangle in a backface. + + Return the matrix as a list of values sorted in row-major order.""" + src_x, src_y = self.get_triangle_center(triangle) + dest_x, dest_y = self.get_triangle_center_in_backfaces(triangle) + theta = triangle.get_angle_in_backface_relative_to_hexagon() + + return self.backend.calc_rotate_translate_transform(src_x, src_y, + dest_x, dest_y, theta) + def draw_hexagon_template(self, hexagon): for triangle in hexagon.triangles: cx, cy = self.get_triangle_center(triangle) diff --git a/src/flexagon/trihexaflexagon.py b/src/flexagon/trihexaflexagon.py index 7bd4f72..ca21726 100755 --- a/src/flexagon/trihexaflexagon.py +++ b/src/flexagon/trihexaflexagon.py @@ -37,6 +37,10 @@ class Triangle(object): return xoffset, yoffset + def get_backface_index(self): + # The backfaces have the triangles in the reverse rotational order + return 5 - self.index + def get_angle_in_plan(self): """The angle of a triangle in the hexaflexagon plan.""" return - ((self.index) % 2) * pi / 3. @@ -61,6 +65,16 @@ class Triangle(object): # angle functions change this one can be left untouched. return self.get_angle_in_hexagon() - self.get_angle_in_plan() + def get_angle_in_backface_relative_to_hexagon(self): + + """"Get the angle of the triangle in the backface relative to the + rotation of the same triangle in the hexagon.""" + + backface_triangle_index = self.get_backface_index() + # group triangles in couples + group = (((backface_triangle_index + 1) % 6) // 2) + return pi + pi * 2 / 3 * group + def get_angle_in_hexagon(self): """Get the angle of the triangle in the hexagons. diff --git a/src/svg_hexaflexagon_editor.py b/src/svg_hexaflexagon_editor.py index 48c8646..d64468b 100755 --- a/src/svg_hexaflexagon_editor.py +++ b/src/svg_hexaflexagon_editor.py @@ -32,6 +32,7 @@ class SvgwriteHexaflexagonDiagram(HexaflexagonDiagram): # create some layers and groups layers = { "Hexagons": svg.layer(label="Hexagons"), + "Backfaces": svg.layer(label="Backfaces"), "Hexaflexagon": svg.layer(label="Hexaflexagon"), "Folding guide": svg.layer(label="Folding guide"), "Template": svg.layer(label="Template") @@ -110,6 +111,24 @@ class SvgwriteHexaflexagonDiagram(HexaflexagonDiagram): ref['clip-path'] = "url(%s)" % (triangle_href + '-clip-path') group.add(ref) + # draw the backfaces + group = self.groups["Backfaces"] + for hexagon in self.hexaflexagon.hexagons: + for triangle in hexagon.triangles: + m = self.get_triangle_backfaces_transform(triangle) + svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3], + m[1], m[4], + m[2], m[5]) + + triangle_href = "#hexagon%d-triangle%d" % (hexagon.index, triangle.index) + + # Reuse the content to draw the backface + content_href = "#hexagon%d-content" % hexagon.index + ref = self.backend.svg.use(content_href) + ref['transform'] = svg_matrix + ref['clip-path'] = "url(%s)" % (triangle_href + '-clip-path') + group.add(ref) + def draw_triangle_template(self, triangle, cx, cy, theta): old_active_group = self.backend.active_group group_name = "hexagon%d-triangle%d" % (triangle.hexagon.index, triangle.index) -- 2.1.4