def draw_j(t, n): beam(t, n, 2) arc(t, n/2, 90) fd(t, 3*n/2) skip(t, -2*n) rt(t) skip(t, n/2)
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 move(t, length): """Move Turtle (t) forward (length) units without leaving a trail. Leaves the pen down. """ pu(t) fd(t, length) pd(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 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 draw_pie(t, n, r): """Draws a pie, then moves into position to the right. t: Turtle n: number of segments r: length of the radial spokes """ polypie(t, n, r) pu(t) fd(t, r*2 + 10) pd(t)
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 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 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 polyline(t, n, length, angle): for i in range(n): fd(t, length) lt(t, angle)
def fdlt(t, n, angle=90): """forward and left""" fd(t, n) lt(t, angle)
def draw_g(t, n): hangman(t, n, 2) fd(t, n/2) beam(t, n/2, 2) fd(t, n/2) post(t, n)
def draw_i(t, n): beam(t, n, 2) fd(t, n/2) post(t, 2*n) fd(t, n/2)
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 draw_l(t, n): post(t, 2*n) fd(t, n)
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 draw_square(turtle_name, length): for i in range(4): fd(turtle_name, length) lt(turtle_name)
def draw_e(t, n): draw_ef(t, n) fd(t, n)
def draw_z(t, n): beam(t, n, 2) diagonal(t, n, 2*n) fd(t, n)
def skip(t, n): """lift the pen and move""" pu(t) fd(t, n) pd(t)
def draw_u(t, n): post(t, 2*n) fd(t, n) post(t, 2*n)
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 fdbk(t, n): """forward and back, ending at the original position""" fd(t, n) bk(t, n)
def draw_c(t, n): hangman(t, n, 2) fd(t, n)