Skip to content

Commit 7bf2e46

Browse files
authored
Merge pull request #5 from dastels/master
Dot implemented
2 parents 57fc8ad + 14d8d49 commit 7bf2e46

File tree

3 files changed

+114
-7
lines changed

3 files changed

+114
-7
lines changed

adafruit_turtle.py

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
4747
"""
4848

49-
#pylint:disable=too-many-public-methods,invalid-name,too-many-instance-attributes
50-
#pylint:disable=too-few-public-methods,too-many-lines
49+
#pylint:disable=too-many-public-methods, too-many-instance-attributes, invalid-name
50+
#pylint:disable=too-few-public-methods, too-many-lines, too-many-arguments
5151

5252
import gc
5353
import math
@@ -185,6 +185,7 @@ def __init__(self, display=board.DISPLAY):
185185

186186
self._penstate = False
187187
self._pencolor = None
188+
self._pensize = 1
188189
self.pencolor(Color.WHITE)
189190

190191
self._display.show(self._splash)
@@ -287,7 +288,8 @@ def goto(self, x1, y1=None):
287288
while (not rev and x0 <= x1) or (rev and x1 <= x0):
288289
if steep:
289290
try:
290-
self._fg_bitmap[int(y0), int(x0)] = self._pencolor
291+
self._plot(int(y0), int(x0), self._pencolor)
292+
# self._fg_bitmap[int(y0), int(x0)] = self._pencolor
291293
except IndexError:
292294
pass
293295
self._x = y0
@@ -296,7 +298,8 @@ def goto(self, x1, y1=None):
296298
time.sleep(0.003)
297299
else:
298300
try:
299-
self._fg_bitmap[int(x0), int(y0)] = self._pencolor
301+
self._plot(int(x0), int(y0), self._pencolor)
302+
# self._fg_bitmap[int(x0), int(y0)] = self._pencolor
300303
except IndexError:
301304
pass
302305
self._x = x0
@@ -357,6 +360,60 @@ def home(self):
357360
self.setheading(90)
358361
self.goto(0, 0)
359362

363+
def _plot(self, x, y, c):
364+
try:
365+
self._fg_bitmap[int(x), int(y)] = c
366+
except IndexError:
367+
pass
368+
369+
def _draw_disk(self, x, y, width, height, r, color, fill=True, outline=True, stroke=1):
370+
"""Draw a filled and/or outlined circle"""
371+
if fill:
372+
self._helper(x+r, y+r, r, color=color, fill=True,
373+
x_offset=width-2*r-1, y_offset=height-2*r-1)
374+
if outline:
375+
self._helper(x+r, y+r, r, color=color, stroke=stroke,
376+
x_offset=width-2*r-1, y_offset=height-2*r-1)
377+
378+
# pylint: disable=too-many-locals, too-many-branches
379+
def _helper(self, x0, y0, r, color, x_offset=0, y_offset=0,
380+
stroke=1, fill=False):
381+
"""Draw quandrant wedges filled or outlined"""
382+
f = 1 - r
383+
ddF_x = 1
384+
ddF_y = -2 * r
385+
x = -1
386+
y = r
387+
388+
while x < y:
389+
if f >= 0:
390+
y -= 1
391+
ddF_y += 2
392+
f += ddF_y
393+
x += 1
394+
ddF_x += 2
395+
f += ddF_x
396+
if fill:
397+
for w in range(x0-y, x0+y+x_offset):
398+
self._plot(w, y0 + x + y_offset, color)
399+
self._plot(w, y0 - x, color)
400+
for w in range(x0-x, x0+x+x_offset):
401+
self._plot(w, y0 + y + y_offset, color)
402+
self._plot(w, y0 - y, color)
403+
else:
404+
for line in range(stroke):
405+
self._plot(x0 - y + line, y0 + x + y_offset, color)
406+
self._plot(x0 - x, y0 + y + y_offset - line, color)
407+
self._plot(x0 - y + line, y0 - x, color)
408+
self._plot(x0 - x, y0 - y + line, color)
409+
for line in range(stroke):
410+
self._plot(x0 + x + x_offset, y0 + y + y_offset - line, color)
411+
self._plot(x0 + y + x_offset - line, y0 + x + y_offset, color)
412+
self._plot(x0 + x + x_offset, y0 - y + line, color)
413+
self._plot(x0 + y + x_offset - line, y0 - x, color)
414+
415+
# pylint: enable=too-many-locals, too-many-branches
416+
360417
def circle(self, radius, extent=None, steps=None):
361418
"""Not implemented
362419
@@ -380,7 +437,7 @@ def circle(self, radius, extent=None, steps=None):
380437
raise NotImplementedError
381438

382439
#pylint:disable=keyword-arg-before-vararg
383-
def dot(self, size=None, *color):
440+
def dot(self, size=None, color=None):
384441
"""Not implemented
385442
386443
Draw a circular dot with diameter size, using color.
@@ -391,7 +448,15 @@ def dot(self, size=None, *color):
391448
:param color: the color of the dot
392449
393450
"""
394-
raise NotImplementedError
451+
if size is None:
452+
size = max(self._pensize + 4, self._pensize * 2)
453+
if color is None:
454+
color = self._pencolor
455+
else:
456+
color = self._color_to_pencolor(color)
457+
self._logger.debug('dot(%d)', size)
458+
self._draw_disk(self._x - size, self._y - size, 2 * size + 1, 2 * size + 1, size, color)
459+
self._fg_sprite[0, 0] = 0
395460

396461
def stamp(self):
397462
"""Not implemented
@@ -555,7 +620,9 @@ def pensize(self, width=None):
555620
:param width: - a positive number
556621
557622
"""
558-
raise NotImplementedError
623+
if width is not None:
624+
self._pensize = width
625+
return self._pensize
559626
width = pensize
560627

561628
def pen(self, pen=None, **pendict):
@@ -596,6 +663,11 @@ def isdown(self):
596663
############################################################################
597664
# Color control
598665

666+
#pylint:disable=no-self-use
667+
def _color_to_pencolor(self, c):
668+
return 1 + Color.colors.index(c)
669+
#pylint:enable=no-self-use
670+
599671
def color(self, *args):
600672
"""Not implemented
601673

examples/turtle_dots.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import board
2+
from adafruit_turtle import turtle, Color
3+
4+
turtle = turtle(board.DISPLAY)
5+
size = min(board.DISPLAY.width, board.DISPLAY.height) * 0.5
6+
7+
print("Turtle time! Lets draw a rainbow benzene")
8+
9+
10+
turtle.pendown()
11+
12+
for _ in range(4):
13+
turtle.dot(8, Color.RED)
14+
turtle.left(90)
15+
turtle.forward(25)
16+
17+
while True:
18+
pass

examples/turtle_swirl.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import board
2+
from adafruit_turtle import turtle, Color
3+
4+
turtle = turtle(board.DISPLAY)
5+
6+
7+
turtle.pendown()
8+
9+
colors = [Color.ORANGE, Color.PURPLE]
10+
11+
for x in range(300):
12+
turtle.pencolor(colors[x % 2])
13+
turtle.forward(x)
14+
turtle.left(91)
15+
16+
while True:
17+
pass

0 commit comments

Comments
 (0)