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
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.
Example usage
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 thexmin,ymin,xmax, andymaxvalues to match the dimensions of your image.