3838
3939"""
4040
41- import displayio
41+ from adafruit_display_shapes . polygon import Polygon
4242
4343__version__ = "0.0.0-auto.0"
4444__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Shapes.git"
4545
4646
47- class Triangle (displayio . TileGrid ):
47+ class Triangle (Polygon ):
4848 # pylint: disable=too-many-arguments,invalid-name
4949 """A triangle.
5050
@@ -59,6 +59,7 @@ class Triangle(displayio.TileGrid):
5959 :param outline: The outline of the triangle. Can be a hex value for a color or
6060 ``None`` for no outline.
6161 """
62+ # pylint: disable=too-many-locals
6263 def __init__ (self , x0 , y0 , x1 , y1 , x2 , y2 , * , fill = None , outline = None ):
6364 # Sort coordinates by Y order (y2 >= y1 >= y0)
6465 if y0 > y1 :
@@ -75,29 +76,29 @@ def __init__(self, x0, y0, x1, y1, x2, y2, *, fill=None, outline=None):
7576
7677 # Find the largest and smallest X values to figure out width for bitmap
7778 xs = [x0 , x1 , x2 ]
78- width = max (xs ) - min (xs ) + 1
79- height = y2 - y0 + 1
79+ points = [(x0 , y0 ), (x1 , y1 ), (x2 , y2 )]
8080
81- self ._palette = displayio .Palette (3 )
82- self ._palette .make_transparent (0 )
83- self ._bitmap = displayio .Bitmap (width , height , 3 )
81+ # Initialize the bitmap and palette
82+ super ().__init__ (points )
8483
8584 if fill is not None :
8685 self ._draw_filled (x0 - min (xs ), 0 , x1 - min (xs ), y1 - y0 , x2 - min (xs ), y2 - y0 )
87- self ._palette [ 2 ] = fill
86+ self .fill = fill
8887 else :
89- self ._palette . make_transparent ( 2 )
88+ self .fill = None
9089
9190 if outline is not None :
92- # print("outline")
93- self ._palette [1 ] = outline
94- self ._line (x0 - min (xs ), 0 , x1 - min (xs ), y1 - y0 , 1 )
95- self ._line (x1 - min (xs ), y1 - y0 , x2 - min (xs ), y2 - y0 , 1 )
96- self ._line (x2 - min (xs ), y2 - y0 , x0 - min (xs ), 0 , 1 )
97-
98- super ().__init__ (self ._bitmap , pixel_shader = self ._palette , x = min (xs ), y = y0 )
91+ self .outline = outline
92+ for index , _ in enumerate (points ):
93+ point_a = points [index ]
94+ if index == len (points ) - 1 :
95+ point_b = points [0 ]
96+ else :
97+ point_b = points [index + 1 ]
98+ self ._line (point_a [0 ] - min (xs ), point_a [1 ] - y0 ,
99+ point_b [0 ] - min (xs ), point_b [1 ] - y0 , 1 )
99100
100- # pylint: disable=invalid-name, too-many-locals, too-many- branches
101+ # pylint: disable=invalid-name, too-many-branches
101102 def _draw_filled (self , x0 , y0 , x1 , y1 , x2 , y2 ):
102103 if y0 == y2 : # Handle awkward all-on-same-line case as its own thing
103104 a = x0
@@ -134,47 +135,6 @@ def _draw_filled(self, x0, y0, x1, y1, x2, y2):
134135 if a > b :
135136 a , b = b , a
136137 self ._line (a , y , b , y , 2 )
137-
138- def _line (self , x0 , y0 , x1 , y1 , color ):
139- if x0 == x1 :
140- if y0 > y1 :
141- y0 , y1 = y1 , y0
142- for _h in range (y0 , y1 ):
143- self ._bitmap [x0 , _h ] = color
144- elif y0 == y1 :
145- if x0 > x1 :
146- x0 , x1 = x1 , x0
147- for _w in range (x0 , x1 ):
148- self ._bitmap [_w , y0 ] = color
149- else :
150- steep = abs (y1 - y0 ) > abs (x1 - x0 )
151- if steep :
152- x0 , y0 = y0 , x0
153- x1 , y1 = y1 , x1
154-
155- if x0 > x1 :
156- x0 , x1 = x1 , x0
157- y0 , y1 = y1 , y0
158-
159- dx = x1 - x0
160- dy = abs (y1 - y0 )
161-
162- err = dx / 2
163-
164- if y0 < y1 :
165- ystep = 1
166- else :
167- ystep = - 1
168-
169- for x in range (x0 , x1 ):
170- if steep :
171- self ._bitmap [y0 , x ] = color
172- else :
173- self ._bitmap [x , y0 ] = color
174- err -= dy
175- if err < 0 :
176- y0 += ystep
177- err += dx
178138 # pylint: enable=invalid-name, too-many-locals, too-many-branches
179139
180140 @property
@@ -191,18 +151,3 @@ def fill(self, color):
191151 else :
192152 self ._palette [2 ] = color
193153 self ._palette .make_opaque (2 )
194-
195- @property
196- def outline (self ):
197- """The outline of the triangle. Can be a hex value for a color or
198- ``None`` for no outline."""
199- return self ._palette [1 ]
200-
201- @outline .setter
202- def outline (self , color ):
203- if color is None :
204- self ._palette [1 ] = 0
205- self ._palette .make_transparent (1 )
206- else :
207- self ._palette [1 ] = color
208- self ._palette .make_opaque (1 )
0 commit comments