I have a set of 1 * 1 polygons, each of which is determined by its border (a set of four points), and I use the area () function below in my code example to create these. I want to combine such adjacent squares into one polygon, and this polygon is also defined in terms of its boundary points.
I want to do it in brute force when I start by adding two adjacent 1 * 1 squares to create a larger polygon using the join () function below and continuing in such a way as to grow the polygon. So the first argument of the union is the polygon, and the second argument is the adjacent 1 * 1 square that I want to add to the polygon. The return value is the boundary of the new polygon, current , associated with the new 1 * 1.
Here is what I have come so far:
def join(current, new): """ current is the polygon, new the 1*1 square being added to it""" return get_non_touching_part_of_boundary(current, new) + get_non_touching_part_of_boundary(new, current) def get_non_touching_part_of_boundary(this, other): for i,point in enumerate(this): if point not in other and this[i-1] in other: break
This gives me the following polygon shape (with numbers representing the order in which its composite squares are added):
234 1 5 6
The printed output of the above code gives:
[(2, 2), (1, 2), (1, 1), (0, 1), (0, 3), (3, 3), (3, 0), (2, 0)]
this is the minimum number of points needed to determine the boundary of a polygon.
But if I add another square to this form via a = join (a, area ((1,0))) and thereby create a hole, my algorithm breaks into pieces:
234 1 5 76
Here is another polygon that my algorithm cannot handle:
123 64 5
Can anyone help me? I would like the holes in the polygon to be listed on a separate list.
Thanks!