import math


def xy_intersect_of_polar_coords(rho1, theta1, rho2, theta2):
    """(x, y) coordinates of 2 lines defined by their parameters in polar coordinates

    @param rho1: distance from origin to line 1
    @param theta1: angle in radians between horizontal line and perpendicular to line 1
    @param rho2: distance from origin to line 2
    @param theta2: angle in radians between horizontal line and perpendicular to line 2

    @note: This function assumes that the lines cross and doesn't take special
    care to be numerically stable.  Its intended usage is to straighten a
    rectangle that has only be slightly warped by the perspective of the
    camera taking its picture.

        >>> xy_intersect_of_polar_coords(0, 0, 0, math.pi / 2)
        (0.0, 0.0)
        >>> xy_intersect_of_polar_coords(0, math.pi/4, math.sqrt(2), - math.pi / 4)
        (1.0, -1.0000000000000002)
    """
    # Represent line 1 as a*x + b*y = e
    a = math.cos(theta1)
    b = math.sin(theta1)
    e = rho1
    # Represent line 2 as c*x + d*y = f
    c = math.cos(theta2)
    d = math.sin(theta2)
    f = rho2
    # These two lines cross in x, y where x, y is the solution of the 2 linear
    # equations defined above.
    det = a * d - b * c
    x = (d * e - b * f) / det
    y = (a * f - c * e) / det
    return x, y