如何将混合列表中的多个非重叠多边形顶点坐标拆分为独立列表
多混合多边形顶点列表拆分方案
适用前提
当前方案完全匹配你的场景特征:
- 待拆分的多个多边形互相无重叠、无相交
- 单个多边形为闭合结构,顶点序列首尾坐标完全相等
- 不同多边形的顶点在总列表中连续存储,无穿插混排
实现方案(Python)
核心逻辑
遍历总坐标列表,每次遇到和当前多边形起始点一致的坐标时,即判定当前多边形闭合,将该段序列存入结果集后,从下一个坐标开始构建新的多边形即可。
代码实现
def split_polygons(coords_list): polygons = [] n = len(coords_list) # 合法多边形至少需要3个顶点,长度不足直接返回空 if n < 3: return polygons idx = 0 while idx < n: start_point = coords_list[idx] current_poly = [start_point] idx += 1 # 检索当前多边形的闭合结束点 while idx < n: current_point = coords_list[idx] current_poly.append(current_point) if current_point == start_point: # 仅保留顶点数符合要求的合法多边形 if len(current_poly) >= 3: polygons.append(current_poly) idx += 1 break idx += 1 return polygons # 代入你的示例数据测试 coordslist_of_two_polygons= [(634.196912844544, 573.5518673990564), (634.72, 596.06), (634.04, 612.57), (628.14, 636.73), (619.82, 651.76), (614.85, 661.02), (612.03, 660.08), (613.91, 653.64), (619.01, 639.95), (621.7, 631.76), (623.98, 622.64), (626.53, 608.54), (624.52, 587.34), (624.92, 585.99), (623.5164081989366, 571.5658544699229), (621.28, 571.15), (622.6759134800419, 565.013087688352), (638.1176399986085, 565.552256750043), (637.67, 570.49), (635.37, 573.77), (634.196912844544, 573.5518673990564), (605.9745784899422, 564.4299376691641), (605.05, 565.73), (602.1, 572.04), (598.34, 579.28), (594.99, 583.18), (590.02, 581.3), (593.24, 577.27), (596.5165952802347, 563.3459954757998), (598.53, 564.17), (605.9745784899422, 564.4299376691641), (634.196912844544, 573.5518673990564)] result = split_polygons(coordslist_of_two_polygons) coords_polygon1 = result[0] coords_polygon2 = result[1]
特殊场景补充
如果你的顶点是打乱混排在总列表中的,可改用连通性判断逻辑拆分:
- 任选一个未归类的顶点作为起始点
- 每次取距离当前顶点最近的未归类顶点作为下一个顶点,逐步构建序列直到回到起始点,形成闭合多边形
- 重复上述步骤直到所有顶点都被归类
内容的提问来源于stack exchange,提问作者usario121233
相关产品推荐
相关产品推荐

