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 get_regular_polygon(x, y, sides, r, theta0=0.0):
131 """Calc the coordinates of the regular polygon.
133 NOTE: the first point will be in axis with y."""
134 theta = 2 * pi / sides
137 for i in range(sides):
138 px = x + r * sin(theta0 + i * theta)
139 py = y + r * cos(theta0 + i * theta)
140 verts.append((px, py))
144 def draw_polygon_by_verts(self, verts,
145 stroke_color=(0, 0, 0),
147 raise NotImplementedError
149 def draw_regular_polygon(self, cx, cy, sides, r, theta=0.0,
150 stroke_color=(0, 0, 0),
152 verts = self.get_regular_polygon(cx, cy, sides, r, theta)
153 self.draw_polygon_by_verts(verts, stroke_color, fill_color)
156 def draw_star_by_verts(self, cx, cy, verts, stroke_color=(0, 0, 0)):
157 raise NotImplementedError
159 def draw_star(self, cx, cy, sides, r, theta=0.0, stroke_color=(0, 0, 0)):
160 verts = self.get_regular_polygon(cx, cy, sides, r, theta)
161 self.draw_star_by_verts(cx, cy, verts, stroke_color)
164 def draw_apothem_star(self, cx, cy, sides, r, theta=0.0, stroke_color=(0, 0, 0)):
165 """Draw a star but calculate the regular polygon apothem from the passed radius."""
166 apothem = r * cos(pi / sides)
167 apothem_angle = theta + pi / sides
169 return self.draw_star(cx, cy, sides, apothem, apothem_angle, stroke_color)
171 def draw_rect(self, x, y, width, height, theta=0,
173 fill_color=(1, 1, 1, 0.8)):
174 raise NotImplementedError
176 def draw_rect_from_center(self, cx, cy, width, height, theta=0.0,
178 fill_color=(1, 1, 1, 0.8)):
179 # the position of the center of a rectangle at (0,0)
183 # calculate the position of the bottom-left corner after rotating the
184 # rectangle around the center
185 rx = cx - (mx * cos(theta) - my * sin(theta))
186 ry = cy - (mx * sin(theta) + my * cos(theta))
188 self.draw_rect(rx, ry, width, height, theta, stroke_color, fill_color)