def cutToFitIntoPolygon(line, polygon):
p0_inside = pointInsidePolygon(line[<NUM_LIT:0>], line[<NUM_LIT:1>], polygon)<EOL>p1_inside = pointInsidePolygon(line[<NUM_LIT:2>], line[<NUM_LIT:3>], polygon)<EOL>if not p0_inside or not p1_inside:<EOL><INDENT>for (i0, j0), (i1, j1) in zip(polygon[:-<NUM_LIT:1>], polygon[<NUM_LIT:1>:]):<EOL><INDENT>isec = segmentIntersection(line, (i0, j0, i1, j1))<EOL>if isec is not None:<EOL><INDENT>if not p0_inside:<EOL><INDENT>line = (isec[<NUM_LIT:0>], isec[<NUM_LIT:1>], line[<NUM_LIT:2>], line[<NUM_LIT:3>])<EOL>p0_inside = True<EOL><DEDENT>elif not p1_inside:<EOL><INDENT>line = (line[<NUM_LIT:0>], line[<NUM_LIT:1>], isec[<NUM_LIT:0>], isec[<NUM_LIT:1>])<EOL>p1_inside = True<EOL><DEDENT><DEDENT>if p0_inside and p1_inside:<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>return line<EOL>
cut line so it fits into polygon
polygon = (( x0,y0), (x1,y1) ,...)