[英]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.