README.md: mention also the tetraflexagon example
[flexagon-toolkit.git] / src / diagram / diagram.py
1 #!/usr/bin/env python
2 #
3 # A Diagram base class
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 from math import cos, sin, pi, fmod
21
22
23 class Diagram(object):
24     def __init__(self, width, height, background=(1, 1, 1), font_size=20, stroke_width=2):
25         self.width = width
26         self.height = height
27         self.background = background
28         self.font_size = font_size
29         self.stroke_width = stroke_width
30
31     def clear(self):
32         raise NotImplementedError
33
34     @staticmethod
35     def test(diagram):
36         diagram.clear()
37
38         x = 40
39         y = 200
40
41         x_offset = x
42
43         theta = 0
44
45         diagram.draw_line(0, y, 400, y, (1, 0, 0, 1))
46
47         advance = diagram.draw_centered_text(x_offset, y, "Ciao", theta,
48                                              align_baseline=True,
49                                              bb_stroke_color=(0, 0, 0, 0.5),
50                                              bb_fill_color=(1, 1, 1, 0.8))
51         x_offset += advance
52
53         advance = diagram.draw_centered_text(x_offset, y, "____", theta + pi / 4,
54                                              align_baseline=True,
55                                              bb_stroke_color=(0, 0, 0, 0.5),
56                                              bb_fill_color=(1, 1, 1, 0.8))
57         x_offset += advance
58
59         advance = diagram.draw_centered_text(x_offset, y, "jxpqdlf", theta + pi / 2,
60                                              align_baseline=True,
61                                              bb_stroke_color=(0, 0, 0, 0.5),
62                                              bb_fill_color=(1, 1, 1, 0.8))
63         x_offset += advance
64
65         advance = diagram.draw_centered_text(x_offset, y, "pppp", theta + 3 * pi / 4,
66                                              align_baseline=True,
67                                              bb_stroke_color=(0, 0, 0, 0.5),
68                                              bb_fill_color=(1, 1, 1, 0.8))
69         x_offset += advance
70
71         advance = diagram.draw_centered_text(x_offset, y, "dddd", theta + pi,
72                                              align_baseline=True,
73                                              bb_stroke_color=(0, 0, 0, 0.5),
74                                              bb_fill_color=(1, 1, 1, 0.8))
75         x_offset += advance
76
77         advance = diagram.draw_centered_text(x_offset, y, "Jjjj", theta + 5 * pi / 4,
78                                              align_baseline=True,
79                                              bb_stroke_color=(0, 0, 0, 0.5),
80                                              bb_fill_color=(1, 1, 1, 0.8))
81         x_offset += advance
82
83         advance = diagram.draw_centered_text(x_offset, y, "1369", theta + 3 * pi / 2,
84                                              color=(0, 1, 0),
85                                              align_baseline=True,
86                                              bb_stroke_color=(0, 0, 0, 0.5),
87                                              bb_fill_color=(1, 1, 1, 0.8))
88         x_offset += advance
89
90         advance = diagram.draw_centered_text(x_offset, y, "qqqq", theta + 7 * pi / 4,
91                                              align_baseline=True,
92                                              bb_stroke_color=(0, 0, 0, 0.5),
93                                              bb_fill_color=(1, 1, 1, 0.8))
94         x_offset += advance
95
96         diagram.draw_rect(40, 40, 300, 100, stroke_color=(0, 0, 0, 0.8))
97         diagram.draw_rect(40, 40, 300, 100, pi / 30, stroke_color=(0, 0, 0, 0.8))
98
99         verts = diagram.draw_regular_polygon(190, 90, 3, 20)
100
101         diagram.draw_rect(40, 250, 300, 100, stroke_color=(0, 0, 0, 0.8))
102         diagram.draw_rect_from_center(40 + 150, 250 + 50, 300, 100, theta=(pi / 40),
103                                       stroke_color=(1, 0, 0),
104                                       fill_color=None)
105
106         verts = diagram.draw_regular_polygon(190, 300, 6, 20, pi / 3., (0, 0, 1, 0.5), (0, 1, 0.5))
107         diagram.draw_apothem_star(190, 300, 6, 20, 0, (1, 0, 1))
108
109         diagram.draw_star_by_verts(190, 300, verts, (1, 0, 0, 0.5))
110         diagram.draw_star(190, 300, 6, 25, 0, (1, 0, 1, 0.2))
111
112         diagram.draw_circle(190, 300, 30, (0, 1, 0, 0.5), None)
113         diagram.draw_circle(100, 300, 30, (1, 0, 0, 0.5), (0, 1, 1, 0.5))
114
115     @staticmethod
116     def color_to_rgba(color):
117         assert len(color) >= 3
118
119         color = tuple(float(c) for c in color)
120         if len(color) == 3:
121             color += (1.0,)
122
123         return color
124
125     @staticmethod
126     def normalized_angle_01(theta):
127         return fmod(theta, 2 * pi) / (2 * pi)
128
129     @staticmethod
130     def calc_rotate_translate_transform(src_x, src_y, dest_x, dest_y, theta):
131         """Calculate the transformation matrix resulting from a rotation and
132         a translation.
133
134         Return the matrix as a list of values sorted in row-major order."""
135
136         # A rotate-translate transformation is composed by these steps:
137         #
138         #   1. rotate by 'theta' around (src_x, src_y);
139         #   2. move to (dest_x, dest_y).
140         #
141         # Step 1 can be expressed by these sub-steps:
142         #
143         #  1a. translate by (-src_x, -src_y)
144         #  1b. rotate by 'theta'
145         #  1c. translate by (src_x, src_y)
146         #
147         # Step 2. can be expressed by a translation like:
148         #
149         #  2a. translate by (dest_x - src_x, dest_y - src_y)
150         #
151         # The consecutive translations 1c and 2a can be easily combined, so
152         # the final steps are:
153         #
154         #  T1 -> translate by (-src_x, -src_y)
155         #  R  -> rotate by 'theta'
156         #  T2 -> translate by (dest_x, dest_y)
157         #
158         # Using affine transformations these are expressed as:
159         #
160         #      | 1  0  -src_x |
161         # T1 = | 0  1  -src_y |
162         #      | 0  0       1 |
163         #
164         #      | cos(theta)  -sin(theta)  0 |
165         # R  = | sin(theta)   cos(theta)  0 |
166         #      |          0            0  1 |
167         #
168         #      | 1  0  dest_x |
169         # T2 = | 0  1  dest_y |
170         #      | 0  0       1 |
171         #
172         # Composing these transformations into one is achieved by multiplying
173         # the matrices from right to left:
174         #
175         #   T = T2 * R * T1
176         #
177         # NOTE: To remember this think about composing functions: T2(R(T1())),
178         # the inner one is performed first.
179         #
180         # The resulting  T matrix is the one below.
181         matrix = [
182             cos(theta), -sin(theta), -src_x * cos(theta) + src_y * sin(theta) + dest_x,
183             sin(theta),  cos(theta), -src_x * sin(theta) - src_y * cos(theta) + dest_y,
184                      0,           0,                                                 1
185         ]
186
187         return matrix
188
189     @staticmethod
190     def get_regular_polygon(x, y, sides, r, theta0=0.0):
191         """Calc the coordinates of the regular polygon.
192
193         NOTE: the first point will be in axis with y."""
194         theta = 2 * pi / sides
195
196         verts = []
197         for i in range(sides):
198             px = x + r * sin(theta0 + i * theta)
199             py = y + r * cos(theta0 + i * theta)
200             verts.append((px, py))
201
202         return verts
203
204     def draw_polygon_by_verts(self, verts,
205                               stroke_color=(0, 0, 0),
206                               fill_color=None):
207         raise NotImplementedError
208
209     def draw_regular_polygon(self, cx, cy, sides, r, theta=0.0,
210                              stroke_color=(0, 0, 0),
211                              fill_color=None):
212         verts = self.get_regular_polygon(cx, cy, sides, r, theta)
213         self.draw_polygon_by_verts(verts, stroke_color, fill_color)
214         return verts
215
216     def draw_star_by_verts(self, cx, cy, verts, stroke_color=(0, 0, 0)):
217         raise NotImplementedError
218
219     def draw_star(self, cx, cy, sides, r, theta=0.0, stroke_color=(0, 0, 0)):
220         verts = self.get_regular_polygon(cx, cy, sides, r, theta)
221         self.draw_star_by_verts(cx, cy, verts, stroke_color)
222         return verts
223
224     def draw_apothem_star(self, cx, cy, sides, r, theta=0.0, stroke_color=(0, 0, 0)):
225         """Draw a star but calculate the regular polygon apothem from the passed radius."""
226         apothem = r * cos(pi / sides)
227         apothem_angle = theta + pi / sides
228
229         return self.draw_star(cx, cy, sides, apothem, apothem_angle, stroke_color)
230
231     def draw_rect(self, x, y, width, height, theta=0,
232                   stroke_color=None,
233                   fill_color=(1, 1, 1, 0.8)):
234         raise NotImplementedError
235
236     def draw_rect_from_center(self, cx, cy, width, height, theta=0.0,
237                               stroke_color=None,
238                               fill_color=(1, 1, 1, 0.8)):
239         # the position of the center of a rectangle at (0,0)
240         mx = width / 2.0
241         my = height / 2.0
242
243         # calculate the position of the bottom-left corner after rotating the
244         # rectangle around the center
245         rx = cx - (mx * cos(theta) - my * sin(theta))
246         ry = cy - (mx * sin(theta) + my * cos(theta))
247
248         self.draw_rect(rx, ry, width, height, theta, stroke_color, fill_color)