You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python从单坐标列表生成9个逆时针闭合环坐标子列表

解决方案

观察输入的16个坐标点,它们的x、y值分别对应4个不同的数值,刚好构成4×4的网格结构。我们可以基于这个网格,直接提取相邻的4个点组成逆时针闭合环,共9个(3×3)。

实现步骤

  1. 提取所有唯一的x、y坐标值,分别排序(x从小到大,y从大到小,确保后续生成的环是逆时针方向)。
  2. 构建坐标映射字典,快速通过(x,y)找到对应的完整3D点。
  3. 遍历网格的每个2×2子区域,按左上→左下→右下→右上的顺序取点,这个顺序天然构成逆时针闭合环。

Python代码实现

input_list = [(2282.405, -89.9415, 266.1414), (2276.534, -89.9526, 266.9091), (2276.534, -83.9573, 266.9091), (2282.405, -83.9464, 266.1414), (2288.276, -77.9407, 265.3738), (2294.148, -77.9301, 264.6062), (2294.148, -83.9247, 264.6062), (2288.276, -83.9356, 265.3738), (2282.405, -71.9563, 266.1414), (2288.276, -71.9459, 265.3738), (2282.405, -77.9514, 266.1414), (2288.276, -89.9304, 265.3738), (2276.534, -77.962, 266.9091), (2294.148, -71.9355, 264.6062), (2276.534, -71.9667, 266.9091), (2294.148, -89.9193, 264.6062)]

# 提取唯一x坐标并从小到大排序
x_coords = sorted({p[0] for p in input_list})
# 提取唯一y坐标并从大到小排序(确保逆时针方向)
y_coords = sorted({p[1] for p in input_list}, reverse=True)

# 构建(x,y)到完整点的映射
point_map = {(p[0], p[1]): p for p in input_list}

# 生成9个逆时针闭合环
quadrilaterals = []
for i in range(len(x_coords) - 1):
    for j in range(len(y_coords) - 1):
        # 按左上→左下→右下→右上的顺序取点,构成逆时针环
        top_left = point_map[(x_coords[i], y_coords[j])]
        bottom_left = point_map[(x_coords[i], y_coords[j+1])]
        bottom_right = point_map[(x_coords[i+1], y_coords[j+1])]
        top_right = point_map[(x_coords[i+1], y_coords[j])]
        quadrilaterals.append([top_left, bottom_left, bottom_right, top_right])

# 输出结果
for idx, quad in enumerate(quadrilaterals, 1):
    print(f"第{idx}个闭合环:{quad}")

验证说明

这个方法利用了点的网格分布特性,避免了复杂的距离计算和组合筛选。生成的每个子列表包含4个点,按顺序连接后自然形成逆时针闭合环,完全符合需求。

内容的提问来源于stack exchange,提问作者Suren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 13:30:53