Skip to content

Commit

Permalink
Account for fractions of a pixel when drawing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jepler committed Dec 4, 2024
1 parent 06de267 commit e1cddff
Showing 1 changed file with 9 additions and 70 deletions.
79 changes: 9 additions & 70 deletions adafruit_turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,84 +46,21 @@ class Color:
"""Standard colors"""

WHITE = 0xFFFFFF
"""0xFFFFFF
:meta hide-value:"""

BLACK = 0x000000
"""0x000000
:meta hide-value:"""

RED = 0xFF0000
"""0xFF0000
:meta hide-value:"""

ORANGE = 0xFFA500
"""0xFFA500
:meta hide-value:"""

YELLOW = 0xFFEE00
"""0xFFEE00
:meta hide-value:"""

GREEN = 0x00C000
"""0x00C000
:meta hide-value:"""

BLUE = 0x0000FF
"""0x0000FF
:meta hide-value:"""

PURPLE = 0x8040C0
"""0x8040C0
:meta hide-value:"""

PINK = 0xFF40C0
"""0xFF40C0
:meta hide-value:"""

LIGHT_GRAY = 0xAAAAAA
"""0xAAAAAA
:meta hide-value:"""

GRAY = 0x444444
"""0x444444
:meta hide-value:"""

BROWN = 0xCA801D
"""0xCA801D
:meta hide-value:"""

DARK_GREEN = 0x008700
"""0x008700
:meta hide-value:"""

TURQUOISE = 0x00C0C0
"""0x00C0C0
:meta hide-value:"""

DARK_BLUE = 0x0000AA
"""0x0000AA
:meta hide-value:"""

DARK_RED = 0x800000
"""0x800000
:meta hide-value:"""

colors = (
BLACK,
Expand Down Expand Up @@ -234,11 +171,11 @@ def __init__(
self._y = self._h // (2 * scale)
self._speed = 6
self._heading: float = 0
self._logomode = True
self._fullcircle = 360.0
self._degreesPerAU = 1.0
self._logomode = False
self._angleOrient = -1
self._angleOffset: float = self._fullcircle / 4
self._angleOrient = 1
self._angleOffset: float = 0
self._bg_color = 0

self._splash: displayio.Group = displayio.Group()
Expand Down Expand Up @@ -414,6 +351,12 @@ def goto(
self._y = yn
self._drawturtle()
return

self._do_draw_line(round(self._x), round(self._y), round(xn), round(yn))
self._x = xn
self._y = yn

def _do_draw_line(self, x0: int, y0: int, xn: int, yn: int):
steep = abs(yn - y0) > abs(xn - x0)
rev = False
dx = xn - x0
Expand Down Expand Up @@ -444,15 +387,11 @@ def goto(
self._plot(int(y0), int(x0), self._pencolor)
except IndexError:
pass
self._x = y0
self._y = x0
else:
try:
self._plot(int(x0), int(y0), self._pencolor)
except IndexError:
pass
self._x = x0
self._y = y0
if self._speed > 0:
if step >= self._speed:
# mark the step
Expand Down

0 comments on commit e1cddff

Please sign in to comment.