Skip to content

Commit 614e9e2

Browse files
committed
Account for fractions of a pixel when drawing
Previously, the endpoint of the line was always moved along in increments of 1 pixel, so that the endpoint would always be rounded down. This could accumulate to give quite large differences from what the program intended. Ensure that "goto" always ends up storing the floating point endpoints and that the line is drawn from the rounded-integer starting coordinate and rounded-integer ending coordinate. This makes the 3 test lines in the OP's "turtle_truncate.txt" example be the same length. Closes: #41
1 parent 06de267 commit 614e9e2

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

adafruit_turtle.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,17 @@ def goto(
407407
xn: float = x1[0] if y1 is None else x1 # type: ignore
408408
xn += self._w // 2
409409
yn = self._h // 2 - yn
410-
x0 = self._x
411-
y0 = self._y
412410
if not self.isdown():
413411
self._x = xn # woot, we just skip ahead
414412
self._y = yn
415413
self._drawturtle()
416414
return
415+
416+
self._do_draw_line(round(self._x), round(self._y), round(xn), round(yn))
417+
self._x = xn
418+
self._y = yn
419+
420+
def _do_draw_line(self, x0: int, y0: int, xn: int, yn: int):
417421
steep = abs(yn - y0) > abs(xn - x0)
418422
rev = False
419423
dx = xn - x0
@@ -444,15 +448,11 @@ def goto(
444448
self._plot(int(y0), int(x0), self._pencolor)
445449
except IndexError:
446450
pass
447-
self._x = y0
448-
self._y = x0
449451
else:
450452
try:
451453
self._plot(int(x0), int(y0), self._pencolor)
452454
except IndexError:
453455
pass
454-
self._x = x0
455-
self._y = y0
456456
if self._speed > 0:
457457
if step >= self._speed:
458458
# mark the step

0 commit comments

Comments
 (0)