Skip to content

Commit e1cddff

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 e1cddff

File tree

1 file changed

+9
-70
lines changed

1 file changed

+9
-70
lines changed

adafruit_turtle.py

Lines changed: 9 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -46,84 +46,21 @@ class Color:
4646
"""Standard colors"""
4747

4848
WHITE = 0xFFFFFF
49-
"""0xFFFFFF
50-
51-
:meta hide-value:"""
52-
5349
BLACK = 0x000000
54-
"""0x000000
55-
56-
:meta hide-value:"""
57-
5850
RED = 0xFF0000
59-
"""0xFF0000
60-
61-
:meta hide-value:"""
62-
6351
ORANGE = 0xFFA500
64-
"""0xFFA500
65-
66-
:meta hide-value:"""
67-
6852
YELLOW = 0xFFEE00
69-
"""0xFFEE00
70-
71-
:meta hide-value:"""
72-
7353
GREEN = 0x00C000
74-
"""0x00C000
75-
76-
:meta hide-value:"""
77-
7854
BLUE = 0x0000FF
79-
"""0x0000FF
80-
81-
:meta hide-value:"""
82-
8355
PURPLE = 0x8040C0
84-
"""0x8040C0
85-
86-
:meta hide-value:"""
87-
8856
PINK = 0xFF40C0
89-
"""0xFF40C0
90-
91-
:meta hide-value:"""
92-
9357
LIGHT_GRAY = 0xAAAAAA
94-
"""0xAAAAAA
95-
96-
:meta hide-value:"""
97-
9858
GRAY = 0x444444
99-
"""0x444444
100-
101-
:meta hide-value:"""
102-
10359
BROWN = 0xCA801D
104-
"""0xCA801D
105-
106-
:meta hide-value:"""
107-
10860
DARK_GREEN = 0x008700
109-
"""0x008700
110-
111-
:meta hide-value:"""
112-
11361
TURQUOISE = 0x00C0C0
114-
"""0x00C0C0
115-
116-
:meta hide-value:"""
117-
11862
DARK_BLUE = 0x0000AA
119-
"""0x0000AA
120-
121-
:meta hide-value:"""
122-
12363
DARK_RED = 0x800000
124-
"""0x800000
125-
126-
:meta hide-value:"""
12764

12865
colors = (
12966
BLACK,
@@ -234,11 +171,11 @@ def __init__(
234171
self._y = self._h // (2 * scale)
235172
self._speed = 6
236173
self._heading: float = 0
174+
self._logomode = True
237175
self._fullcircle = 360.0
238176
self._degreesPerAU = 1.0
239-
self._logomode = False
240-
self._angleOrient = -1
241-
self._angleOffset: float = self._fullcircle / 4
177+
self._angleOrient = 1
178+
self._angleOffset: float = 0
242179
self._bg_color = 0
243180

244181
self._splash: displayio.Group = displayio.Group()
@@ -414,6 +351,12 @@ def goto(
414351
self._y = yn
415352
self._drawturtle()
416353
return
354+
355+
self._do_draw_line(round(self._x), round(self._y), round(xn), round(yn))
356+
self._x = xn
357+
self._y = yn
358+
359+
def _do_draw_line(self, x0: int, y0: int, xn: int, yn: int):
417360
steep = abs(yn - y0) > abs(xn - x0)
418361
rev = False
419362
dx = xn - x0
@@ -444,15 +387,11 @@ def goto(
444387
self._plot(int(y0), int(x0), self._pencolor)
445388
except IndexError:
446389
pass
447-
self._x = y0
448-
self._y = x0
449390
else:
450391
try:
451392
self._plot(int(x0), int(y0), self._pencolor)
452393
except IndexError:
453394
pass
454-
self._x = x0
455-
self._y = y0
456395
if self._speed > 0:
457396
if step >= self._speed:
458397
# mark the step

0 commit comments

Comments
 (0)