您的两个外部循环正在创建列表的 组合 。为这些使用itertools.combinations()
迭代器。您最里面的双循环会产生笛卡尔积 ,因此请使用itertools.product()
迭代器。
不要使用range(), just loop directly over the polygon lists; use
enumerate()来生成索引以添加索引,而不要使索引相反。
到配对部分,该pairwise()
配方从itertools
食谱部; 这样您就可以使用所有细分。要再次从头开始绕圈(将最后一个坐标与第一个坐标配对),只需将列表的第一个元素附加到末尾即可。
一旦摆脱了嵌套循环,就可以使用break
结束循环而不是使用标志变量。
from itertools import combinations, product
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
for (i, a_poly), (j, b_poly) in combinations(enumerate(polygons), 2):
for a in a_poly:
if isInside(a.x, a.y, b_poly):
union(i, j)
for b in b_poly:
if isInside(b.x, b.y, a_poly):
union(j, i)
# attach the first element at the end so you go 'round'
a_segments = pairwise(a_poly + a_poly[:1])
b_segments = pairwise(b_poly + b_poly[:1])
for a_seg, b_seg in product(a_segments, b_segments):
if doIntersect(*a_seg, *b_seg):
union(i,j)
break
实际上,一旦确定某个东西是一个并集,就不必继续进行其余的测试。您可以使用any()
功能停止测试isInside()
和doIntersect
早期的功能:
for (i, a_poly), (j, b_poly) in combinations(enumerate(polygons), 2):
if any(isInside(a.x, a.y, b_poly) for a in a_poly):
union(i, j)
break # union found, no need to look further
for any(isInside(b.x, b.y, a_poly) for b in b_poly):
union(i, j)
break # union found, no need to look further
# attach the first element at the end so you go 'round'
a_segments = pairwise(a_poly + a_poly[:1])
b_segments = pairwise(b_poly + b_poly[:1])
if any(doIntersect(*a_seg, *b_seg)
for a_seg, b_seg in product(a_segments, b_segments)):
union(i,j)
这不仅现在可读性强,而且还应该更有效!