README.md: mention also the tetraflexagon example
[flexagon-toolkit.git] / src / svg_hexaflexagon_editor.py
1 #!/usr/bin/env python3
2 #
3 # Draw an SVG hexaflexagon which can be edited live in Inkscape.
4 #
5 # Copyright (C) 2018  Antonio Ospite <ao2@ao2.it>
6 #
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.
11 #
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.
16 #
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/>.
19
20 import svgwrite
21
22 from diagram.svgwrite_diagram import SvgwriteDiagram
23 from flexagon.hexaflexagon_diagram import HexaflexagonDiagram
24
25
26 class SvgwriteHexaflexagonDiagram(HexaflexagonDiagram):
27     def __init__(self, *args, **kwargs):
28         super(SvgwriteHexaflexagonDiagram, self).__init__(*args, **kwargs)
29
30         svg = self.backend.svg
31
32         # create some layers and groups
33         layers = {
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")
39         }
40         for layer in layers.values():
41             svg.add(layer)
42
43         self.groups = layers
44
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)
50
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)
56
57     def draw(self):
58         for hexagon in self.hexaflexagon.hexagons:
59             cx, cy = self.get_hexagon_center(hexagon)
60
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,
65                                               stroke_color=None,
66                                               fill_color=(0.5, 0.5, 0.5, 0.2))
67             self.backend.active_group = old_active_group
68
69             # Add folding guides
70             old_active_group = self.backend.active_group
71             self.backend.active_group = self.groups["Folding guide"]
72
73             for triangle in hexagon.triangles:
74                 cx, cy = self.get_triangle_center(triangle)
75                 theta = triangle.get_angle_in_hexagon()
76                 self.backend.draw_regular_polygon(cx, cy, 3, self.triangle_radius, theta,
77                                                   stroke_color=(0, 0, 0, 0.2),
78                                                   fill_color=None)
79                 polygon = self.backend.active_group.elements[-1]
80                 polygon['id'] = "hexagon%d-triangle%d-folding" % (triangle.hexagon.index,
81                                                                   triangle.index)
82
83             self.backend.active_group = old_active_group
84
85         # Draw the normal template for hexagons
86         for hexagon in self.hexaflexagon.hexagons:
87             self.draw_hexagon_template(hexagon)
88
89         # draw plan using references
90         for hexagon in self.hexaflexagon.hexagons:
91             for triangle in hexagon.triangles:
92                 m = self.get_triangle_transform(triangle)
93                 svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
94                                                                  m[1], m[4],
95                                                                  m[2], m[5])
96
97                 # Reuse the hexagons triangle for the hexaflexagon template
98                 group = self.groups["Template"]
99                 triangle_href = "#hexagon%d-triangle%d" % (hexagon.index, triangle.index)
100                 ref = self.backend.svg.use(triangle_href)
101                 ref['transform'] = svg_matrix
102                 group.add(ref)
103
104                 # Reuse the folding guides
105                 group = self.groups["Folding guide"]
106                 folding_href = "#hexagon%d-triangle%d-folding" % (hexagon.index, triangle.index)
107                 ref = self.backend.svg.use(folding_href)
108                 ref['transform'] = svg_matrix
109                 group.add(ref)
110
111                 # Reuse the content to draw the final hexaflexagon
112                 group = self.groups["Hexaflexagon"]
113                 content_href = "#hexagon%d-content" % hexagon.index
114                 ref = self.backend.svg.use(content_href)
115                 ref['transform'] = svg_matrix
116                 ref['clip-path'] = "url(%s)" % (triangle_href + '-clip-path')
117                 group.add(ref)
118
119         # draw the backfaces
120         group = self.groups["Backfaces"]
121         for hexagon in self.hexaflexagon.hexagons:
122             for triangle in hexagon.triangles:
123                 m = self.get_triangle_backfaces_transform(triangle)
124                 svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
125                                                                  m[1], m[4],
126                                                                  m[2], m[5])
127
128                 triangle_href = "#hexagon%d-triangle%d" % (hexagon.index, triangle.index)
129
130                 # Reuse the content to draw the backface
131                 content_href = "#hexagon%d-content" % hexagon.index
132                 ref = self.backend.svg.use(content_href)
133                 ref['transform'] = svg_matrix
134                 ref['clip-path'] = "url(%s)" % (triangle_href + '-clip-path')
135                 group.add(ref)
136
137     def draw_triangle_template(self, triangle, cx, cy, theta):
138         old_active_group = self.backend.active_group
139         group_name = "hexagon%d-triangle%d" % (triangle.hexagon.index, triangle.index)
140         self.backend.active_group = self.groups[group_name]
141
142         super(SvgwriteHexaflexagonDiagram, self).draw_triangle_template(triangle, cx, cy, theta)
143
144         # The triangle outline in the active group's element is the only polygon
145         # element, so get it and set its id so that it can be reused as
146         # a clip-path
147         for element in self.backend.active_group.elements:
148             if isinstance(element, svgwrite.shapes.Polygon):
149                 element['id'] = group_name + "-outline"
150                 break
151
152         clip_path = self.backend.svg.clipPath(id=group_name + '-clip-path')
153         self.backend.svg.defs.add(clip_path)
154         ref = self.backend.svg.use('#%s-outline' % group_name)
155         clip_path.add(ref)
156
157         self.backend.active_group = old_active_group
158
159
160 def main():
161     width = 3508
162     height = 2480
163
164     x_border = width / 50
165     font_size = width / 80
166     stroke_width = width / 480
167
168     svg_backend = SvgwriteDiagram(width, height, font_size=font_size, stroke_width=stroke_width)
169     hexaflexagon = SvgwriteHexaflexagonDiagram(x_border, backend=svg_backend)
170     hexaflexagon.draw()
171     svg_backend.save_svg("inkscape-hexaflexagon-editor.svg")
172
173
174 if __name__ == "__main__":
175     main()