C# -GH: trying to make a tree with connected lines in each branch

I want to sort every line that is Connected by given node into one branch in a tree. I managde to make one list for one node, but I want to make a tree With all Connected lines.
First Picture shows the code that Works for one node. The next code is what I have tried, but it does not work… Does anybody knows why? The last Picture is the structure With lines.


I didn’t test-run this code, but it should work:

var myTree = new DataTree<Line>();

// No traversing of the empty tree, just add Lines to it:

for (var i=0; i<iPoint.Count; i++)
    var pt = iPoint[i];

    // use i-index to get things grouped/branched by the point
    var path = new GH_Path(i); 
    foreach (var line in iLines)
          if (line.To == pt || line.From == pt) // * see comment below
               myTree.Add(line, path);

B = myTree;

Hope this helps.

// Rolf

Edit: It is considered safer to compare points and doubles with “EqualsEpsilon” due to rounding errors etc., so your last check could look like this instead:

if (pt.EpsilonEquals(line.To, double.Epsilon) || 
    pt.EpsilonEquals(line.From, double.Epsilon))
    myTree.Add(line, path);
1 Like

It worked!!
thank u so much!
but it a problem, it collects the lineconnection of a point several times… Do you know why? it seems like it goes over same node/point several times…

No, it doesn’t go over the same node (point) twice, but it finds the same Line twice (from both ends)… This is due to a line having two ends (points) :wink:

You can skip the checking of one end of the lines to avoid this problem. Depending on the direction of the lines that will do what you want. (if the directions are scrambled, then it will take some extra coding to avoid this)

// Rolf

thats the problem! the ends are random… is the coding very hard than?

Can you post the lines, or the definition producing the lines and points? Whithout the data I can’t know what it takes to fix this. A “visited” list perhaps would be a strategy, but without the data I can’t tell.

// Rolf

here is the rhino file:
Globalstruc_Rhino.3dm (32.7 KB)

here is th GH:Global_structure_intersection.gh (91.0 KB)

Here is a better GH file:

Sorrry if it is a mess, but the Group in Purple is the code!

Global_structure_intersection2.gh (91.7 KB)