Grasshopper

algorithmic modeling for Rhino

# How to distribute serval intersect Curves into different Group with C#？

hello everyone,

this is my first post,i want to ask one question that how can i distribute serval intersect curves into different Group.

for instance,the follow picture shows that different colors represents a unique group.

this is one method,like this

but i want to use other algorithm to find intersect curves one by one with scripting,not using the Boolean calculation...How can i do this？Expect for your answer

Views: 796

Attachments:

### Replies to This Discussion

A crude way to implement this idea is to: to have an input list of curves. (List<Curve>), and have a dictionary of lists: Dictionary<List<Curve>> () (or some other nested lists of your liking, the datatree may also proof to be useful perhaps), this should have a worst-case performance of O(n^2): it will scale exponentionally with the amount of curves you add.

1. Take a curve

2. See if it intersects with one or more of the other groups.

2a. If it intersects with no group: Add a new group

2b. If it intersects with one group, add it to the group

2c. If it intersects with more then one group, merge the groups and add it to the merged group.

3. Repeat next curve, untill we've done all the curves.

A next step might be to improve this with a spatial search tree to speed things up.

Another optimization would be to treat lines as lines instead of linecurves, the intersection will be a lot faster.

--

David Rutten

david@mcneel.com

Here's a quick script that will do it. It starts with the first curve and creates a new group from it, adds its index to a temporary list, and flags the curve as being assigned. It then sets up a do loop to recurse through all curves connected to this original curve, by cycling through the rest of the curves in the list, testing for intersections with the first curve, and then adding intersecting curves both to the group and to the list of indices to be tested in the current do loop.

I am sure that there are ways to optimise this (e.g. David's suggesting to use different intersection methods according to efficiency) but it should work pretty well.

private void RunScript(List<Curve> C, ref object G)
{

DataTree<Curve> CurveGroups = new DataTree<Curve>();
bool[] Assigned = new bool[C.Count];
int PathCounter = 0;

for (int i = 0; i < C.Count; i++)
{

if (!Assigned[i])
{

List<int> GroupIndices = new List<int>();
Assigned[i] = true;

do
{
for (int j = 0; j < C.Count; j++)
{

if (j != GroupIndices[0] && !Assigned[j])
{

Rhino.Geometry.Intersect.CurveIntersections CI = Rhino.Geometry.Intersect.Intersection.CurveCurve(C[GroupIndices[0]], C[j], RhinoDoc.ActiveDoc.ModelAbsoluteTolerance, 0);

if (CI.Count > 0)
{
Assigned[j] = true;
}

}

}

GroupIndices.RemoveAt(0);

}while (GroupIndices.Count > 0);

PathCounter += 1;

}

}

G = CurveGroups;

}

Absolutely magnificent！Thanks to all of you！

Nice implementation. Small tip: Pastebin is a nice way to share code in a way that it will be readable without throwing it into an ide-like environment to format it - again:

http://pastebin.com/VRUtS64x

• View All