Exemple #1
0
def island(w, side, d, sp):
    """
    Draws a Minkowski island with the given side length and depth d.

    This function clears the window and makes a new Turtle t.  This turtle starts in
    lower right corner of the square centered at (0,0) with side length side. It is
    facing straight up. It draws by calling the function island_edge(t, side, d) four
    times, rotating the turtle left after each call to form a square.

    The turtle should be visible while drawing, but hidden at the end. The turtle color
    is 'sea green'.

    REMEMBER: You need to flush the turtle if the speed is 0.

    Parameter w: The window to draw upon.
    Precondition: w is a Window object.

    Parameter side: The side-length of the island
    Precondition: side is a valid side length (number >= 0)

    Parameter d: The recursive depth of the island
    Precondition: d is a valid depth (int >= 0)

    Parameter sp: The drawing speed.
    Precondition: sp is a valid turtle/pen speed.
    """
    # ARE THESE ALL OF THE PRECONDITIONS?
    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_valid_depth(d), report_error('d is not a valid depth', d)
    assert is_valid_speed(sp), \
    report_error('sp is not a valid turtle/pen speed',sp)

    w.clear()
    t = Turtle(w)

    t.visible = True

    t.speed = sp
    t.color = 'sea green'

    t.move(side / 2, -side / 2)
    t.heading = 90
    for i in range(4):
        island_edge(t, side, d)
        t.left(90)

    t.visible = False
    t.flush()
Exemple #2
0
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 north (NOT the default east).
    It then calls draw_spiral_helper(t, side, ang, n, sp). When it is done,
    the turtle is left hidden (visible is False).

    REMEMBER: You need to flush the turtle if the speed is 0.

    This procedure asserts all preconditions.

    Parameter w: The window to draw upon.
    Precondition: w is a introcs 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.
    """
    # ARE THESE ALL OF THE PRECONDITIONS?
    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_valid_iteration(n), report_error(
        'n is not a valid number of iterations', side)
    assert is_valid_speed(sp), report_error('sp is not a valid speed', sp)
    #added angle
    assert is_number(ang), report_error('ang is not a valid angle', ang)

    # HINT: w.clear() clears window.
    # HINT: Set the visible attribute to False at the end, and remember to flush
    w.clear()
    t = Turtle(w)
    t.heading = 90
    t.visible = True
    draw_spiral_helper(t, side, ang, n, sp)
    t.visible = False

    if t.speed == 0:
        t.flush()
    pass
Exemple #3
0
def radiate_petals(w, radius, width, n, sp):
    """
    Draws a color flower with n petals using radiate_petals_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 west (NOT the default east).
    It then calls radiate_petals_helper(t, side, n, sp). When it is done, the
    turtle is left hidden (visible is False).

    REMEMBER: You need to flush the turtle if the speed is 0.

    This procedure asserts all preconditions.

    Parameter w: The window to draw upon.
    Precondition: w is a introcs Window object.

    Parameter radius: The radius of the produced "flower"
    Precondition: radius is a valid side length (number >= 0)

    Parameter width: The width of an open petal
    Precondition: width 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.
    """
    # ARE THESE ALL OF THE PRECONDITIONS?
    assert is_window(w), report_error('w is not a valid window', w)
    assert is_valid_length(radius), report_error(
        'radius is not a valid length', radius)
    assert is_valid_length(width), report_error('width is not a valid length',
                                                width)
    assert is_valid_speed(sp), report_error('sp is not a valid speed', sp)
    assert (type(n) == int
            and n >= 2), report_error('n is not a valid number of lines', n)

    # HINT: w.clear() clears window.
    w.clear()
    t = Turtle(w)
    t.heading = 180
    t.visible = True
    radiate_petals_helper(t, radius, width, n, sp)
    t.visible = False
    if t.speed == 0:
        t.flush()
    # HINT: Set the visible attribute to False at the end, and remember to flush
    pass
Exemple #4
0
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 south (NOT the default east).
    It then calls multi_polygons_helper(t, side, k, n, sp). When it is done,
    the turtle is left hidden (visible is False).

    REMEMBER: You need to flush the turtle if the speed is 0.

    This procedure asserts all preconditions.

    Parameter w: The window to draw upon.
    Precondition: w is a introcs 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.
    """
    # ARE THESE ALL OF THE PRECONDITIONS?
    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_valid_speed(sp), report_error('sp is not a valid speed', sp)
    assert is_valid_iteration(k), report_error(
        'k is not a valid num of polygons', k)
    assert (type(n) == int
            and n >= 3), report_error('n is not a valid number of sides', n)

    # HINT: w.clear() clears window.
    # HINT: Set the visible attribute to False at the end, and remember to flush
    w.clear()
    t = Turtle(w)
    t.heading = 90
    t.visible = True
    multi_polygons_helper(t, side, k, n, sp)
    t.visible = False
    if t.speed == 0:
        t.flush()
    pass
Exemple #5
0
def branches(w, hght, ang, d, sp):
    """
    Draws a three-branches tree with height hght, angle ang, and depth d.

    This function clears the window and makes a new turtle t with color
    'forest green'. This turtle starts at the bottom of the tree facing north
    (NOT the default east). Since the tree should be centered at (0,0), this
    means the turtle is positioned at (0,-hght/2). This function then calls
    branches_helper(t,hght,ang,d), which does all the drawing. When it is
    done, the turtle is left hidden (visible is False).

    REMEMBER: You need to flush the turtle if the speed is 0.

    This procedure asserts all preconditions.

    Parameter w: The window to draw upon.
    Precondition: w is a introcs Window object.

    Parameter hght: The height of the three-branches tree
    Precondition: hght is a valid side length (number >= 0)

    Parameter angle: The branch angles (measured from the central branch)
    Precondition: angle is a number, 0 < angle < 180

    Parameter d: The recursive depth of the three-branches tree
    Precondition: n is a valid depth (int >= 0)
    """
    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 height',
                                               hght)
    assert (is_number(ang) and ang > 0
            and ang < 180), report_error('ang is not a valid angle', ang)
    assert is_valid_depth(d), report_error('d is not a valid depth', d)
    assert is_valid_speed(sp), report_error('sp is not a valid speed', sp)
    w.clear()
    t = Turtle(w)
    t.left(90)
    t.move(0, -hght / 2)
    t.visible = True
    branches_helper(t, hght, ang, d)
    t.visible = False
    if t.speed == 0:
        t.flush()
Exemple #6
0
def draw_two_lines(w, sp):
    """
    Draws two lines on to window w.

    This function clears w of any previous drawings. Then, in the middle of
    the window w, this function draws a green line 100 pixels to the east,
    and then a blue line 200 pixels to the north. It uses a new turtle that
    moves at speed sp, 0 <= sp <= 10, with 1 being slowest and 10 fastest
    (and 0 being "instant").

    REMEMBER: You need to flush the turtle if the speed is 0.

    This procedure asserts all preconditions.

    Parameter w: The window to draw upon.
    Precondition: w is a introcs 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)

    # Clear the window first!
    w.clear()

    # Create a turtle and draw
    t = 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 = 'blue'
    t.forward(200)

    # This is necessary if speed is 0!
    t.flush()