1+ from renderer .renderer import IRenderer
2+ from math import sin , cos , pi , degrees
3+
4+ class Math :
5+ @staticmethod
6+ def math_rounding (value : float ) -> int :
7+ """Округление по правилам математики"""
8+ return int (value )+ 1 if value % 1 > 0.5 else int (value )
9+
10+ class Calc :
11+ def __init__ (
12+ self ,
13+ outer_radius : int ,
14+ thickness : int ,
15+ percent_array : list [float ],
16+ sections_colors_array : list [str ],
17+ names_array : list [str ],
18+ renderer : IRenderer ,
19+ margin_x : int = 0 ,
20+ margin_y : int = 0 ,
21+ ) -> None :
22+ self ._thickness = thickness
23+ self ._outer_radius = outer_radius
24+ self ._renderer = renderer
25+ self ._margin_x = margin_x
26+ self ._margin_y = margin_y
27+ self ._percent_array = percent_array
28+ self ._sections_colors_array = sections_colors_array
29+ self ._inner_radius = outer_radius - thickness
30+ self ._names_array = names_array
31+
32+ def __call__ (self ) -> str :
33+ angle_accumulator = 0 # sum of all previous angles
34+
35+ next_outer_curve_start_x = self ._margin_x + self ._outer_radius # x-axis position of end of previous outer curve / start of new outer curve
36+ next_outer_curve_start_y = self ._margin_y # y-axis position of end of previous outer curve / start of new outer curve
37+
38+ next_inner_curve_start_x = self ._margin_x + self ._outer_radius # x-axis position of end of previous inner curve / start of new inner curve
39+ next_inner_curve_start_y = self ._margin_y + self ._thickness # y-axis position of end of previous inner curve / start of new inner curve
40+
41+ for percent ,color ,name in zip (self ._percent_array , self ._sections_colors_array , self ._names_array ):
42+ current_angle = 2 * pi * percent + angle_accumulator
43+
44+ outer_x = self ._margin_x + self ._outer_radius * (1 + sin (current_angle ))
45+ outer_y = self ._margin_y + self ._outer_radius * (1 - cos (current_angle ))
46+ inner_x = self ._margin_x + self ._thickness + self ._inner_radius * (1 + sin (current_angle ))
47+ inner_y = self ._margin_y + self ._thickness + self ._inner_radius * (1 - cos (current_angle ))
48+
49+ self ._renderer .add_section (
50+ (Math .math_rounding (next_outer_curve_start_x ), Math .math_rounding (next_outer_curve_start_y )),
51+ (Math .math_rounding (outer_x ), Math .math_rounding (outer_y )),
52+ (Math .math_rounding (inner_x ), Math .math_rounding (inner_y )),
53+ (Math .math_rounding (next_inner_curve_start_x ), Math .math_rounding (next_inner_curve_start_y )),
54+ section_start_angle = degrees (angle_accumulator ),
55+ section_finish_angle = degrees (current_angle ),
56+ color = color ,
57+ name = name
58+ )
59+
60+ # Updating all values
61+ angle_accumulator += 2 * pi * percent
62+ next_outer_curve_start_x = outer_x
63+ next_outer_curve_start_y = outer_y
64+ next_inner_curve_start_x = inner_x
65+ next_inner_curve_start_y = inner_y
66+
67+ return self ._renderer .render ()
68+
0 commit comments