簡體   English   中英

迭代:從隨機列表中選擇一個隨機元素

[英]iteration: select a random element from random list

我有一棵樹(或“矩陣”),其中包含一堆線位置點:

tree = [[[x,y],[x,y],[x,y]],
    [[x,y],[x,y],[x,y]],
    [[x,y],[x,y],[x,y]]]

我的目標是將更多的線從每個“分支”上的隨機點連接到單獨分支上的其他隨機點。 創建線后,兩個分支上的位置點將無法用於將來的線創建。

我已經設法通過random.shuffle()隨機化了點迭代,但是我現在一次只獲取一個分支,並且一次只將其與一個相鄰分支進行比較。 這給了我很多線,從一個分支連接到它檢查的第一個分支,但是我希望它們到處都是,並連接到許多分支。

到目前為止,這就是我所擁有的。 這些功能是Cinema 4D固有的。 可在以下位置找到可搜索的API: https : //developers.maxon.net/docs/Cinema4DPythonSDK/html/index.html函數GetSplinePoint()僅返回樣條線(3D線)對象的位置。

rand_pts = [_ for _ in xrange(spline_resolution)]        
for p_spline in xrange(len(splines)):
    random.shuffle(rand_pts)
    for p in rand_pts:
        if p == 0 or p == spline_resolution-1 or occupied[p_spline][p] == True: continue
        mindist = 99999
        new_connection = False
        p_amt = c4d.utils.RangeMap(p, 0, spline_resolution-1, 0, 1, False)
        p_pt = spline_objs[p_spline].GetSplinePoint(p_amt)
        for n_spline in xrange(len(splines)):
            if n_spline != p_spline:
                random.shuffle(rand_pts)
                for n in rand_pts:
                    if n == 0 or n == spline_resolution-1 or occupied[n_spline][n] == True: continue
                    n_amt = c4d.utils.RangeMap(n, 0, spline_resolution-1, 0, 1, False)
                    n_pt = spline_objs[n_spline].GetSplinePoint(n_amt)
                    dist = (p_pt - n_pt).GetLength()
                    if dist < op[c4d.ID_USERDATA,5] and dist < mindist:
                        mindist = dist
                        new_connection = True
                        break

得到它的工作。

我做了一個新清單:

rand_splines = [_ for _ in xrange(len(splines))]

然后在我的第一次和第二次迭代之前用random.shuffle(rand_splines)進行混洗,並用於for spline in rand_splines:迭代混洗的列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM