3 # Draw an SVG hexaflexagon which can be edited live in Inkscape.
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/>.
22 from diagram.svgwrite_diagram import SvgwriteDiagram
23 from flexagon.hexaflexagon_diagram import HexaflexagonDiagram
26 class SvgwriteHexaflexagonDiagram(HexaflexagonDiagram):
27 def __init__(self, *args, **kwargs):
28 super(SvgwriteHexaflexagonDiagram, self).__init__(*args, **kwargs)
30 svg = self.backend.svg
32 # create some layers and groups
34 "Hexagons": svg.layer(label="Hexagons"),
35 "Backfaces": svg.layer(label="Backfaces"),
36 "Hexaflexagon": svg.layer(label="Hexaflexagon"),
37 "Folding guide": svg.layer(label="Folding guide"),
38 "Template": svg.layer(label="Template")
40 for layer in layers.values():
45 for hexagon in self.hexaflexagon.hexagons:
46 name = "hexagon%d-content" % hexagon.index
47 layer = svg.layer(id=name, label="Hexagon %d" % (hexagon.index + 1))
48 self.groups[name] = layer
49 layers['Hexagons'].add(layer)
51 for triangle in hexagon.triangles:
52 name = "hexagon%d-triangle%d" % (hexagon.index, triangle.index)
53 group = svg.g(id=name)
54 self.groups[name] = group
55 layers['Template'].add(group)
58 for hexagon in self.hexaflexagon.hexagons:
59 cx, cy = self.get_hexagon_center(hexagon)
61 # Draw some default content
62 old_active_group = self.backend.active_group
63 self.backend.active_group = self.groups["hexagon%d-content" % hexagon.index]
64 self.backend.draw_regular_polygon(cx, cy, 6, self.hexagon_radius, fill_color=(0.5, 0.5, 0.5, 0.2), stroke_color=None)
65 self.backend.active_group = old_active_group
68 old_active_group = self.backend.active_group
69 self.backend.active_group = self.groups["Folding guide"]
71 for triangle in hexagon.triangles:
72 cx, cy = self.get_triangle_center(triangle)
73 theta = triangle.get_angle_in_hexagon()
74 self.backend.draw_regular_polygon(cx, cy, 3, self.triangle_radius, theta, (0, 0, 0, 0.2))
75 polygon = self.backend.active_group.elements[-1]
76 polygon['id'] = "hexagon%d-triangle%d-folding" % (triangle.hexagon.index, triangle.index)
78 self.backend.active_group = old_active_group
80 # Draw the normal template for hexagons
81 for hexagon in self.hexaflexagon.hexagons:
82 self.draw_hexagon_template(hexagon)
84 # draw plan using references
85 for hexagon in self.hexaflexagon.hexagons:
86 for triangle in hexagon.triangles:
87 m = self.get_triangle_transform(triangle)
88 svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
92 # Reuse the hexagons triangle for the hexaflexagon template
93 group = self.groups["Template"]
94 triangle_href = "#hexagon%d-triangle%d" % (hexagon.index, triangle.index)
95 ref = self.backend.svg.use(triangle_href)
96 ref['transform'] = svg_matrix
99 # Reuse the folding guides
100 group = self.groups["Folding guide"]
101 folding_href = "#hexagon%d-triangle%d-folding" % (hexagon.index, triangle.index)
102 ref = self.backend.svg.use(folding_href)
103 ref['transform'] = svg_matrix
106 # Reuse the content to draw the final hexaflexagon
107 group = self.groups["Hexaflexagon"]
108 content_href = "#hexagon%d-content" % hexagon.index
109 ref = self.backend.svg.use(content_href)
110 ref['transform'] = svg_matrix
111 ref['clip-path'] = "url(%s)" % (triangle_href + '-clip-path')
115 group = self.groups["Backfaces"]
116 for hexagon in self.hexaflexagon.hexagons:
117 for triangle in hexagon.triangles:
118 m = self.get_triangle_backfaces_transform(triangle)
119 svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
123 triangle_href = "#hexagon%d-triangle%d" % (hexagon.index, triangle.index)
125 # Reuse the content to draw the backface
126 content_href = "#hexagon%d-content" % hexagon.index
127 ref = self.backend.svg.use(content_href)
128 ref['transform'] = svg_matrix
129 ref['clip-path'] = "url(%s)" % (triangle_href + '-clip-path')
132 def draw_triangle_template(self, triangle, cx, cy, theta):
133 old_active_group = self.backend.active_group
134 group_name = "hexagon%d-triangle%d" % (triangle.hexagon.index, triangle.index)
135 self.backend.active_group = self.groups[group_name]
137 super(SvgwriteHexaflexagonDiagram, self).draw_triangle_template(triangle, cx, cy, theta)
139 # The triangle outline in the active group's element is the only polygon
140 # element, so get it and set its id so that it can be reused as
142 for element in self.backend.active_group.elements:
143 if isinstance(element, svgwrite.shapes.Polygon):
144 element['id'] = group_name + "-outline"
147 clip_path = self.backend.svg.clipPath(id=group_name + '-clip-path')
148 self.backend.svg.defs.add(clip_path)
149 ref = self.backend.svg.use('#%s-outline' % group_name)
152 self.backend.active_group = old_active_group
159 x_border = width / 50
160 font_size = width / 80
161 stroke_width = width / 480
163 svg_backend = SvgwriteDiagram(width, height, font_size=font_size, stroke_width=stroke_width)
164 hexaflexagon = SvgwriteHexaflexagonDiagram(x_border, backend=svg_backend)
166 svg_backend.save_svg("inkscape-hexaflexagon-editor.svg")
169 if __name__ == "__main__":