33
33
34
34
35
35
class Polygon (displayio .TileGrid ):
36
- # pylint: disable=too-many-arguments,invalid-name
37
36
"""A polygon.
38
37
39
38
:param list points: A list of (x, y) tuples of the points
@@ -56,15 +55,7 @@ def __init__(
56
55
close : Optional [bool ] = True ,
57
56
colors : Optional [int ] = 2 ,
58
57
) -> None :
59
- if close :
60
- points .append (points [0 ])
61
-
62
- xs = []
63
- ys = []
64
-
65
- for point in points :
66
- xs .append (point [0 ])
67
- ys .append (point [1 ])
58
+ (xs , ys ) = zip (* points )
68
59
69
60
x_offset = min (xs )
70
61
y_offset = min (ys )
@@ -77,25 +68,37 @@ def __init__(
77
68
self ._palette .make_transparent (0 )
78
69
self ._bitmap = displayio .Bitmap (width , height , colors + 1 )
79
70
71
+ shifted = [(x - x_offset , y - y_offset ) for (x , y ) in points ]
72
+
80
73
if outline is not None :
81
- # print("outline")
82
74
self .outline = outline
83
- for index , _ in enumerate (points [:- 1 ]):
84
- point_a = points [index ]
85
- point_b = points [index + 1 ]
86
- self ._line (
87
- point_a [0 ] - x_offset ,
88
- point_a [1 ] - y_offset ,
89
- point_b [0 ] - x_offset ,
90
- point_b [1 ] - y_offset ,
91
- self ._OUTLINE ,
92
- )
75
+ self .draw (self ._bitmap , shifted , self ._OUTLINE , close )
93
76
94
77
super ().__init__ (
95
78
self ._bitmap , pixel_shader = self ._palette , x = x_offset , y = y_offset
96
79
)
97
80
98
- # pylint: disable=invalid-name, too-many-locals, too-many-branches
81
+ @staticmethod
82
+ def draw (
83
+ bitmap : displayio .Bitmap ,
84
+ points : List [Tuple [int , int ]],
85
+ color_id : int ,
86
+ close : Optional [bool ] = True ,
87
+ ) -> None :
88
+ """Draw a polygon conecting points on provided bitmap with provided color_id
89
+
90
+ :param displayio.Bitmap bitmap: bitmap to draw on
91
+ :param list points: A list of (x, y) tuples of the points
92
+ :param int color_id: Color to draw with
93
+ :param bool close: (Optional) Wether to connect first and last point. (True)
94
+ """
95
+
96
+ if close :
97
+ points .append (points [0 ])
98
+
99
+ for index in range (len (points ) - 1 ):
100
+ Polygon ._line_on (bitmap , points [index ], points [index + 1 ], color_id )
101
+
99
102
def _line (
100
103
self ,
101
104
x0 : int ,
@@ -104,16 +107,27 @@ def _line(
104
107
y1 : int ,
105
108
color : int ,
106
109
) -> None :
110
+ self ._line_on (self ._bitmap , (x0 , y0 ), (x1 , y1 ), color )
111
+
112
+ @staticmethod
113
+ def _line_on (
114
+ bitmap : displayio .Bitmap ,
115
+ p0 : Tuple [int , int ],
116
+ p1 : Tuple [int , int ],
117
+ color : int ,
118
+ ) -> None :
119
+ (x0 , y0 ) = p0
120
+ (x1 , y1 ) = p1
107
121
if x0 == x1 :
108
122
if y0 > y1 :
109
123
y0 , y1 = y1 , y0
110
124
for _h in range (y0 , y1 + 1 ):
111
- self . _bitmap [x0 , _h ] = color
125
+ bitmap [x0 , _h ] = color
112
126
elif y0 == y1 :
113
127
if x0 > x1 :
114
128
x0 , x1 = x1 , x0
115
129
for _w in range (x0 , x1 + 1 ):
116
- self . _bitmap [_w , y0 ] = color
130
+ bitmap [_w , y0 ] = color
117
131
else :
118
132
steep = abs (y1 - y0 ) > abs (x1 - x0 )
119
133
if steep :
@@ -136,16 +150,14 @@ def _line(
136
150
137
151
for x in range (x0 , x1 + 1 ):
138
152
if steep :
139
- self . _bitmap [y0 , x ] = color
153
+ bitmap [y0 , x ] = color
140
154
else :
141
- self . _bitmap [x , y0 ] = color
155
+ bitmap [x , y0 ] = color
142
156
err -= dy
143
157
if err < 0 :
144
158
y0 += ystep
145
159
err += dx
146
160
147
- # pylint: enable=invalid-name, too-many-locals, too-many-branches
148
-
149
161
@property
150
162
def outline (self ) -> Optional [int ]:
151
163
"""The outline of the polygon. Can be a hex value for a color or
0 commit comments