Ok, I guess you need a bit more explanation.
Simple algo:
First off, write a function that splits a convex polygon by an arbitrary line. This can be done very simply with one iteration over the points of the polygon. It returns one or two polygons, depending on if there was a successful split.
Then start with a list of size one containing the starting square. For each intersecting line, split every polygon by it, and put the results together into the next list to be processed.
Finally, pick the largest polygon in the list.
l = [starting_square]
for line in intersecting_lines:
new_list = []
for poly in l:
new_list.extend(split_poly(poly, line))
l = new_list
# Find max in list:
max_poly = max(l, key=poly_area)
Advanced algo (using BSPs):
The former will run really slowly if you have a lot of intersecting lines. To avoid this, we construct a tree as we go. Each node has a polygon associated with it and two or zero children. We ensure that the polygons of the children are a complete partition of of their parent.
Start with the tree of one node, the starting square, then split the entire tree by each intersecting line. It is quicker to split the tree than the original list, because we can skip many leaf nodes at once if their common ancestor doesn't intersect with the line.
def split_polygon(polygon, line):
# 2 polygons if split,
# Otherwise, returns None and the original polygon,
# In an order indicating which side of the line the polygon is on
...
def make_node(poly, child_a, child_b):
if child_a is not None:
if child_b is not None:
return Node(poly, child_a, child_b)
else:
return child_a
else:
if child_b is not None:
return child_b
else:
assert False
def split_tree(node, line):
# 2 nodes if split,
# Otherwise, returns None and the original nodes ,
# In an order indicating which side of the line the polygon is on
poly1, poly2 = split_polygon(node.poly, line)
if poly1 is None:
return None,node
if poly2 is None:
return node, None
if node.is_leaf
return Node(poly1), Node(poly2)
node1_a, node2_a = split_tree(node.child_a)
node1_b, node2_b = split_tree(node.child_b)
node1 = make_node(poly1, node1_a, node1_b)
node2 = make_node(poly2, node2_a, node2_b)
return node1, node2
tree = Node(starting_square)
for line in intersection_lines:
node1, node2 = split_tree(tree, line)
tree = make_node(tree.poly, node1, node2)
# Find max in tree:
def find_max(node):
if node.is_leaf:
return node.poly
poly_a = find_max(node.child_a)
poly_b = find_max(node.child_b)
return max(poly_a, poly_b, key=poly_area)
max_poly = find_max(tree)