def _convert_to_barycentric(point, simplex, coordinates):
    """ Converts the coordinates of a point into barycentric coordinates given a simplex.

    Given a 2D point inside a simplex (a line segment or a triangle), find out its
    barycentric coordinates. In the case of the line (1-simplex), this would be the point
    expressed as a linear combination of the two endpoints. In the case of the triangle
    (2-simplex), this would be the point expressed as a linear combination of three corner
    coordinates.

    This method will not work when finding barycentric coordinates of points within a
    triangle or line segment in R^3. It is only meant for finding barycentric coordinates
    of 2D points within 2D line segments or triangles.

    Parameters
    ----------
    point: list of floats, length 2
        The 2D coordinates of the flattened vertex. The z-component should be 0.
    simplex: list of ints, length 2 or 3
        The indices corresponding to coordinates making up the line segment/triangle.
    coordinates: list of pairs of floats
        The 2D coordinate system in which the point and simplex lie.

    Returns
    -------
    list of floats, length 2 or 3
        The lambda values (i.e. the weights used in the linear combination) corresponding
        to the barycentric coordinates of the point in the simplex. Length depends on the
        type of simplex - 2 if a line, 3 if a triangle. If all values are between 0 and 1,
        the point is in the simplex.
    """

    if not len(point) == len(coordinates[0]) == 2:
        raise Exception("_convert_to_barycentric: Invalid coordinate dimensions. " \
                        "This method only accepts coordinates in 2D.")
    #initialise result
    result = []

    #if the simplex is a triangle, calculate the barycentric coordinates of the
    #point in the triangle
    if len(simplex) == 3:
        #get coordinates from vertices of simplex
        triangle_coordinates = [coordinates[i] for i in simplex]
        (x_0, y_0), (x_1, y_1), (x_2, y_2) = triangle_coordinates

        #find each of the three weights
        lambda_0 = ((y_1-y_2)*(point[0]-x_2)+(x_2-x_1)*(point[1]-y_2)) / \
                            ((y_1-y_2)*(x_0-x_2)+(x_2-x_1)*(y_0-y_2))
        lambda_1 = ((y_2-y_0)*(point[0]-x_2)+(x_0-x_2)*(point[1]-y_2)) / \
                            ((y_1-y_2)*(x_0-x_2)+(x_2-x_1)*(y_0-y_2))
        lambda_2 = 1 - lambda_0 - lambda_1
        result = [lambda_0, lambda_1, lambda_2]

    #if the simplex is a line segment, find the proportions of each point in the line segment
    elif len(simplex) == 2:
        #since it's linear interpolation, the proportions are the same for both
        #x and y components, so we just use one of them
        x_0, x_1 = coordinates[simplex[0]][0], coordinates[simplex[1]][0]

        #find the two weights
        lambda_1 = (point[0] - x_0) / (x_1 - x_0)
        lambda_0 = 1 - lambda_1
        result = [lambda_0, lambda_1]

    else:
        raise Exception("_convert_to_barycentric: Invalid input simplex. " \
                        "This method is only defined for triangles and edges")
    return result