def draw_oval(a, b):
    """
    Draws an oval centered around the Turtle's starting position by defining
    points around the oval and drawing lines between them.

    a is a numebr representing half of the oval's width
    b is a number representing half of the oval's height

    returns the points used to draw the oval.
    """
    points = []
    with restore_state_when_finished():
        enter_position = position()
        fly(enter_position[0]-(a), enter_position[1])
        points.append(position())
        for x in range(int(enter_position[0]-a), int(enter_position[0]+a)):
            y = sqrt(pow(b,2)-pow((x-enter_position[0])*b/a, 2)) + enter_position[1]
            goto(x, y)
            points.append(position())
        for x in range(int(enter_position[0]+a), int(enter_position[0]-a), -1):
            y = -(sqrt(pow(b,2)-pow((x-enter_position[0])*b/a, 2))) + enter_position[1]
            goto(x, y)
            points.append(position())
        goto(enter_position[0]-(a), enter_position[1])
        points.append(position())
    return(points)
def draw_taper(larger_oval, smaller_oval):
    """
    Draws a taper between a smaller oval and a larger oval assigning each point
    on the larger oval to a point on the smaller circle and drawing lines
    between them.

    larger_oval is a set of points represnting the points around the larger oval
    smaller_oval is a set of points represnting the points around the smaller oval
    """
    with restore_state_when_finished():
        prev_smaller_oval_index = 0
        prev_smaller_oval_point = smaller_oval[prev_smaller_oval_index]
        larger_oval_index = 0
        for curr_smaller_oval_index in range(1, len(smaller_oval)):
            curr_smaller_oval_point = smaller_oval[curr_smaller_oval_index]
            while abs( (larger_oval_index/len(larger_oval)) - (prev_smaller_oval_index/len(smaller_oval)) ) < abs( (larger_oval_index/len(larger_oval)) - (curr_smaller_oval_index/len(smaller_oval)) ):
                fly(larger_oval[larger_oval_index][0],larger_oval[larger_oval_index][1])
                goto(prev_smaller_oval_point)
                larger_oval_index += 1

            prev_smaller_oval_point = curr_smaller_oval_point
            prev_smaller_oval_index = curr_smaller_oval_index

        for i in range(larger_oval_index, len(larger_oval)):
            fly(larger_oval[i][0], larger_oval[i][1])
            goto(prev_smaller_oval_point)
def draw_cylinder(radius, height, x_rotation, color):
    """
    Draws a cylinder centered around the Turtle's starting position by drawing
    an over and then adding perspective to that oval with an origin defined
    below the oval. The oval is then projected toward that origin to create a
    3D cylinder.

    radius is a number representing the radius of the top circle of the cylinder
    height is a number representing the height of the water bottle
    x_rotation is a number between 0 and 1 used to determine the ratio of width
        to height of the oval (creating a perception of depth)
    color is a color used to  color of the water bottle

    returns the points used to draw the top oval of the cylinder.
    """
    with restore_state_when_finished():
        penup()
        setheading(270)
        forward(height/2)
        pendown()
        pencolor('gray')
        draw_oval(radius, radius*x_rotation)
        penup()
        setheading(90)
        forward(height)
        pendown()
        pencolor(color)
        oval_points = draw_oval(radius, radius*x_rotation)
        add_perspective(oval_points, [0, 100*height], -.01)
        pencolor('gray')
        draw_oval(radius, (radius)*x_rotation)
        penup()
    return oval_points
示例#4
0
def draw_head(settings):
    "Draws the head"
    fillcolor(HEAD_COLOR)
    with restore_state_when_finished():
        update_position(HEAD_ORIGIN)
        right(settings['head_angle'])
        rectangle_from_center(HEAD_WIDTH, HEAD_HEIGHT)
示例#5
0
def draw_triangle(side_len, color_name):
    #This function draws an equilateral triangle
    with restore_state_when_finished():
        color(color_name)
        begin_fill()
        for i in range(3):
            forward(side_len)
            right(120)
        end_fill()
示例#6
0
def draw_arm(settings, which_arm):
    """
    Draws an arm, starting with the upper arm, then the lower arm, and finally the hand
    """
    fillcolor(ARM_COLOR)
    with restore_state_when_finished():
        update_position(ARM_ORIGIN)
        right(90)
        shoulder_angle = settings[which_arm + '_arm_shoulder_angle']
        right(shoulder_angle)
        rectangle_from_side_edge(UPPER_ARM_WIDTH, UPPER_ARM_LENGTH)
        fly(UPPER_ARM_LENGTH)
        elbow_angle = settings[which_arm + '_arm_elbow_angle']
        right(elbow_angle)
        rectangle_from_side_edge(LOWER_ARM_WIDTH, LOWER_ARM_LENGTH)
        fly(LOWER_ARM_LENGTH)
        left(45)
        rectangle(HAND_WIDTH, HAND_LENGTH)
示例#7
0
def draw_leg(settings, which_leg):
    """
    Draws a leg, starting with the upper leg, then the lower leg, and finally the foot.
    """
    fillcolor(LEG_COLOR)
    with restore_state_when_finished():
        update_position(LEG_ORIGIN)
        right(90)
        hip_angle = settings[which_leg + '_leg_hip_angle']
        right(hip_angle)
        rectangle_from_side_edge(UPPER_LEG_WIDTH, UPPER_LEG_LENGTH)
        fly(UPPER_LEG_LENGTH)
        knee_angle = settings[which_leg + '_leg_knee_angle']
        right(knee_angle)
        rectangle_from_side_edge(LOWER_LEG_WIDTH, LOWER_LEG_LENGTH)
        fly(LOWER_LEG_LENGTH)
        left(90)
        back(LOWER_LEG_WIDTH / 2)
        rectangle_from_side_edge(FOOT_WIDTH, FOOT_LENGTH)