def poly_to_rect(pos_cor, height, width):
    """
        Polygon to rectangle
    Args:
        pos_cor (list): x1, y1, x2, y2, x3, y3, x4, y4
        height (int): image height
        width (int): image width

    Returns:
        list: rect: x, y, width, height

    """
    x_1, y_1, x_2, y_2, x_3, y_3, x_4, y_4 = pos_cor

    # calculate the circumscribed rectangle coordinate
    dst_x1 = max(0, min(x_1, x_2, x_3, x_4))
    dst_y1 = max(0, min(y_1, y_2, y_3, y_4))
    dst_x2 = min(width - 1, max(x_1, x_2, x_3, x_4))
    dst_y2 = min(height - 1, max(y_1, y_2, y_3, y_4))

    dst_x, dst_y = dst_x1, dst_y1
    width = dst_x2 - dst_x1 + 1
    height = dst_y2 - dst_y1 + 1

    return [dst_x, dst_y, width, height]