26
26
except ImportError :
27
27
pass
28
28
29
+ import bitmaptools
29
30
import displayio
30
31
31
32
__version__ = "0.0.0+auto.0"
@@ -42,6 +43,7 @@ class Polygon(displayio.TileGrid):
42
43
:param int colors: (Optional) Number of colors to use. Most polygons would use two, one for
43
44
outline and one for fill. If you're not filling your polygon, set this to 1
44
45
for smaller memory footprint. (2)
46
+ :param int stroke: Thickness of the outline.
45
47
"""
46
48
47
49
_OUTLINE = 1
@@ -54,6 +56,8 @@ def __init__(
54
56
outline : Optional [int ] = None ,
55
57
close : Optional [bool ] = True ,
56
58
colors : Optional [int ] = 2 ,
59
+ stroke : int = 1 ,
60
+ # pylint: disable=too-many-arguments
57
61
) -> None :
58
62
(x_s , y_s ) = zip (* points )
59
63
@@ -66,13 +70,14 @@ def __init__(
66
70
67
71
self ._palette = displayio .Palette (colors + 1 )
68
72
self ._palette .make_transparent (0 )
69
- self ._bitmap = displayio .Bitmap (width , height , colors + 1 )
73
+ self ._bitmap = displayio .Bitmap (width + stroke , height + stroke , colors + 1 )
74
+ self ._stroke = stroke
70
75
71
76
shifted = [(x - x_offset , y - y_offset ) for (x , y ) in points ]
72
77
73
78
if outline is not None :
74
79
self .outline = outline
75
- self .draw (self ._bitmap , shifted , self ._OUTLINE , close )
80
+ self .draw (self ._bitmap , shifted , self ._OUTLINE , close , stroke )
76
81
77
82
super ().__init__ (
78
83
self ._bitmap , pixel_shader = self ._palette , x = x_offset , y = y_offset
@@ -84,6 +89,7 @@ def draw(
84
89
points : List [Tuple [int , int ]],
85
90
color_id : int ,
86
91
close : Optional [bool ] = True ,
92
+ stroke = 1 ,
87
93
) -> None :
88
94
"""Draw a polygon conecting points on provided bitmap with provided color_id
89
95
@@ -97,7 +103,7 @@ def draw(
97
103
points .append (points [0 ])
98
104
99
105
for index in range (len (points ) - 1 ):
100
- Polygon ._line_on (bitmap , points [index ], points [index + 1 ], color_id )
106
+ Polygon ._line_on (bitmap , points [index ], points [index + 1 ], color_id , stroke )
101
107
102
108
# pylint: disable=too-many-arguments
103
109
def _line (
@@ -129,23 +135,36 @@ def _line_on(
129
135
p_0 : Tuple [int , int ],
130
136
p_1 : Tuple [int , int ],
131
137
color : int ,
138
+ stroke : int = 1 ,
132
139
) -> None :
133
140
(x_0 , y_0 ) = p_0
134
141
(x_1 , y_1 ) = p_1
135
142
136
- def pt_on (x , y ):
137
- Polygon ._safe_draw (bitmap , (x , y ), color )
143
+ def pt_on (x , y , pt_size = 1 ):
144
+ if pt_size > 1 :
145
+ x = x + pt_size // 2
146
+ y = y + pt_size // 2
147
+ bitmaptools .fill_region (
148
+ bitmap ,
149
+ x - (pt_size // 2 ),
150
+ y - (pt_size // 2 ),
151
+ x + (pt_size // 2 ),
152
+ y + (pt_size // 2 ),
153
+ color ,
154
+ )
155
+ else :
156
+ Polygon ._safe_draw (bitmap , (x , y ), color )
138
157
139
158
if x_0 == x_1 :
140
159
if y_0 > y_1 :
141
160
y_0 , y_1 = y_1 , y_0
142
161
for _h in range (y_0 , y_1 + 1 ):
143
- pt_on (x_0 , _h )
162
+ pt_on (x_0 , _h , stroke )
144
163
elif y_0 == y_1 :
145
164
if x_0 > x_1 :
146
165
x_0 , x_1 = x_1 , x_0
147
166
for _w in range (x_0 , x_1 + 1 ):
148
- pt_on (_w , y_0 )
167
+ pt_on (_w , y_0 , stroke )
149
168
else :
150
169
steep = abs (y_1 - y_0 ) > abs (x_1 - x_0 )
151
170
if steep :
@@ -168,9 +187,9 @@ def pt_on(x, y):
168
187
169
188
for x in range (x_0 , x_1 + 1 ):
170
189
if steep :
171
- pt_on (y_0 , x )
190
+ pt_on (y_0 , x , stroke )
172
191
else :
173
- pt_on (x , y_0 )
192
+ pt_on (x , y_0 , stroke )
174
193
err -= d_y
175
194
if err < 0 :
176
195
y_0 += ystep
0 commit comments