Example #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()
Example #2
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()
Example #3
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()