Пример #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()
Пример #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
Пример #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
Пример #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