def intersect(r1, z1, r2, z2, closed1=True, closed2=True):
    """Test if two polynomials intersect. The polynomials consist of
    (r1, z1) and (r2, z2) line segments. All inputs are expected to be lists.

    Returns True or False.
    """
    assert len(r1) == len(z1)
    assert len(r2) == len(z2)
    n1 = len(r1) if closed1 else len(r1) - 1
    n2 = len(r2) if closed2 else len(r2) - 1
    for i in range(n1):
        for j in range(n2):
            ip = (i + 1) % n1
            jp = (j + 1) % n2
            a = r1[ip] - r1[i]
            b = r2[jp] - r2[j]
            c = z1[ip] - z1[i]
            d = z2[jp] - z2[j]
            dr = r2[jp] - r1[i]
            dz = z2[jp] - z1[i]
            det = a * d - b * c
            if abs(det) < 1e-06:
                continue
            alpha = (d * dr - b * dz) / det
            beta = (a * dz - c * dr) / det
            if (alpha > 0.0) & (alpha < 1.0) & (beta > 0.0) & (beta < 1.0):
                return True
    return False
