def bump(t, n, height): """make a bump with radius n at height*n """ stump(t, n*height) arc(t, n/2.0, 180) lt(t) fdlt(t, n*height+n)
def draw_s(t, n): fd(t, n/2) arc(t, n/2, 180) arc(t, n/2, -180) fdlt(t, n/2, -90) skip(t, 2*n) lt(t)
def square(t, length): """Draws a square with sides of the given length. Returns the Turtle to the starting position and location. """ for i in range(4): fd(t, length) lt(t)
def diagonal(t, x, y): """make a diagonal line to the given x, y offsets and return""" from math import atan2, sqrt, pi angle = atan2(y, x) * 180 / pi dist = sqrt(x**2 + y**2) lt(t, angle) fdbk(t, dist) rt(t, angle)
def flower(t, n, rf, re): angle = asin(0.5 * float(rf) / re) * 180 / pi rt(t, angle) for _ in range(n): arc(t, re, angle * 2) lt(t, 180 - angle * 2) arc(t, re, angle * 2) lt(t, 180 - angle * 2 + 360 / n)
def hangman(t, n, height): """make a vertical line to the given height and a horizontal line at the given height and then return. This is efficient to implement, and turns out to be useful, but it's not so semantically clean.""" stump(t, n * height) fdbk(t, n) lt(t) bk(t, n*height) rt(t)
def petal(t, r, angle): """Draws a petal using two arcs. t: Turtle r: radius of the arcs angle: angle (degrees) that subtends the arcs """ for i in range(2): arc(t, r, angle) lt(t, 180-angle)
def polyline(t, n, length, angle): """Draws n line segments. t: Turtle object n: number of line segments length: length of each segment angle: degrees between segments """ for i in range(n): fd(t, length) lt(t, angle)
def polypie(t, n, r): """Draws a pie divided into radial segments. t: Turtle n: number of segments r: length of the radial spokes """ angle = 360.0 / n for i in range(n): isosceles(t, r, angle/2) lt(t, angle)
def flower(t, n, r, angle): """Draws a flower with n petals. t: Turtle n: number of petals r: radius of the arcs angle: angle (degrees) that subtends the arcs """ for i in range(n): petal(t, r, angle) lt(t, 360.0/n)
def pie(t, sides, radius): angle1 = 360.0 / sides angle2 = (180.0 - angle1) / 2 print angle1 print angle2 rt(t, angle1 / 2) for _ in range(sides): fd(t, radius) lt(t, 180 - angle2) fd(t, 2 * radius * sin(pi / 180 * angle1 / 2)) lt(t, 180 - angle2) fd(t, radius) rt(t, 180)
def arc(t, r, angle): """Draws an arc with the given radius and angle. t: Turtle r: radius angle: angle subtended by the arc, in degrees """ arc_length = 2 * math.pi * r * abs(angle) / 360 n = int(arc_length / 4) + 1 step_length = arc_length / n step_angle = float(angle) / n # making a slight left turn before starting reduces # the error caused by the linear approximation of the arc lt(t, step_angle/2) polyline(t, n, step_length, step_angle) rt(t, step_angle/2)
def draw_spiral(t, n, length=3, a=0.1, b=0.0002): """Draws an Archimedian spiral starting at the origin. Args: n: how many line segments to draw length: how long each segment is a: how loose the initial spiral starts out (larger is looser) b: how loosly coiled the spiral is (larger is looser) http://en.wikipedia.org/wiki/Spiral """ theta = 0.0 for i in range(n): fd(t, length) dtheta = 1 / (a + b * theta) lt(t, dtheta) theta += dtheta
def isosceles(t, r, angle): """Draws an icosceles triangle. The turtle starts and ends at the peak, facing the middle of the base. t: Turtle r: length of the equal legs angle: peak angle in degrees """ y = r * math.sin(angle * math.pi / 180) rt(t, angle) fd(t, r) lt(t, 90+angle) fd(t, 2*y) lt(t, 90+angle) fd(t, r) lt(t, 180-angle)
def draw_square(turtle_name, length): for i in range(4): fd(turtle_name, length) lt(turtle_name)
def square(t,steps): for i in range(4): fd(t, steps) lt(t)
def stump(t, n, angle=90): """make a vertical line and leave the turtle at the top, facing right""" lt(t) fd(t, n) rt(t, angle)
def hollow(t, n): """move the turtle vertically and leave it at the top, facing right""" lt(t) skip(t, n) rt(t)
def polyline(t, n, length, angle): for i in range(n): fd(t, length) lt(t, angle)
def post(t, n): """make a vertical line and return to the original position""" lt(t) fdbk(t, n) rt(t)
def draw_polygon(turtle_name, length, num_sides): degree = 360 / num_sides for i in range(num_sides): fd(turtle_name, length) lt(turtle_name, degree)
def circle(t, r): """Draws a circle with the given radius. t: Turtle r: radius """ arc(t, r, 360) # the following condition checks whether we are # running as a script, in which case run the test code, # or being imported, in which case don't. if __name__ == '__main__': world = TurtleWorld() bob = Turtle() bob.delay = 0.001 # draw a circle centered on the origin radius = 100 pu(bob) fd(bob, radius) lt(bob) pd(bob) circle(bob, radius) die(bob) wait_for_user()
def fdlt(t, n, angle=90): """forward and left""" fd(t, n) lt(t, angle)