Is there a Linq method for selecting a curve with the longest length from a list?
At the moment I am looping and wondering if this is the best method for this.
thanks,
Joel
Is there a Linq method for selecting a curve with the longest length from a list?
At the moment I am looping and wondering if this is the best method for this.
thanks,
Joel
This is about as unreadable as I can make it (lol):
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
var go = new GetObject();
go.SetCommandPrompt("Select curves");
go.GeometryFilter = ObjectType.Curve;
go.GetMultiple(1, 0);
if (go.CommandResult() != Result.Success)
return go.CommandResult();
var curves = new List<Curve>(go.ObjectCount);
curves.AddRange(go.Objects().Select(objref => objref.Curve()).Where(curve => null != curve));
var max_length = curves.Select(crv => crv.GetLength()).Concat(new[] { -1.0 }).Max(); ;
RhinoApp.WriteLine("Max curve length: {0}", max_length);
return Result.Success;
}
Dale,
Thanks for the reply but this looks like it only returns the max length but not the curve?
This is what I am using for the same.
double max = h.Max(c => c.GetLength());
I then loop over the list and compare them to this value to find the one I am looking for. This seems like the wrong approach to me.
Joel
Iām guessing this is slow, but who knows.
var sorted_curves = curves.OrderBy(o => o.GetLength()).ToList();
ā D
Maybe something like
var longest = (from crv in curves let len = crv.GetLength() where len > 0 orderby len descending select crv) .First();
-Nathan this works well!
I have noticed that there are a few rhino collections like Point3dList and CurveList. I really like that these collections have functions like transformations. It would be nice to be able to sort them by attributes like length etc. Although I am not sure if linq is faster or a native collection would be.
Joel
Probably not faster with vanilla LINQ, but if you do stuff like this you might benefit from PLINQ. Untested, but try curves
to curves.AsParallel()
. Probably more useful with larger datasets.