Find the largest rectangle that doesn't intersect any given polygon

73 views Asked by At

I'm given an image with several (not necessarily convex) polygons defined, as well as a single point of interest.

I need to find the largest rectangle (parallel to the axes of the image - not rotated) that contains the point of interest but does not intersect any of the polygons.

If that rectangle isn't uniquely defined, I'll take any reasonable solution. In general, it's better for the rectangle to be too large than too small, and it's okay to sometimes be too large if it simplifies the code.

What algorithm can I use to do this? I'm working in Python and would like to use shapely.


My rough algorithm so far is:

xmin = 0
xmax = 1000 # Plane is 1000x1000 pixels
ymin = 0
ymax = 1000
while True:
  for each poly in polygons:
      intersection = poly.intersects(rect(xmin, xmax, ymin, ymax)
      if intersection:
           raise xmin or ymin, or lower xmax or ymax, to not include this point of intersection while still including the point of interest
  if we loop through all polygons and have no intersection, we are done    
           
1

There are 1 answers

0
FortyTwo On

To find the largest rectangle that contains a given point of interest but does not intersect any of the polygons in the image, you can use a binary search approach along with geometric operations provided by shapely in Python.

# Define the point of interest
point_of_interest = Point(x, y)  # Replace x, y with the coordinates of your point of interest

# Define the bounding box of the image
xmin, ymin, xmax, ymax = 0, 0, 1000, 1000  # Adjust these values according to your image dimensions

def is_valid_rectangle(rectangle, polygons):
    """
    Check if the given rectangle does not intersect any of the polygons.
    """
    for poly in polygons:
        if rectangle.intersects(poly):
            return False
    return True

def find_largest_rectangle(point_of_interest, polygons, xmin, ymin, xmax, ymax):
    """
    Find the largest rectangle containing the point of interest
    and not intersecting any of the polygons.
    """
    while True:
        # Check if the current rectangle is valid
        rectangle = Polygon([(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)])
        if is_valid_rectangle(rectangle, polygons):
            return rectangle

        # Binary search to adjust the boundaries of the rectangle
        mid_x = (xmin + xmax) // 2
        mid_y = (ymin + ymax) // 2

        # Determine in which direction to adjust the boundaries
        if point_of_interest.x < mid_x:
            xmax = mid_x
        else:
            xmin = mid_x
        if point_of_interest.y < mid_y:
            ymax = mid_y
        else:
            ymin = mid_y

Example usage

# List of shapely Polygon objects representing the polygons in the image
polygons = [...]

largest_rectangle = find_largest_rectangle(
    point_of_interest,
    polygons,
    xmin,
    ymin,
    xmax,
    ymax
)

This algorithm works by repeatedly subdividing the bounding box of the image using a binary search approach and checking if the resulting rectangles intersect any of the polygons. The search continues until a valid rectangle is found that contains the point of interest and does not intersect any polygons. The resulting rectangle is then returned as the largest rectangle meeting the criteria.

Make sure to replace [...] with your actual list of Polygon objects representing the polygons in the image. Additionally, adjust the xmin, ymin, xmax, and ymax values to match the dimensions of your image.