简体   繁体   English

迭代:从随机列表中选择一个随机元素

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

I have a tree (or "matrix") containing a bunch of line position points: 我有一棵树(或“矩阵”),其中包含一堆线位置点:

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

My goal is to connect even more lines from random points on each "branch" to other random points on separate branches. 我的目标是将更多的线从每个“分支”上的随机点连接到单独分支上的其他随机点。 Once a line is created, the position points on both branches will be unusable for future line creation. 创建线后,两个分支上的位置点将无法用于将来的线创建。

I've managed to randomize the point iteration with random.shuffle(), but I'm currently taking one branch at a time and comparing it's points with only one neighboring branch at a time. 我已经设法通过random.shuffle()随机化了点迭代,但是我现在一次只获取一个分支,并且一次只将其与一个相邻分支进行比较。 This gives me a bunch of lines connecting from one branch to the first branch it checks, but I want them all over the place, connecting to many branches. 这给了我很多线,从一个分支连接到它检查的第一个分支,但是我希望它们到处都是,并连接到许多分支。

Here's what I have so far. 到目前为止,这就是我所拥有的。 The functions are native to Cinema 4D. 这些功能是Cinema 4D固有的。 The searchable API can be found here: https://developers.maxon.net/docs/Cinema4DPythonSDK/html/index.html Function GetSplinePoint() simply returns the position of a spline (3D line) object. 可在以下位置找到可搜索的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

Got it working. 得到它的工作。

I made a new list: 我做了一个新清单:

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

then shuffled with random.shuffle(rand_splines) before my first and second iterations and used for spline in rand_splines: to iterate the shuffled list. 然后在我的第一次和第二次迭代之前用random.shuffle(rand_splines)进行混洗,并用于for spline in rand_splines:迭代混洗的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM