Other than that a few notes on recursion (single or nested):
ALWAYS mind the termination: meaning that (until you get fully the gist) in tandem with your exit clause … use a PlanB as well: like some loop counter: this can save you a lot of frustration and a halted PC.
ALWAYS use as much public declarations as possible: avoiding the locomotive effect when calling the recursion Method(s).
Monitor your results in a tree: i.e. sample results in a branch per loop and read the previous branch, that way you can trace back the “history” of the loops (or “animate” them if you like)
I can provide you a ton of C#'s that do recursion (from simple to very complex) … but for the start describe explicitly the task that you are after and a start-up C# could be yours in no time (containing 3 as above).
Sorted by intersection.
First all LineEndPoint Intersection and second all lines with intersection between LineEndPoints.
The goal is to calculate all weigth points for a booth rig like Dancergraham mentioned in this post Result forces on a grid.
This represents the rig above the booth where all light instalations are fixed.
This is the code to get the curve order (thats what i try to make in c#)
import Rhino as rh
C = [] # Sorted rig
# Order to calculate
def calculation_order(x):
if len(x) >= 0:
for j in x:
L = []
for e in x:
if e != j:
CrvEnd = rh.Geometry.CurveEnd.Both
ej = rh.Geometry.Curve.Trim(j,CrvEnd,2)
#E.append(ej)
L.append(rh.Geometry.Curve.ClosestPoint(ej,e.PointAtStart,1)[0])
L.append(rh.Geometry.Curve.ClosestPoint(ej,e.PointAtEnd,1)[0])
if any(L) == False:
C.append(j)
x.remove(j)
return calculation_order(x)
calculation_order(x)
And this is the code to calculate the weight for all endpoints.
Only the endpoints with a higher count than 1 are calculatet.
import Rhino as rh
L = []
D = {}
for i in C:
SP,EP = i.PointAtStart,i.PointAtEnd
L.append(SP)
L.append(EP)
D[SP] = []
D[EP] = []
J = []
for i in range(0,len(C)):
# create Start and Endpoint weights for all rig parts
SP,EP = C[i].PointAtStart,C[i].PointAtEnd
weight = (rh.Geometry.Curve.GetLength(C[i])*z)/2
D[SP].append(weight)
D[EP].append(weight)
for j in D:
if rh.Geometry.Curve.GetLength(C[i]) > rh.Geometry.Curve.ClosestPoint(C[i],j,1)[1] > 0:
print i
SP_factor = rh.Geometry.Point3d.DistanceTo(SP,j)/rh.Geometry.Curve.GetLength(C[i])
EP_factor = 1 - SP_factor
SP_weight = sum(D.get(j))*EP_factor
EP_weight = sum(D.get(j))*SP_factor
D[SP].append(SP_weight)
D[EP].append(EP_weight)
sum(D.get(SP))
sum(D.get(EP))
J.append(j)
D1 = {}
SL = (set(L))-(set(J))
for i in SL:
if i in D:
D1[i] = sum(D[i])
Dv = D1.values()
Dk = D1.keys()