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 "Hexaflexagon": svg.layer(label="Hexaflexagon"),
36 "Folding guide": svg.layer(label="Folding guide"),
37 "Template": svg.layer(label="Template")
39 for layer in layers.values():
44 for hexagon in self.hexaflexagon.hexagons:
45 name = "hexagon%d-content" % hexagon.index
46 layer = svg.layer(id=name, label="Hexagon %d" % (hexagon.index + 1))
47 self.groups[name] = layer
48 layers['Hexagons'].add(layer)
50 for triangle in hexagon.triangles:
51 name = "hexagon%d-triangle%d" % (hexagon.index, triangle.index)
52 group = svg.g(id=name)
53 self.groups[name] = group
54 layers['Template'].add(group)
57 for hexagon in self.hexaflexagon.hexagons:
58 cx, cy = self.get_hexagon_center(hexagon)
60 # Draw some default content
61 old_active_group = self.backend.active_group
62 self.backend.active_group = self.groups["hexagon%d-content" % hexagon.index]
63 self.backend.draw_regular_polygon(cx, cy, 6, self.hexagon_radius, fill_color=(0.5, 0.5, 0.5, 0.2), stroke_color=None)
64 self.backend.active_group = old_active_group
67 old_active_group = self.backend.active_group
68 self.backend.active_group = self.groups["Folding guide"]
70 for triangle in hexagon.triangles:
71 cx, cy = self.get_triangle_center(triangle)
72 theta = triangle.get_angle_in_hexagon()
73 self.backend.draw_regular_polygon(cx, cy, 3, self.triangle_radius, theta, (0, 0, 0, 0.2))
74 polygon = self.backend.active_group.elements[-1]
75 polygon['id'] = "hexagon%d-triangle%d-folding" % (triangle.hexagon.index, triangle.index)
77 self.backend.active_group = old_active_group
79 # Draw the normal template for hexagons
80 for hexagon in self.hexaflexagon.hexagons:
81 self.draw_hexagon_template(hexagon)
83 # draw plan using references
84 for hexagon in self.hexaflexagon.hexagons:
85 for triangle in hexagon.triangles:
86 m = self.get_triangle_transform(triangle)
87 svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
91 # Reuse the hexagons triangle for the hexaflexagon template
92 group = self.groups["Template"]
93 triangle_href = "#hexagon%d-triangle%d" % (hexagon.index, triangle.index)
94 ref = self.backend.svg.use(triangle_href)
95 ref['transform'] = svg_matrix
98 # Reuse the folding guides
99 group = self.groups["Folding guide"]
100 folding_href = "#hexagon%d-triangle%d-folding" % (hexagon.index, triangle.index)
101 ref = self.backend.svg.use(folding_href)
102 ref['transform'] = svg_matrix
105 # Reuse the content to draw the final hexaflexagon
106 group = self.groups["Hexaflexagon"]
107 content_href = "#hexagon%d-content" % hexagon.index
108 ref = self.backend.svg.use(content_href)
109 ref['transform'] = svg_matrix
110 ref['clip-path'] = "url(%s)" % (triangle_href + '-clip-path')
113 def draw_triangle_template(self, triangle, cx, cy, theta):
114 old_active_group = self.backend.active_group
115 group_name = "hexagon%d-triangle%d" % (triangle.hexagon.index, triangle.index)
116 self.backend.active_group = self.groups[group_name]
118 super(SvgwriteHexaflexagonDiagram, self).draw_triangle_template(triangle, cx, cy, theta)
120 # The triangle outline in the active group's element is the only polygon
121 # element, so get it and set its id so that it can be reused as
123 for element in self.backend.active_group.elements:
124 if isinstance(element, svgwrite.shapes.Polygon):
125 element['id'] = group_name + "-outline"
128 clip_path = self.backend.svg.clipPath(id=group_name + '-clip-path')
129 self.backend.svg.defs.add(clip_path)
130 ref = self.backend.svg.use('#%s-outline' % group_name)
133 self.backend.active_group = old_active_group
140 x_border = width / 50
141 font_size = width / 80
142 stroke_width = width / 480
144 svg_backend = SvgwriteDiagram(width, height, font_size=font_size, stroke_width=stroke_width)
145 hexaflexagon = SvgwriteHexaflexagonDiagram(x_border, backend=svg_backend)
147 svg_backend.save_svg("inkscape-hexaflexagon-editor.svg")
150 if __name__ == "__main__":