-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpolar.py
More file actions
156 lines (131 loc) · 4.59 KB
/
polar.py
File metadata and controls
156 lines (131 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
#
# SPDX-License-Identifier: MIT
"""
`polar`
================================================================================
CircuitPython Polar graph
* Author(s): Jose D. Montoya
"""
try:
from typing import Optional, Union
from circuitpython_uplot.plot import Plot
except ImportError:
pass
from bitmaptools import draw_line, draw_circle
from ulab import numpy as np
__version__ = "0.11.0"
__repo__ = "https://github.com/adafruit/CircuitPython_uplot.git"
# pylint: disable=dangerous-default-value
class Polar:
"""
Class to draw polar plots
"""
def __init__(
self,
plot: Plot,
radius: Union[list, np.linspace, np.ndarray],
theta: Union[list, np.linspace, np.ndarray],
rangex: Optional[list] = None,
rangey: Optional[list] = None,
line_color: Optional[int] = None,
polar_plot_line_color: int = 0x647182,
nudge: bool = True,
radius_ticks: list = [1.0, 2.0],
) -> None:
"""
:param Plot plot: Plot object for the scatter to be drawn
:param list|ulab.numpy.linspace|ulab.numpy.ndarray radius: radius points
:param list|ulab.numpy.linspace|ulab.numpy.ndarray theta: theta points
:param list|None rangex: x range limits. Defaults to None
:param list|None rangey: y range limits. Defaults to None
:param int|None line_color: line color. Defaults to None
:param bool fill: Show the filling. Defaults to `False`
:param bool nudge: moves the graph a little for better displaying. Defaults to `True`
"""
angles_list = [45, 90, 135, 180, 225, 270, 315, 360]
angles = np.ndarray(angles_list, dtype=np.int16)
self.angle_radians = np.radians(angles)
self._color_index = plot._index_colorused + 1
if line_color is not None:
plot._plot_palette[plot._index_colorused] = line_color
plot._plot_palette[self._color_index] = polar_plot_line_color
if nudge:
nudge_factor = 1
else:
nudge_factor = 0
if rangex is None:
xmin = np.min(radius) - nudge_factor * (
abs(np.max(radius) - np.min(radius)) / 10
)
xmax = np.max(radius) + nudge_factor * (
abs(np.max(radius) - np.min(radius)) / 10
)
else:
xmin = min(rangex)
xmax = max(rangex)
if rangey is None:
ymin = np.min(theta) - nudge_factor * (
abs(np.max(theta) - np.min(theta)) / 10
)
ymax = np.max(theta) + nudge_factor * (
abs(np.max(theta) - np.min(theta)) / 10
)
else:
ymin = min(rangey)
ymax = max(rangey)
xnorm = np.array(
plot.transform(
xmin,
xmax,
plot._newxmin,
plot._newxmax,
np.cos(np.array(radius)) * np.array(theta),
),
dtype=np.int16,
)
ynorm = np.array(
plot.transform(
ymin,
ymax,
plot._newymin,
plot._newymax,
np.sin(np.array(radius)) * np.array(theta),
),
dtype=np.int16,
)
rnorm = np.array(
plot.transform(
0, xmax, plot._newxmin, plot._newxmax, np.array(radius_ticks)
),
dtype=np.int16,
)
self.originx = int(plot.transform(xmin, xmax, plot._newxmin, plot._newxmax, 0))
self.originy = int(plot.transform(ymin, ymax, plot._newymin, plot._newymax, 0))
for index, _ in enumerate(xnorm):
if index + 1 >= len(xnorm):
break
if theta[index] >= ymax:
continue
draw_line(
plot._plotbitmap,
xnorm[index],
ynorm[index],
xnorm[index + 1],
ynorm[index + 1],
plot._index_colorused,
)
for radius_norm in rnorm:
draw_circle(
plot._plotbitmap,
self.originx,
self.originy,
radius_norm // 2,
self._color_index,
)
for element in self.angle_radians:
x = self.originx + int(np.cos(element) * rnorm[-1] // 2)
y = self.originy + int(np.sin(element) * rnorm[-1] // 2)
draw_line(
plot._plotbitmap, self.originx, self.originy, x, y, self._color_index
)