2626except ImportError :
2727 pass
2828
29+ import bitmaptools
2930import displayio
3031
3132__version__ = "0.0.0+auto.0"
@@ -42,6 +43,7 @@ class Polygon(displayio.TileGrid):
4243 :param int colors: (Optional) Number of colors to use. Most polygons would use two, one for
4344 outline and one for fill. If you're not filling your polygon, set this to 1
4445 for smaller memory footprint. (2)
46+ :param int stroke: Thickness of the outline.
4547 """
4648
4749 _OUTLINE = 1
@@ -54,6 +56,8 @@ def __init__(
5456 outline : Optional [int ] = None ,
5557 close : Optional [bool ] = True ,
5658 colors : Optional [int ] = 2 ,
59+ stroke : int = 1 ,
60+ # pylint: disable=too-many-arguments
5761 ) -> None :
5862 (x_s , y_s ) = zip (* points )
5963
@@ -66,13 +70,14 @@ def __init__(
6670
6771 self ._palette = displayio .Palette (colors + 1 )
6872 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
7075
7176 shifted = [(x - x_offset , y - y_offset ) for (x , y ) in points ]
7277
7378 if outline is not None :
7479 self .outline = outline
75- self .draw (self ._bitmap , shifted , self ._OUTLINE , close )
80+ self .draw (self ._bitmap , shifted , self ._OUTLINE , close , stroke )
7681
7782 super ().__init__ (
7883 self ._bitmap , pixel_shader = self ._palette , x = x_offset , y = y_offset
@@ -84,6 +89,7 @@ def draw(
8489 points : List [Tuple [int , int ]],
8590 color_id : int ,
8691 close : Optional [bool ] = True ,
92+ stroke = 1 ,
8793 ) -> None :
8894 """Draw a polygon conecting points on provided bitmap with provided color_id
8995
@@ -97,7 +103,7 @@ def draw(
97103 points .append (points [0 ])
98104
99105 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 )
101107
102108 # pylint: disable=too-many-arguments
103109 def _line (
@@ -129,23 +135,36 @@ def _line_on(
129135 p_0 : Tuple [int , int ],
130136 p_1 : Tuple [int , int ],
131137 color : int ,
138+ stroke : int = 1 ,
132139 ) -> None :
133140 (x_0 , y_0 ) = p_0
134141 (x_1 , y_1 ) = p_1
135142
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 )
138157
139158 if x_0 == x_1 :
140159 if y_0 > y_1 :
141160 y_0 , y_1 = y_1 , y_0
142161 for _h in range (y_0 , y_1 + 1 ):
143- pt_on (x_0 , _h )
162+ pt_on (x_0 , _h , stroke )
144163 elif y_0 == y_1 :
145164 if x_0 > x_1 :
146165 x_0 , x_1 = x_1 , x_0
147166 for _w in range (x_0 , x_1 + 1 ):
148- pt_on (_w , y_0 )
167+ pt_on (_w , y_0 , stroke )
149168 else :
150169 steep = abs (y_1 - y_0 ) > abs (x_1 - x_0 )
151170 if steep :
@@ -168,9 +187,9 @@ def pt_on(x, y):
168187
169188 for x in range (x_0 , x_1 + 1 ):
170189 if steep :
171- pt_on (y_0 , x )
190+ pt_on (y_0 , x , stroke )
172191 else :
173- pt_on (x , y_0 )
192+ pt_on (x , y_0 , stroke )
174193 err -= d_y
175194 if err < 0 :
176195 y_0 += ystep
0 commit comments