def main(): """Run each of the functions, allowing user to skip functions.""" w = tkturtle.Window() ans = raw_input('Call draw_two_lines? [y/n]: ') if ans.strip().lower() == 'y': draw_two_lines(w,5) ans = raw_input('Call draw_triangle? [y/n]: ') if ans.strip().lower() == 'y': w.clear() turt = tkturtle.Turtle(w) draw_triangle(turt,50,'blue') ans = raw_input('Call draw_hex? [y/n]: ') if ans.strip().lower() == 'y': w.clear() turt = tkturtle.Turtle(w) draw_hex(turt,50) ans = raw_input('Call draw_spiral? [y/n]: ') if ans.strip().lower() == 'y': draw_spiral(w, 1, 24, 64, 0) ans = raw_input('Call multi_polygons? [y/n]: ') if ans.strip().lower() == 'y': multi_polygons(w, 100, 5, 6, 0) ans = raw_input('Call radiate? [y/n]: ') if ans.strip().lower() == 'y': radiate(w, 150, 45, 0) ans = raw_input('Cantor depth [-1 to skip]: ') d = str_to_int(ans) if d >= 0: cantor(w, 200, 100, d) ans = raw_input('Sierpinski depth [-1 to skip]: ') d = str_to_int(ans) if d >= 0: sierpinski(w, 150, d) ans = raw_input('3-Branches depth [-1 to skip]: ') d = str_to_int(ans) if d >= 0: branches(w, 250, d) # Pause for the final image raw_input('Press <return>')
def radiate(w, side, n, sp): """Draw n straight radiating lines using radiate_helper(t, side, n, sp) This function clears the window and makes a new turtle t. This turtle starts in the middle of the canvas facing east (NOT the default west). It then calls radiate_helper(t, side, n, sp). When it is done, the turtle is left hidden (visible is False). Parameter w: The window to draw upon. Precondition: w is a tkturtle Window object. Parameter side: The length of each radial line Precondition: side is a valid side length (number >= 0) Parameter n: The number of lines to draw Precondition: n is an int >= 2 Parameter sp: The turtle speed. Precondition: sp is a valid turtle speed.""" assert is_window(w), report_error('w is not a valid window',w) assert is_valid_length(side), report_error('side is not a valid length',side) assert type(n) == int and n >=2, report_error('n is not a valid value',n) assert is_valid_speed(sp), report_error('sp is not a valid speed',sp) w.clear() t = tkturtle.Turtle(w) t.heading = 0 radiate_helper(t, side, n, sp) t.visible=False
def multi_polygons(w, side, k, n, sp): """Draws polygons using multi_polygons_helper(t, side, k, n, sp) This function clears the window and makes a new turtle t. This turtle starts in the middle of the canvas facing north (NOT the default west). It then calls multi_polygons_helper(t, side, k, n, sp). When it is done, the turtle is left hidden (visible is False). Parameter w: The window to draw upon. Precondition: w is a tkturtle Window object. Parameter side: The length of each polygon side Precondition: side is a valid side length (number >= 0) Parameter k: The number of polygons to draw Precondition: k is an int >= 1 Parameter n: The number of sides of each polygon Precondition: n is an int >= 3 Parameter sp: The turtle speed. Precondition: sp is a valid turtle speed.""" assert is_window(w), report_error('w is not a valid window',w) assert is_valid_length(side), report_error('side is not a valid length',side) assert type(k) == int and k >=1, report_error('k is not a valid value') assert type(n) == int and n >=3, report_error('n is not a valid side no.') assert is_valid_speed(sp), report_error('sp is not a valid speed',sp) w.clear() t = tkturtle.Turtle(w) t.heading = 90 multi_polygons_helper(t, side, k, n, sp) t.visible=False
def draw_spiral(w, side, ang, n, sp): """Draws a spiral using draw_spiral_helper(t, side, ang, n, sp) This function clears the window and makes a new turtle t. This turtle starts in the middle of the canvas facing east (NOT the default west). It then calls draw_spiral_helper(t, side, ang, n, sp). When it is done, the turtle is left hidden (visible is False). Parameter w: The window to draw upon. Precondition: w is a tkturtle Window object. Parameter side: The length of each spiral side Precondition: side is a valid side length (number >= 0) Parameter ang: The angle of each corner of the spiral Precondition: ang is a number Parameter n: The number of edges of the spiral Precondition: n is a valid number of iterations (int >= 1) Parameter sp: The turtle speed. Precondition: sp is a valid turtle speed.""" assert is_window(w), report_error('w is not a valid window',w) assert is_valid_length(side), report_error('side is not a valid length',side) assert is_number(ang), report_error('ang is not a valid angle',ang) assert is_valid_iteration(n), report_error('n is not a valid iteration',side) assert is_valid_speed(sp), report_error('sp is not a valid speed',sp) w.clear() t = tkturtle.Turtle(w) t.heading = 0 draw_spiral_helper(t, side, ang, n, sp) t.visible=False
def branches(w, hght, d): """Draws a three-branches tree with height hght and depth d. This function clears the window and makes a new turtle t. This turtle starts at the bottom of the tree facing north (NOT the default west). Since the tree should be centered at (0,0), this means the turtle is positioned at (0,-hght/2). This function calls branches_helper(t,hght,d), which does all the drawing. When it is done, the turtle is left hidden (visible is False). The turtle color is 'blue'. The Turtle is visible when drawing and hidden at the end. Parameter w: The window to draw upon. Precondition: w is a tkturtle Window object. Parameter hght: The height of the three-branches tree Precondition: side is a valid side length (number >= 0) Parameter d: The recursive depth of the three-branches tree Precondition: n is a valid depth (int >= 0)""" # ARE THESE ALL OF THE PRECONDITIONS? assert is_window(w), report_error('w is not a valid window',w) assert is_valid_length(hght), report_error('hght is not a valid length',hght) assert is_valid_depth(d), report_error('d is not a valid depth', d) w.clear() t = tkturtle.Turtle(w) t.heading = 90 t.move(0,-hght/2.0) t.visible = True t.color = 'blue' branches_helper(t, hght, d) t.visible = False
def draw_two_lines(w,sp): """Draws two lines on to window w. In the middle of the window w, this function draws a green line 100 pixels to the west, and then a red line 200 pixels to the south. It uses a new turtle that moves at speed sp, 0 <= sp <= 10, with 1 being slowest and 10 fastest (and 0 being "instant"). Parameter w: The window to draw upon. Precondition: w is a tkturtle Window object. Parameter sp: The turtle speed. Precondition: sp is a valid turtle speed.""" # Assert the preconditions assert is_window(w), report_error('w is not a valid window',w) assert is_valid_speed(sp), report_error('sp is not a valid speed',sp) t = tkturtle.Turtle(w) t.speed = sp t.color = 'green' t.forward(100) # draw a line 100 pixels in the current direction t.left(90) # add 90 degrees to the angle t.color = 'red' t.forward(200)