欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

Grasshopper Python點(diǎn)集排序:帶索引的Z字形排序算法

1. 功能介紹

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

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

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

3. 核心算法流程

4. 代碼解析

4.1 點(diǎn)索引對(duì)的創(chuàng)建和處理

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

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ù)接收點(diǎn)索引對(duì)和容差值
  • 使用lambda函數(shù)訪問點(diǎn)的Y坐標(biāo)進(jìn)行排序
  • pair[1]訪問點(diǎn)對(duì)象,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
  • 遍歷點(diǎn)索引對(duì)
  • 基于Y坐標(biāo)差值分組
  • 保持索引與點(diǎn)的關(guān)聯(lián)

4.4 排序和結(jié)果提取

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)
  • 組內(nèi)按X坐標(biāo)排序
  • 分別提取點(diǎn)和索引
  • 維護(hù)排序后的兩個(gè)列表

5. Python語(yǔ)法要點(diǎn)

5.1 元組拆包

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

5.2 Lambda表達(dá)式

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

5.3 列表操作

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

6. 數(shù)據(jù)結(jié)構(gòu)

6.1 點(diǎn)索引對(duì)

(index, point) 結(jié)構(gòu):
- index: 原始位置
- point: 點(diǎn)對(duì)象
  - X: X坐標(biāo)
  - Y: Y坐標(biāo)
  - Z: Z坐標(biāo)

6.2 分組結(jié)構(gòu)

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

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

相關(guān)文章

最新評(píng)論