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