def intersection(line1, line2):
        x1, y1, x2, y2 = line1
        x3, y3, x4, y4 = line2

        a, b = x2 - x1, y2 - y1
        c, d = x3 - x1, y3 - y1
        e, f = x4 - x1, y4 - y1

        t1 = c * b - a * d
        t2 = e * b - a * f

        if t1 * t2 >= 0:
            return 0

        g, h = x4 - x2, y4 - y2
        i, j = x4 - x3, y4 - y3

        t1 = e * j - i * f
        t2 = g * j - i * h

        if t1 * t2 >= 0:
            return 0
        
        denom = a * j - b * i
        ynum = b * c * j + a * j * y1 - b * i * y3
        xnum = a * (c * j - d * i) + x1 * denom
        g = pef.ggcd((denom, ynum, xnum))
        return (ynum/g, xnum/g, denom/g)
    def rgen(m, n):
        """
        Using co-prime pairs to generate primitive Eisenstein triplets.
        See https://en.wikipedia.org/wiki/Integer_triangle
        """

        if m > 2 * n:
            a, b = 2*m*n - n*n, m*m - n*n
        else:
            a, b = m*m - n*n, 2*m*n - n*n

        g = pef.ggcd((a, b, m*m - m*n + n*n))
        return g, sqrt(3) / 2 * (m-n) * n / g