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/>.
20 from math import cos, sin, pi, fmod
23 class Diagram(object):
24 def __init__(self, width, height, background=(1, 1, 1), font_size=20, stroke_width=2):
27 self.background = background
28 self.font_size = font_size
29 self.stroke_width = stroke_width
32 raise NotImplementedError
45 diagram.draw_line(0, y, 400, y, (1, 0, 0, 1))
47 advance = diagram.draw_centered_text(x_offset, y, "Ciao", theta,
49 bb_stroke_color=(0, 0, 0, 0.5),
50 bb_fill_color=(1, 1, 1, 0.8))
53 advance = diagram.draw_centered_text(x_offset, y, "____", theta + pi / 4,
55 bb_stroke_color=(0, 0, 0, 0.5),
56 bb_fill_color=(1, 1, 1, 0.8))
59 advance = diagram.draw_centered_text(x_offset, y, "jxpqdlf", theta + pi / 2,
61 bb_stroke_color=(0, 0, 0, 0.5),
62 bb_fill_color=(1, 1, 1, 0.8))
65 advance = diagram.draw_centered_text(x_offset, y, "pppp", theta + 3 * pi / 4,
67 bb_stroke_color=(0, 0, 0, 0.5),
68 bb_fill_color=(1, 1, 1, 0.8))
71 advance = diagram.draw_centered_text(x_offset, y, "dddd", theta + pi,
73 bb_stroke_color=(0, 0, 0, 0.5),
74 bb_fill_color=(1, 1, 1, 0.8))
77 advance = diagram.draw_centered_text(x_offset, y, "Jjjj", theta + 5 * pi / 4,
79 bb_stroke_color=(0, 0, 0, 0.5),
80 bb_fill_color=(1, 1, 1, 0.8))
83 advance = diagram.draw_centered_text(x_offset, y, "1369", theta + 3 * pi / 2,
86 bb_stroke_color=(0, 0, 0, 0.5),
87 bb_fill_color=(1, 1, 1, 0.8))
90 advance = diagram.draw_centered_text(x_offset, y, "qqqq", theta + 7 * pi / 4,
92 bb_stroke_color=(0, 0, 0, 0.5),
93 bb_fill_color=(1, 1, 1, 0.8))
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))
99 verts = diagram.draw_regular_polygon(190, 90, 3, 20)
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),
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))
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))
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))
116 def color_to_rgba(color):
117 assert len(color) >= 3
119 color = tuple(float(c) for c in color)
126 def normalized_angle_01(theta):
127 return fmod(theta, 2 * pi) / (2 * pi)
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
134 Return the matrix as a list of values sorted in row-major order."""
136 # A rotate-translate transformation is composed by these steps:
138 # 1. rotate by 'theta' around (src_x, src_y);
139 # 2. move to (dest_x, dest_y).
141 # Step 1 can be expressed by these sub-steps:
143 # 1a. translate by (-src_x, -src_y)
144 # 1b. rotate by 'theta'
145 # 1c. translate by (src_x, src_y)
147 # Step 2. can be expressed by a translation like:
149 # 2a. translate by (dest_x - src_x, dest_y - src_y)
151 # The consecutive translations 1c and 2a can be easily combined, so
152 # the final steps are:
154 # T1 -> translate by (-src_x, -src_y)
155 # R -> rotate by 'theta'
156 # T2 -> translate by (dest_x, dest_y)
158 # Using affine transformations these are expressed as:
161 # T1 = | 0 1 -src_y |
164 # | cos(theta) -sin(theta) 0 |
165 # R = | sin(theta) cos(theta) 0 |
169 # T2 = | 0 1 dest_y |
172 # Composing these transformations into one is achieved by multiplying
173 # the matrices from right to left:
177 # NOTE: To remember this think about composing functions: T2(R(T1())),
178 # the inner one is performed first.
180 # The resulting T matrix is the one below.
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,
190 def get_regular_polygon(x, y, sides, r, theta0=0.0):
191 """Calc the coordinates of the regular polygon.
193 NOTE: the first point will be in axis with y."""
194 theta = 2 * pi / sides
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))
204 def draw_polygon_by_verts(self, verts,
205 stroke_color=(0, 0, 0),
207 raise NotImplementedError
209 def draw_regular_polygon(self, cx, cy, sides, r, theta=0.0,
210 stroke_color=(0, 0, 0),
212 verts = self.get_regular_polygon(cx, cy, sides, r, theta)
213 self.draw_polygon_by_verts(verts, stroke_color, fill_color)
216 def draw_star_by_verts(self, cx, cy, verts, stroke_color=(0, 0, 0)):
217 raise NotImplementedError
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)
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
229 return self.draw_star(cx, cy, sides, apothem, apothem_angle, stroke_color)
231 def draw_rect(self, x, y, width, height, theta=0,
233 fill_color=(1, 1, 1, 0.8)):
234 raise NotImplementedError
236 def draw_rect_from_center(self, cx, cy, width, height, theta=0.0,
238 fill_color=(1, 1, 1, 0.8)):
239 # the position of the center of a rectangle at (0,0)
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))
248 self.draw_rect(rx, ry, width, height, theta, stroke_color, fill_color)