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
35 def color_to_rgba(color):
36 assert len(color) >= 3
38 color = tuple(float(c) for c in color)
45 def normalized_angle_01(theta):
46 return fmod(theta, 2 * pi) / (2 * pi)
49 def get_regular_polygon(x, y, sides, r, theta0=0.0):
50 """Calc the coordinates of the regular polygon.
52 NOTE: the first point will be in axis with y."""
53 theta = 2 * pi / sides
56 for i in range(sides):
57 px = x + r * sin(theta0 + i * theta)
58 py = y + r * cos(theta0 + i * theta)
59 verts.append((px, py))
63 def draw_polygon_by_verts(self, verts,
64 stroke_color=(0, 0, 0),
66 raise NotImplementedError
68 def draw_regular_polygon(self, cx, cy, sides, r, theta=0.0,
69 stroke_color=(0, 0, 0),
71 verts = self.get_regular_polygon(cx, cy, sides, r, theta)
72 self.draw_polygon_by_verts(verts, stroke_color, fill_color)
75 def draw_star_by_verts(self, cx, cy, verts, stroke_color=(0, 0, 0)):
76 raise NotImplementedError
78 def draw_star(self, cx, cy, sides, r, theta=0.0, stroke_color=(0, 0, 0)):
79 verts = self.get_regular_polygon(cx, cy, sides, r, theta)
80 self.draw_star_by_verts(cx, cy, verts, stroke_color)
83 def draw_apothem_star(self, cx, cy, sides, r, theta=0.0, stroke_color=(0, 0, 0)):
84 """Draw a star but calculate the regular polygon apothem from the passed radius."""
85 apothem = r * cos(pi / sides)
86 apothem_angle = theta + pi / sides
88 return self.draw_star(cx, cy, sides, apothem, apothem_angle, stroke_color)
90 def draw_rect(self, x, y, width, height, theta=0,
92 fill_color=(1, 1, 1, 0.8)):
93 raise NotImplementedError
95 def draw_rect_from_center(self, cx, cy, width, height, theta=0.0,
97 fill_color=(1, 1, 1, 0.8)):
98 # the position of the center of a rectangle at (0,0)
102 # calculate the position of the bottom-left corner after rotating the
103 # rectangle around the center
104 rx = cx - (mx * cos(theta) - my * sin(theta))
105 ry = cy - (mx * sin(theta) + my * cos(theta))
107 self.draw_rect(rx, ry, width, height, theta, stroke_color, fill_color)