From a0f37cc267acc07799b9d6e765f0a28a5531bf14 Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Thu, 27 Jun 2019 13:15:10 -0400 Subject: [PATCH 1/3] Remove 'Not implemented' note from docstring --- adafruit_turtle.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/adafruit_turtle.py b/adafruit_turtle.py index 743396f..1de9abc 100644 --- a/adafruit_turtle.py +++ b/adafruit_turtle.py @@ -438,9 +438,7 @@ def circle(self, radius, extent=None, steps=None): #pylint:disable=keyword-arg-before-vararg def dot(self, size=None, color=None): - """Not implemented - - Draw a circular dot with diameter size, using color. + """Draw a circular dot with diameter size, using color. If size is not given, the maximum of pensize+4 and 2*pensize is used. From 4ffcc7943250c1d486865c1f64b5915e726c1aed Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Thu, 27 Jun 2019 13:15:40 -0400 Subject: [PATCH 2/3] Move circle method above dot support methods --- adafruit_turtle.py | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/adafruit_turtle.py b/adafruit_turtle.py index 1de9abc..4c90302 100644 --- a/adafruit_turtle.py +++ b/adafruit_turtle.py @@ -366,6 +366,28 @@ def _plot(self, x, y, c): except IndexError: pass + def circle(self, radius, extent=None, steps=None): + """Not implemented + + Draw a circle with given radius. The center is radius units left of + the turtle; extent - an angle - determines which part of the circle is + drawn. If extent is not given, draw the entire circle. If extent is not + a full circle, one endpoint of the arc is the current pen position. + Draw the arc in counterclockwise direction if radius is positive, + otherwise in clockwise direction. Finally the direction of the turtle + is changed by the amount of extent. + + As the circle is approximated by an inscribed regular polygon, steps + determines the number of steps to use. If not given, it will be + calculated automatically. May be used to draw regular polygons. + + :param radius: the radius of the circle + :param extent: the arc of the circle to be drawn + :param steps: how many points along the arc are computed + + """ + raise NotImplementedError + def _draw_disk(self, x, y, width, height, r, color, fill=True, outline=True, stroke=1): """Draw a filled and/or outlined circle""" if fill: @@ -414,28 +436,6 @@ def _helper(self, x0, y0, r, color, x_offset=0, y_offset=0, # pylint: enable=too-many-locals, too-many-branches - def circle(self, radius, extent=None, steps=None): - """Not implemented - - Draw a circle with given radius. The center is radius units left of - the turtle; extent - an angle - determines which part of the circle is - drawn. If extent is not given, draw the entire circle. If extent is not - a full circle, one endpoint of the arc is the current pen position. - Draw the arc in counterclockwise direction if radius is positive, - otherwise in clockwise direction. Finally the direction of the turtle - is changed by the amount of extent. - - As the circle is approximated by an inscribed regular polygon, steps - determines the number of steps to use. If not given, it will be - calculated automatically. May be used to draw regular polygons. - - :param radius: the radius of the circle - :param extent: the arc of the circle to be drawn - :param steps: how many points along the arc are computed - - """ - raise NotImplementedError - #pylint:disable=keyword-arg-before-vararg def dot(self, size=None, color=None): """Draw a circular dot with diameter size, using color. From ad49f2a9818eabf1b8da26cbbbf2fb1f196e359e Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Thu, 27 Jun 2019 13:47:28 -0400 Subject: [PATCH 3/3] Add circle and support --- adafruit_turtle.py | 54 ++++++++++++++++++++++++++++----------- examples/turtle_circle.py | 15 +++++++++++ 2 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 examples/turtle_circle.py diff --git a/adafruit_turtle.py b/adafruit_turtle.py index 4c90302..9bb2b4e 100644 --- a/adafruit_turtle.py +++ b/adafruit_turtle.py @@ -147,6 +147,10 @@ def __init__(self, display=board.DISPLAY): self._speed = 6 self._heading = 90 self._logomode = False + self._fullcircle = 360.0 + self._degreesPerAU = 1.0 + self._mode = "standard" + self._angleOffset = 0 self._splash = displayio.Group(max_size=3) @@ -222,7 +226,6 @@ def backward(self, distance): self.forward(-distance) bk = backward back = backward - def right(self, angle): """Turn turtle right by angle units. (Units are by default degrees, but can be set via the degrees() and radians() functions.) @@ -367,9 +370,7 @@ def _plot(self, x, y, c): pass def circle(self, radius, extent=None, steps=None): - """Not implemented - - Draw a circle with given radius. The center is radius units left of + """Draw a circle with given radius. The center is radius units left of the turtle; extent - an angle - determines which part of the circle is drawn. If extent is not given, draw the entire circle. If extent is not 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): :param radius: the radius of the circle :param extent: the arc of the circle to be drawn :param steps: how many points along the arc are computed - """ - raise NotImplementedError + # call: circle(radius) # full circle + # --or: circle(radius, extent) # arc + # --or: circle(radius, extent, steps) + # --or: circle(radius, steps=6) # 6-sided polygon + + if extent is None: + extent = self._fullcircle + if steps is None: + frac = abs(extent)/self._fullcircle + steps = 1+int(min(11+abs(radius)/6.0, 59.0)*frac) + w = 1.0 * extent / steps + w2 = 0.5 * w + l = 2.0 * radius * math.sin(w2*math.pi/180.0*self._degreesPerAU) + if radius < 0: + l, w, w2 = -l, -w, -w2 + self.left(w2) + for _ in range(steps): + self.forward(l) + self.left(w) + self.right(w2) def _draw_disk(self, x, y, width, height, r, color, fill=True, outline=True, stroke=1): """Draw a filled and/or outlined circle""" @@ -509,7 +528,6 @@ def speed(self, speed=None): "normal": 6 "slow": 3 "slowest": 1 - Speeds from 1 to 10 enforce increasingly faster animation of line drawing and turtle turning. @@ -576,21 +594,27 @@ def distance(self, x1, y1=None): ############################################################################ # Setting and measurement - def degrees(self, fullcircle=360): - """Not implemented + def _setDegreesPerAU(self, fullcircle): + """Helper function for degrees() and radians()""" + self._fullcircle = fullcircle + self._degreesPerAU = 360/fullcircle + if self._mode == "standard": + self._angleOffset = 0 + else: + self._angleOffset = fullcircle/4. + - Set angle measurement units, i.e. set number of "degrees" for a full circle. + def degrees(self, fullcircle=360): + """Set angle measurement units, i.e. set number of "degrees" for a full circle. Default value is 360 degrees. :param fullcircle: the number of degrees in a full circle """ - raise NotImplementedError + self._setDegreesPerAU(fullcircle) def radians(self): - """Not implemented - - Set the angle measurement units to radians. Equivalent to degrees(2*math.pi).""" - raise NotImplementedError + """Set the angle measurement units to radians. Equivalent to degrees(2*math.pi).""" + self._setDegreesPerAU(2*math.pi) ############################################################################ diff --git a/examples/turtle_circle.py b/examples/turtle_circle.py new file mode 100644 index 0000000..09ab899 --- /dev/null +++ b/examples/turtle_circle.py @@ -0,0 +1,15 @@ +import board +from adafruit_turtle import Color, turtle + +turtle = turtle(board.DISPLAY) + +turtle.pencolor(Color.WHITE) + +turtle.pendown() +turtle.circle(20) +turtle.forward(20) +turtle.circle(20, extent=180) +turtle.forward(50) +turtle.circle(50, steps=6) +while True: + pass