def find_corners(refined_line_list):
    """
    Finds corners (without deciding which is which)
    :param refined_line_list:
    :return: list of corners
    """

    if len(refined_line_list) != 4:
        raise ValueError("ERROR: wrong number of lines")

    base_line = refined_line_list[0]
    i = 1
    j = -1
    max_dot_product = -1
    n = len(refined_line_list)
    while i < n:
        a1, b1, rho1 = unpack_line(base_line)
        a2, b2, rho2 = unpack_line(refined_line_list[i])
        cur_dot_product = abs(dot_product(a1, b1, a2, b2))
        if cur_dot_product > max_dot_product:
            max_dot_product = cur_dot_product
            j = i
        i += 1

    corners = []
    for l in [0, j]:
        k = 0
        while k < n:
            if k != 0 and k != j:
                a1, b1, rho1 = unpack_line(refined_line_list[l])
                a2, b2, rho2 = unpack_line(refined_line_list[k])
                try:
                    corners.append(line_instersection(a1, b1, rho1, a2, b2, rho2))
                except ValueError:
                    #should not happen
                    print("ERROR: parallel lines passed through postprocessor")
                    corners.append((0, 0))
            k += 1

    return corners
def splice_lines(hough_line_list, x_window_size, y_window_size, x_pt, y_pt, parallel_lines_margin, dot_product_threshold):
    """
    Splices close lines in region into one line
    :param hough_line_list:
    :param window_size:
    :param x_pt:
    :param y_pt:
    :param parallel_lines_margin:
    :param dot_product_threshold:
    :return: list of four lines
    """
    refined_line_list = []
    i = 0
    n = len(hough_line_list)
    #Iterates through lines
    while i < n:
        line1 = hough_line_list[i]
        a1, b1, rho1 = unpack_line(line1)
        j = i + 1
        #Iterates through the rest of lines trying to find nearly parallel lines
        while j < n:
            line2 = hough_line_list[j]
            a2, b2, rho2 = unpack_line(line2)
            try:
                x, y = line_instersection(a1, b1, rho1, a2, b2, rho2)
            except ValueError as e:
                if str(e) == "Overlap":
                    del hough_line_list[j]
                    n -= 1
                    continue
                if str(e) == "Parallel":
                    if abs(rho2 - rho1) < parallel_lines_margin:
                        del hough_line_list[j]
                        n -= 1
                        continue
                    else:
                        j += 1
                        continue
            #Lines intersect in rectangle and almost parallel
            if x_pt <= x <= x_pt + x_window_size and y_pt <= y <= y_pt + y_window_size and abs(dot_product(a1, b1, a2, b2)) > dot_product_threshold:
                mean_weight = (line1[2] + line2[2])/2
                spliced_line = splice_line(a1, b1, rho1, a2, b2, rho2, mode="grad")
                line1 = (spliced_line[0], spliced_line[1], mean_weight)
                del hough_line_list[j]
                n -= 1
            else:
                j += 1
        refined_line_list.append(line1)
        i += 1
    return refined_line_list