Skip to content

Commit ad49f2a

Browse files
committed
Add circle and support
1 parent 4ffcc79 commit ad49f2a

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

adafruit_turtle.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ def __init__(self, display=board.DISPLAY):
147147
self._speed = 6
148148
self._heading = 90
149149
self._logomode = False
150+
self._fullcircle = 360.0
151+
self._degreesPerAU = 1.0
152+
self._mode = "standard"
153+
self._angleOffset = 0
150154

151155
self._splash = displayio.Group(max_size=3)
152156

@@ -222,7 +226,6 @@ def backward(self, distance):
222226
self.forward(-distance)
223227
bk = backward
224228
back = backward
225-
226229
def right(self, angle):
227230
"""Turn turtle right by angle units. (Units are by default degrees,
228231
but can be set via the degrees() and radians() functions.)
@@ -367,9 +370,7 @@ def _plot(self, x, y, c):
367370
pass
368371

369372
def circle(self, radius, extent=None, steps=None):
370-
"""Not implemented
371-
372-
Draw a circle with given radius. The center is radius units left of
373+
"""Draw a circle with given radius. The center is radius units left of
373374
the turtle; extent - an angle - determines which part of the circle is
374375
drawn. If extent is not given, draw the entire circle. If extent is not
375376
a full circle, one endpoint of the arc is the current pen position.
@@ -384,9 +385,27 @@ def circle(self, radius, extent=None, steps=None):
384385
:param radius: the radius of the circle
385386
:param extent: the arc of the circle to be drawn
386387
:param steps: how many points along the arc are computed
387-
388388
"""
389-
raise NotImplementedError
389+
# call: circle(radius) # full circle
390+
# --or: circle(radius, extent) # arc
391+
# --or: circle(radius, extent, steps)
392+
# --or: circle(radius, steps=6) # 6-sided polygon
393+
394+
if extent is None:
395+
extent = self._fullcircle
396+
if steps is None:
397+
frac = abs(extent)/self._fullcircle
398+
steps = 1+int(min(11+abs(radius)/6.0, 59.0)*frac)
399+
w = 1.0 * extent / steps
400+
w2 = 0.5 * w
401+
l = 2.0 * radius * math.sin(w2*math.pi/180.0*self._degreesPerAU)
402+
if radius < 0:
403+
l, w, w2 = -l, -w, -w2
404+
self.left(w2)
405+
for _ in range(steps):
406+
self.forward(l)
407+
self.left(w)
408+
self.right(w2)
390409

391410
def _draw_disk(self, x, y, width, height, r, color, fill=True, outline=True, stroke=1):
392411
"""Draw a filled and/or outlined circle"""
@@ -509,7 +528,6 @@ def speed(self, speed=None):
509528
"normal": 6
510529
"slow": 3
511530
"slowest": 1
512-
513531
Speeds from 1 to 10 enforce increasingly faster animation of line
514532
drawing and turtle turning.
515533
@@ -576,21 +594,27 @@ def distance(self, x1, y1=None):
576594
############################################################################
577595
# Setting and measurement
578596

579-
def degrees(self, fullcircle=360):
580-
"""Not implemented
597+
def _setDegreesPerAU(self, fullcircle):
598+
"""Helper function for degrees() and radians()"""
599+
self._fullcircle = fullcircle
600+
self._degreesPerAU = 360/fullcircle
601+
if self._mode == "standard":
602+
self._angleOffset = 0
603+
else:
604+
self._angleOffset = fullcircle/4.
605+
581606

582-
Set angle measurement units, i.e. set number of "degrees" for a full circle.
607+
def degrees(self, fullcircle=360):
608+
"""Set angle measurement units, i.e. set number of "degrees" for a full circle.
583609
Default value is 360 degrees.
584610
585611
:param fullcircle: the number of degrees in a full circle
586612
"""
587-
raise NotImplementedError
613+
self._setDegreesPerAU(fullcircle)
588614

589615
def radians(self):
590-
"""Not implemented
591-
592-
Set the angle measurement units to radians. Equivalent to degrees(2*math.pi)."""
593-
raise NotImplementedError
616+
"""Set the angle measurement units to radians. Equivalent to degrees(2*math.pi)."""
617+
self._setDegreesPerAU(2*math.pi)
594618

595619

596620
############################################################################

examples/turtle_circle.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import board
2+
from adafruit_turtle import Color, turtle
3+
4+
turtle = turtle(board.DISPLAY)
5+
6+
turtle.pencolor(Color.WHITE)
7+
8+
turtle.pendown()
9+
turtle.circle(20)
10+
turtle.forward(20)
11+
turtle.circle(20, extent=180)
12+
turtle.forward(50)
13+
turtle.circle(50, steps=6)
14+
while True:
15+
pass

0 commit comments

Comments
 (0)