Skip to content

Commit 2e7d56f

Browse files
authored
fix fill_triangle bug issue #33
This fix for issue #33 replaces the for loop at lines 283 to 291 with a while loop so that the y variable is properly incremented between the loops to fill the top and bottom parts of the triangle. This avoids screen row at y0 being drawn twice, and corrects the shape of the triangle. The changes to lines 278 to 282 (as numbered after the fix) initiate the two loops in a way that addresses issue #22 where a triangle with a flat bottom edge was not drawn correctly. This fix has been implemented in the derived repository [framebuf2](https://github.com/peter-l5/framebuf2) which applies the algorithm here and has been tested with the following code. (Note that `f=True` parameter draws a filled triangle in this implementation.) ``` for z in range(3,123+1,20): for x in range(10,120+1,20): for y in range(0,128): display.fill(0) display.triangle(x,y,30,z,90,63,c=1,f=True) display.show() ```
1 parent ed5a3d6 commit 2e7d56f

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

adafruit_gfx/gfx.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ def fill_triangle(self, x0, y0, x1, y1, x2, y2, *args, **kwargs):
247247
x0, x1 = x1, x0
248248
a = 0
249249
b = 0
250-
y = 0
251250
last = 0
252251
if y0 == y2:
253252
a = x0
@@ -276,18 +275,20 @@ def fill_triangle(self, x0, y0, x1, y1, x2, y2, *args, **kwargs):
276275
dy12 = 1
277276
sa = 0
278277
sb = 0
279-
if y1 == y2 or y0 == y1: # pylint: disable=consider-using-in
280-
last = y1
281-
else:
278+
y = y0
279+
if y0 == y1:
282280
last = y1 - 1
283-
for y in range(y0, last + 1):
281+
else:
282+
last = y1
283+
while y <= last:
284284
a = x0 + sa // dy01
285285
b = x0 + sb // dy02
286286
sa += dx01
287287
sb += dx02
288288
if a > b:
289289
a, b = b, a
290290
self.hline(a, y, b - a + 1, *args, **kwargs)
291+
y += 1
291292
sa = dx12 * (y - y1)
292293
sb = dx02 * (y - y0)
293294
while y <= y2:

0 commit comments

Comments
 (0)