C# code for creating a passage between two closed curves

Hi All,

I have been trying to create a passage between two closed curves at the their closest point and join it into one big continuous curve (I have many curve pairs to run this on) using c# script.
I identified the points using Curve.Closestpoints and drew a line and offset that line by passage width. but I am now not sure how to proceed to remove the curve between lines and connect the curves at the end and start points.

Could somebody suggest any approach!?

This method will give you an array of shattered curves. you can pick the one you need from them.


This is complex (general case).

For a start use a double loop (Note: a more advanced way is to cluster your Curves by poximity):

for(int i = 0; i<cList.Count;i++){
Curve c1 = cList[i];
for(int j = i+1; j<cList.Count;j++){
Curve c2 = cList[j];

Point3d c1p, c2p; c1.ClosestPoints(c2, out c1p, out c2p);
double t1; c1.ClosestPoint(c1p, out t1);
double t2; c2.ClosestPoint(c2p, out t2);

Then test if the LineCurve (cp1, cp2) yields ccx events with any Curve in the cList (if you skip this … things become quite challenging). If events == null then sample the LineCurve in a Tree (where the dims are i,j and j,i) and the t1,2 values in another Tree.

Then post your C# and we’ll see ways to do the bridge and finish the job (but as I said the general case is complex).


See an example with regard the bridge axis on Circles (where proximity on centers is used [and Point3dList]):

An another with crv/crv proximity (spot that you’ll require another flag as well: accept or reject ccx events between the connections):

So the axis are the easy part … but what happens if a bridge “affects” other curves as well? That’s the reason that this is a bit complex.

Thank you @PeterFotiadis for your inputs.

I am attaching the c# (testing model) where I take one curve as base/parent curve to identify the closest curves around it within the tolerance.

I am trying to convert it into a single curve with minimum possible passages from each curve.(i.e each curve should only have one parent curve but can have multiple child curves.)
But I am confused on how to iterate through the next generation child curves each time.
also I have to trim the curves between bridge lines and connect them. couldn’t find the function similar to trim with region in c#.

limit.curve_passages.gh (6.2 KB)

Well … truth is that I’m in vacations and thus I don’t have access on my full arsenal of C#'s (but I do have numerous screenshots … that are not exactly helpfull for the occasion).

Later in the evening I’ll take a look on your stuff (cross fingers that the laptop charger works).

Have in mind that this layout captured is “easy”. But chances are that this task could become quite challenging for the general case (any collection that may contain curves with self ccx events, crv/crv ccx events etc etc).

Iit’s not fully done in C#. I have chated a bit and did a part of the operation (For trimming and unifying boundaries in Grasshopper). But you can take a look at code and see how I filtered the irrelevant roads. I’m sure @PeterFotiadis will give you enough information for fully scripting in C#.
Roads.gh (13.5 KB)

Hi @mahanmotamedi1991, thank you that was really informative. Although my target is to create a path where I can draw the entire end curve without lifting up the pen (hence the parent child rule i mentioned before). For that I believe phase of joining curves has to happen inside the c# script to run iterations for next curve in proximity.

Me too (I was told that there’s no beach party scheduled for yesterday … but you know: these are Karma related things). Hence the delay.

On less serious matters:

Your problem is 2 problems: (a) deal with proximity, (b) do something as a post action.

As I said the general case of that (any collection, curvy bridges, elaborated filtering rules [for the connections] etc etc) is complex and is far and away from the scope of this Forum (i.e. abstract help on Academic style problems).


For the proximity … get a genuine post_beach_party C# done this morning. May have some(?) issues (hangover and the likes). This is the by the book (via a custom class etc etc) way to deal with proximity matters. Note the way that connections are done (they are implicitly accessed via indices in their List).

Remember: connectivity is everything, geometry is nothing:

The other C# is written in a great hurry since there’s wind meaning is windsurfing time > I hear the Call > adios amigos.

More soon(?)

Curve_Proximity_BeachInTheMiddleOfNowhereVersion_V1.gh (150.1 KB)

And the post_post_beach_party trad update:

Curve_Proximity_BeachInTheMiddleOfNowhereVersion_V1A.gh (166.8 KB)

1 Like

Thank you @PeterFotiadis that helped.

Hi @PeterFotiadis

While I try to connect curves, createbooleanUnion takes very long especially while dealing with many curves.
I tried the code you have sent. But as I mentioned before I am trying to get a continuous path through all the curves using bridges(like without skipping or crossing ).

I tried the below way, but trimming seems to be tricky(see the left out curve).
In that code. once I get the parameters at intersection events. I want to compare them and trim the smaller part. but calculation of the length in reverse order is not possible. Any suggestions?

Curve_Passage.gh (9.5 KB)

I’ll give your stuff a spin later.

Other than that as I said - several times - the general case of this is complex. PS you can skip the connections crossing (is an option) in the latest posted C# (V1A).

If you do that type of work quite often and you have the C# skills maybe is worth spending several hours (worst case scenario) in order to achieve a fast and always working solution: is just a matter of filtering what connection to accept or reject (see the Method that does that in V1A with regard the options present).

If on the other hand this is an one-off do it manually: coding is the art of knowing when not to do things.

PS: Your goal is in fact a variation of clustering on conn Tree (i.e.given the connectivity ensure that there’s a traveling salesman (so to speak) routing/path possible [and discard the other connections - in this case set proxCount to 1]).

That still would leave duplicate routes.

My idea (or trick) is to check for one closest curve in each iteration and join these two curves into one through bridge, then proceed to find closest curve to the combined curve and so on. This was it can be ensured that there is always a single continuous closed path to finish the simulated drawing. But as the curve gets bigger booleanUnion takes longer, making it difficult as I have several layers of such planar curves to connect.

Just for a bit of fun - a physical method
shrink_around_curves.gh (21.3 KB)

(similar idea to this video on solving the motorway problem with soap films)


If you mean that node for curve pair i, j is the same as the node for curve pair j, i then yes (but what type of argument is this?).

Other than that … your particular take on this proximity thingy IS a traveling salesman “version” of routing. I have at least 10 C#'s in the practice that do that very very fast … but I’m 1M miles away from base (and chances to return soon [if ever] are null)

BTW: the 2nd C# provided is a very naive take on the problem: because is using bool and the likes (1 minute solution [blame the standard hangover as well] but slow) . But since you have connectivity on hand (via the conn Tree) there’s the obvious very fast analytical way as well (trim the curve pair involved against the bridge > join the 4 pieces > … (or join all when the trim is over)).

is there any curve/region trimming option(like in grasshopper) in c#?. I could only see that curve can be trimmed over an interval.

GH is using solely R Methods for that type of stuff and C# has nothing to do with Geometry things. See SDK (use Trim or Split [whatever suits you best]):

Intervals are derived from t1,t2 due to ccx events (curve, bridge),

BTW: Since the adjacency Matrix is on hand (what curve is connected to what) via the conn Tree (you can also do a dist Matrix), here’s a small challenge for you: write a C# that does that:

Using Matrices And Hungarian Method To Solve The Traveling Salesm.pdf (234.3 KB)