Rhino.Geometry.Line closest point on curve

Hi there,
is there a way to get a closest point of a curve on a Rhino.Geometry.Line?
I know it works with a point, but as I understand a Rhino.Geometry.Line is to be treated more vector-ish if I may call it like that.

I thought about making a bounding box through all elements and use the option to get the Line’s extents, but this might not work in some cases for me because I would need to think outside the limitations of a bounding box.

Thanks,
T.

Is the line to be considered as infinite?
If so, project the curve on a plane perpendicular to the line, and use the usual ClosestPoint method to retrieve the parameter.

public void LineCurveProximity(Line L, Curve C, out double tL, out double tC){
    Plane plane = new Plane(L.From, L.Direction);
    Curve C2 = (Curve) C.DuplicateShallow();
    C2.Transform(Rhino.Geometry.Transform.PlanarProjection(plane));
    double t;
    C2.ClosestPoint(L.From, out t);
    tC = t;
    tL = L.ClosestParameter(C.PointAt(tC));
  }

Your question is not about grasshopper, but it helps anyway:
LineCurveProximity.gh (8.7 KB)

1 Like

Hi @tobias.stoltmann,

A Line is just a struct with two points. And it does not inherit from Curve, so it doesn’t have all the same properties and methods as other Curve-inherited classes, such as LineCurve, ArcCurve, NurbsCurve, etc.

To you your question, I’d just create a LineCurve from your Line and then use Curve.ClosestPoints to get the closest points between the two curves.

Maybe something like this:

def CurveLineClosestPoints(curve, line):
    lineCurve = Rhino.Geometry.LineCurve(line)
    rc, pointOnCurve, pointOnLine = curve.ClosestPoints(lineCurve)
    if rc:
        return [pointOnCurve, pointOnLine]
    return []

– Dale

2 Likes

@maje90
This works like a charm! Thanks!

@dale
If I am not mistaken the point will then be on the LineCurve.
In my case I want the point that is closest on the Line.

But thanks for your help anyways (as always. :slight_smile: ).

@maje90
One idea here.
Do you have an idea how to make this work for two lines?
Let’s say a LineLineProximity?

You can use Rhino.Geometry.Intersect.Intersection.LineLine, which “finds the closest point between two infinite lines”.

@AndersDeleuran thanks. I fear this only works if the lines are coplanar, right.
In cases they are in different planes, the method returns null/None.

I don’t think so. It’s certainly always worked as advertised for me. Here’s a quick example:


220625_LineProximity_GHPython_00.gh (8.1 KB)

@AndersDeleuran, sorry for that, yes.
Just tried it myself and it’s working fine.
Tusind tak!

1 Like