亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python 點集排序之帶索引的Z字形排序算法實現(xiàn)代碼

 更新時間:2025年01月22日 16:48:52   作者:hmywillstronger  
這篇文章介紹了如何使用Python在Grasshopper中實現(xiàn)點集排序功能,包括點的Y坐標分組和X坐標排序,以及追蹤每個點的原始索引位置,通過創(chuàng)建點索引對、分組邏輯和排序,實現(xiàn)了Z字形排序算法,感興趣的朋友一起看看吧

Grasshopper Python點集排序:帶索引的Z字形排序算法

1. 功能介紹

這段代碼實現(xiàn)了一個在Grasshopper中的點集排序功能,不僅可以將空間中的點按照Y坐標分組并在每組內按X坐標排序,還能追蹤每個點的原始索引位置。

2. 輸入輸出參數(shù)

  • 輸入?yún)?shù):
    • x: 待排序的點集(Point List)
    • t: 容差值(Number),默認2000
  • 輸出參數(shù):
    • a: 排序后的點集(Point List)
    • i: 排序后點的原始索引(Number List)

3. 核心算法流程

4. 代碼解析

4.1 點索引對的創(chuàng)建和處理

points_with_index = list(enumerate(x))
  • 使用enumerate()創(chuàng)建(索引, 點)對
  • 將每個點與其原始位置綁定

4.2 分組函數(shù)

def groupPointsByY(points_with_index, tolerance):
    points_with_index = sorted(points_with_index, key=lambda pair: pair[1].Y)
    groups = []
    current_group = [points_with_index[0]]
    current_y = points_with_index[0][1].Y
  • 函數(shù)接收點索引對和容差值
  • 使用lambda函數(shù)訪問點的Y坐標進行排序
  • pair[1]訪問點對象,pair[0]訪問索引

4.3 分組邏輯

for p in points_with_index[1:]:
    if abs(p[1].Y - current_y) <= tolerance:
        current_group.append(p)
    else:
        groups.append(current_group)
        current_group = [p]
        current_y = p[1].Y
  • 遍歷點索引對
  • 基于Y坐標差值分組
  • 保持索引與點的關聯(lián)

4.4 排序和結果提取

for group in grouped_points:
    group_sorted = sorted(group, key=lambda pair: pair[1].X)
    for index, point in group_sorted:
        sorted_points.append(point)
        sorted_indices.append(index)
  • 組內按X坐標排序
  • 分別提取點和索引
  • 維護排序后的兩個列表

5. Python語法要點

5.1 元組拆包

for index, point in group_sorted:
  • 直接將元組拆分為兩個變量
  • 簡化數(shù)據(jù)訪問

5.2 Lambda表達式

key=lambda pair: pair[1].X
  • 用于定義排序鍵函數(shù)
  • 訪問元組中點對象的坐標

5.3 列表操作

sorted_points.append(point)
sorted_indices.append(index)
  • 使用append()逐個添加元素
  • 維護兩個平行列表

6. 數(shù)據(jù)結構

6.1 點索引對

(index, point) 結構:
- index: 原始位置
- point: 點對象
  - X: X坐標
  - Y: Y坐標
  - Z: Z坐標

6.2 分組結構

groups = [
    [(index1, point1), (index2, point2), ...],  # 第一組
    [(index3, point3), (index4, point4), ...],  # 第二組
    ...
]

到此這篇關于Python 點集排序之帶索引的Z字形排序算法的文章就介紹到這了,更多相關Python Z字形排序算法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論