def smoothed_joint(seg0, seg1, maxjointsize=3, tightness=1.99):
    """ See Andy's notes on
    Smoothing Bezier Paths for an explanation of the method.
    Input: two segments seg0, seg1 such that seg0.end==seg1.start, and
    jointsize, a positive number

    Output: seg0_trimmed, elbow, seg1_trimmed, where elbow is a cubic bezier
        object that smoothly connects seg0_trimmed and seg1_trimmed.

    """
    assert seg0.end == seg1.start
    assert 0 < maxjointsize
    assert 0 < tightness < 2
#    sgn = lambda x:x/abs(x)
    q = seg0.end

    try: v = seg0.unit_tangent(1)
    except: v = seg0.unit_tangent(1 - 1e-4)
    try: w = seg1.unit_tangent(0)
    except: w = seg1.unit_tangent(1e-4)

    max_a = maxjointsize / 2
    a = min(max_a, min(seg1.length(), seg0.length()) / 20)
    if isinstance(seg0, Line) and isinstance(seg1, Line):
        '''
        Note: Letting
            c(t) = elbow.point(t), v= the unit tangent of seg0 at 1, w = the
            unit tangent vector of seg1 at 0,
            Q = seg0.point(1) = seg1.point(0), and a,b>0 some constants.
            The elbow will be the unique CubicBezier, c, such that
            c(0)= Q-av, c(1)=Q+aw, c'(0) = bv, and c'(1) = bw
            where a and b are derived above/below from tightness and
            maxjointsize.
        '''
#        det = v.imag*w.real-v.real*w.imag
        # Note:
        # If det is negative, the curvature of elbow is negative for all
        # real t if and only if b/a > 6
        # If det is positive, the curvature of elbow is negative for all
        # real t if and only if b/a < 2

#        if det < 0:
#            b = (6+tightness)*a
#        elif det > 0:
#            b = (2-tightness)*a
#        else:
#            raise Exception("seg0 and seg1 are parallel lines.")
        b = (2 - tightness)*a
        elbow = CubicBezier(q - a*v, q - (a - b/3)*v, q + (a - b/3)*w, q + a*w)
        seg0_trimmed = Line(seg0.start, elbow.start)
        seg1_trimmed = Line(elbow.end, seg1.end)
        return seg0_trimmed, [elbow], seg1_trimmed
    elif isinstance(seg0, Line):
        '''
        Note: Letting
            c(t) = elbow.point(t), v= the unit tangent of seg0 at 1,
            w = the unit tangent vector of seg1 at 0,
            Q = seg0.point(1) = seg1.point(0), and a,b>0 some constants.
            The elbow will be the unique CubicBezier, c, such that
            c(0)= Q-av, c(1)=Q, c'(0) = bv, and c'(1) = bw
            where a and b are derived above/below from tightness and
            maxjointsize.
        '''
#        det = v.imag*w.real-v.real*w.imag
        # Note: If g has the same sign as det, then the curvature of elbow is
        # negative for all real t if and only if b/a < 4
        b = (4 - tightness)*a
#        g = sgn(det)*b
        elbow = CubicBezier(q - a*v, q + (b/3 - a)*v, q - b/3*w, q)
        seg0_trimmed = Line(seg0.start, elbow.start)
        return seg0_trimmed, [elbow], seg1
    elif isinstance(seg1, Line):
        args = (seg1.reversed(), seg0.reversed(), maxjointsize, tightness)
        rseg1_trimmed, relbow, rseg0 = smoothed_joint(*args)
        elbow = relbow[0].reversed()
        return seg0, [elbow], rseg1_trimmed.reversed()
    else:
        # find a point on each seg that is about a/2 away from joint.  Make
        # line between them.
        t0 = seg0.ilength(seg0.length() - a/2)
        t1 = seg1.ilength(a/2)
        seg0_trimmed = seg0.cropped(0, t0)
        seg1_trimmed = seg1.cropped(t1, 1)
        seg0_line = Line(seg0_trimmed.end, q)
        seg1_line = Line(q, seg1_trimmed.start)

        args = (seg0_trimmed, seg0_line, maxjointsize, tightness)
        dummy, elbow0, seg0_line_trimmed = smoothed_joint(*args)

        args = (seg1_line, seg1_trimmed, maxjointsize, tightness)
        seg1_line_trimmed, elbow1, dummy = smoothed_joint(*args)

        args = (seg0_line_trimmed, seg1_line_trimmed, maxjointsize, tightness)
        seg0_line_trimmed, elbowq, seg1_line_trimmed = smoothed_joint(*args)

        elbow = elbow0 + [seg0_line_trimmed] + elbowq + [seg1_line_trimmed] + elbow1
        return seg0_trimmed, elbow, seg1_trimmed
See Andy's notes on
    Smoothing Bezier Paths for an explanation of the method.
    Input: two segments seg0, seg1 such that seg0.end==seg1.start, and
    jointsize, a positive number

    Output: seg0_trimmed, elbow, seg1_trimmed, where elbow is a cubic bezier
        object that smoothly connects seg0_trimmed and seg1_trimmed.