3333
3434
3535class Polygon (displayio .TileGrid ):
36- # pylint: disable=too-many-arguments,invalid-name
3736 """A polygon.
3837
3938 :param list points: A list of (x, y) tuples of the points
@@ -56,15 +55,7 @@ def __init__(
5655 close : Optional [bool ] = True ,
5756 colors : Optional [int ] = 2 ,
5857 ) -> 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 )
6859
6960 x_offset = min (xs )
7061 y_offset = min (ys )
@@ -77,25 +68,37 @@ def __init__(
7768 self ._palette .make_transparent (0 )
7869 self ._bitmap = displayio .Bitmap (width , height , colors + 1 )
7970
71+ shifted = [(x - x_offset , y - y_offset ) for (x , y ) in points ]
72+
8073 if outline is not None :
81- # print("outline")
8274 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 )
9376
9477 super ().__init__ (
9578 self ._bitmap , pixel_shader = self ._palette , x = x_offset , y = y_offset
9679 )
9780
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+
99102 def _line (
100103 self ,
101104 x0 : int ,
@@ -104,16 +107,27 @@ def _line(
104107 y1 : int ,
105108 color : int ,
106109 ) -> 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
107121 if x0 == x1 :
108122 if y0 > y1 :
109123 y0 , y1 = y1 , y0
110124 for _h in range (y0 , y1 + 1 ):
111- self . _bitmap [x0 , _h ] = color
125+ bitmap [x0 , _h ] = color
112126 elif y0 == y1 :
113127 if x0 > x1 :
114128 x0 , x1 = x1 , x0
115129 for _w in range (x0 , x1 + 1 ):
116- self . _bitmap [_w , y0 ] = color
130+ bitmap [_w , y0 ] = color
117131 else :
118132 steep = abs (y1 - y0 ) > abs (x1 - x0 )
119133 if steep :
@@ -136,16 +150,14 @@ def _line(
136150
137151 for x in range (x0 , x1 + 1 ):
138152 if steep :
139- self . _bitmap [y0 , x ] = color
153+ bitmap [y0 , x ] = color
140154 else :
141- self . _bitmap [x , y0 ] = color
155+ bitmap [x , y0 ] = color
142156 err -= dy
143157 if err < 0 :
144158 y0 += ystep
145159 err += dx
146160
147- # pylint: enable=invalid-name, too-many-locals, too-many-branches
148-
149161 @property
150162 def outline (self ) -> Optional [int ]:
151163 """The outline of the polygon. Can be a hex value for a color or
0 commit comments