Skip to content

Commit 8559c02

Browse files
authored
Merge pull request #6 from dastels/master
Implement circle
2 parents 7bf2e46 + ad49f2a commit 8559c02

File tree

2 files changed

+72
-35
lines changed

2 files changed

+72
-35
lines changed

adafruit_turtle.py

Lines changed: 57 additions & 35 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.)
@@ -366,6 +369,44 @@ def _plot(self, x, y, c):
366369
except IndexError:
367370
pass
368371

372+
def circle(self, radius, extent=None, steps=None):
373+
"""Draw a circle with given radius. The center is radius units left of
374+
the turtle; extent - an angle - determines which part of the circle is
375+
drawn. If extent is not given, draw the entire circle. If extent is not
376+
a full circle, one endpoint of the arc is the current pen position.
377+
Draw the arc in counterclockwise direction if radius is positive,
378+
otherwise in clockwise direction. Finally the direction of the turtle
379+
is changed by the amount of extent.
380+
381+
As the circle is approximated by an inscribed regular polygon, steps
382+
determines the number of steps to use. If not given, it will be
383+
calculated automatically. May be used to draw regular polygons.
384+
385+
:param radius: the radius of the circle
386+
:param extent: the arc of the circle to be drawn
387+
:param steps: how many points along the arc are computed
388+
"""
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)
409+
369410
def _draw_disk(self, x, y, width, height, r, color, fill=True, outline=True, stroke=1):
370411
"""Draw a filled and/or outlined circle"""
371412
if fill:
@@ -414,33 +455,9 @@ def _helper(self, x0, y0, r, color, x_offset=0, y_offset=0,
414455

415456
# pylint: enable=too-many-locals, too-many-branches
416457

417-
def circle(self, radius, extent=None, steps=None):
418-
"""Not implemented
419-
420-
Draw a circle with given radius. The center is radius units left of
421-
the turtle; extent - an angle - determines which part of the circle is
422-
drawn. If extent is not given, draw the entire circle. If extent is not
423-
a full circle, one endpoint of the arc is the current pen position.
424-
Draw the arc in counterclockwise direction if radius is positive,
425-
otherwise in clockwise direction. Finally the direction of the turtle
426-
is changed by the amount of extent.
427-
428-
As the circle is approximated by an inscribed regular polygon, steps
429-
determines the number of steps to use. If not given, it will be
430-
calculated automatically. May be used to draw regular polygons.
431-
432-
:param radius: the radius of the circle
433-
:param extent: the arc of the circle to be drawn
434-
:param steps: how many points along the arc are computed
435-
436-
"""
437-
raise NotImplementedError
438-
439458
#pylint:disable=keyword-arg-before-vararg
440459
def dot(self, size=None, color=None):
441-
"""Not implemented
442-
443-
Draw a circular dot with diameter size, using color.
460+
"""Draw a circular dot with diameter size, using color.
444461
If size is not given, the maximum of pensize+4 and
445462
2*pensize is used.
446463
@@ -511,7 +528,6 @@ def speed(self, speed=None):
511528
"normal": 6
512529
"slow": 3
513530
"slowest": 1
514-
515531
Speeds from 1 to 10 enforce increasingly faster animation of line
516532
drawing and turtle turning.
517533
@@ -578,21 +594,27 @@ def distance(self, x1, y1=None):
578594
############################################################################
579595
# Setting and measurement
580596

581-
def degrees(self, fullcircle=360):
582-
"""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.
583605

584-
Set angle measurement units, i.e. set number of "degrees" for a full circle.
606+
607+
def degrees(self, fullcircle=360):
608+
"""Set angle measurement units, i.e. set number of "degrees" for a full circle.
585609
Default value is 360 degrees.
586610
587611
:param fullcircle: the number of degrees in a full circle
588612
"""
589-
raise NotImplementedError
613+
self._setDegreesPerAU(fullcircle)
590614

591615
def radians(self):
592-
"""Not implemented
593-
594-
Set the angle measurement units to radians. Equivalent to degrees(2*math.pi)."""
595-
raise NotImplementedError
616+
"""Set the angle measurement units to radians. Equivalent to degrees(2*math.pi)."""
617+
self._setDegreesPerAU(2*math.pi)
596618

597619

598620
############################################################################

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)