def smoothed_joint(seg0, seg1, maxjointsize=<NUM_LIT:3>, tightness=<NUM_LIT>):
assert seg0.end == seg1.start<EOL>assert <NUM_LIT:0> < maxjointsize<EOL>assert <NUM_LIT:0> < tightness < <NUM_LIT:2><EOL><INDENT>sgn = lambda x:x/abs(x)<EOL><DEDENT>q = seg0.end<EOL>try: v = seg0.unit_tangent(<NUM_LIT:1>)<EOL>except: v = seg0.unit_tangent(<NUM_LIT:1> - <NUM_LIT>)<EOL>try: w = seg1.unit_tangent(<NUM_LIT:0>)<EOL>except: w = seg1.unit_tangent(<NUM_LIT>)<EOL>max_a = maxjointsize / <NUM_LIT:2><EOL>a = min(max_a, min(seg1.length(), seg0.length()) / <NUM_LIT:20>)<EOL>if isinstance(seg0, Line) and isinstance(seg1, Line):<EOL><INDENT>'''<STR_LIT>'''<EOL><INDENT>det = v.imag*w.real-v.real*w.imag<EOL>if det < <NUM_LIT:0>:<EOL><INDENT>b = (<NUM_LIT:6>+tightness)*a<EOL><DEDENT>elif det > <NUM_LIT:0>:<EOL><INDENT>b = (<NUM_LIT:2>-tightness)*a<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT><DEDENT>b = (<NUM_LIT:2> - tightness)*a<EOL>elbow = CubicBezier(q - a*v, q - (a - b/<NUM_LIT:3>)*v, q + (a - b/<NUM_LIT:3>)*w, q + a*w)<EOL>seg0_trimmed = Line(seg0.start, elbow.start)<EOL>seg1_trimmed = Line(elbow.end, seg1.end)<EOL>return seg0_trimmed, [elbow], seg1_trimmed<EOL><DEDENT>elif isinstance(seg0, Line):<EOL><INDENT>'''<STR_LIT>'''<EOL><INDENT>det = v.imag*w.real-v.real*w.imag<EOL><DEDENT>b = (<NUM_LIT:4> - tightness)*a<EOL><INDENT>g = sgn(det)*b<EOL><DEDENT>elbow = CubicBezier(q - a*v, q + (b/<NUM_LIT:3> - a)*v, q - b/<NUM_LIT:3>*w, q)<EOL>seg0_trimmed = Line(seg0.start, elbow.start)<EOL>return seg0_trimmed, [elbow], seg1<EOL><DEDENT>elif isinstance(seg1, Line):<EOL><INDENT>args = (seg1.reversed(), seg0.reversed(), maxjointsize, tightness)<EOL>rseg1_trimmed, relbow, rseg0 = smoothed_joint(*args)<EOL>elbow = relbow[<NUM_LIT:0>].reversed()<EOL>return seg0, [elbow], rseg1_trimmed.reversed()<EOL><DEDENT>else:<EOL><INDENT>t0 = seg0.ilength(seg0.length() - a/<NUM_LIT:2>)<EOL>t1 = seg1.ilength(a/<NUM_LIT:2>)<EOL>seg0_trimmed = seg0.cropped(<NUM_LIT:0>, t0)<EOL>seg1_trimmed = seg1.cropped(t1, <NUM_LIT:1>)<EOL>seg0_line = Line(seg0_trimmed.end, q)<EOL>seg1_line = Line(q, seg1_trimmed.start)<EOL>args = (seg0_trimmed, seg0_line, maxjointsize, tightness)<EOL>dummy, elbow0, seg0_line_trimmed = smoothed_joint(*args)<EOL>args = (seg1_line, seg1_trimmed, maxjointsize, tightness)<EOL>seg1_line_trimmed, elbow1, dummy = smoothed_joint(*args)<EOL>args = (seg0_line_trimmed, seg1_line_trimmed, maxjointsize, tightness)<EOL>seg0_line_trimmed, elbowq, seg1_line_trimmed = smoothed_joint(*args)<EOL>elbow = elbow0 + [seg0_line_trimmed] + elbowq + [seg1_line_trimmed] + elbow1<EOL>return seg0_trimmed, elbow, seg1_trimmed<EOL><DEDENT>
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.