Method to find closest control point

Hi all, Is there a method in RhinoCommon to find the closest control on an interpolated curve to a given test point?
As of now I am using the following code to find the span which the closest point falls in, and I can find the point by testing to which side of the span the point is closer but I feel an easier way must be there !?

double t;
    c.ClosestPoint(testPoint, out t);
    for (int i = 0;i < c.SpanCount;i++)
    {
      if (c.SpanDomain(i).IncludesParameter(t))
      {
        B = i;
        return;
      }
    }

Are you looking for this?: Curve.ClosestPoint Method (Point3d, Double)

no, that outputs the curve parameter, I would like to know the closest “control point” , let’s say you have an interpolated curve with 10 control points, which control points is closest to the given curve parameter ‘t’ ?

That outputs the curve parameter, but you can use this parameter easily to create a point: Curve.PointAt Method

I’m looking for the index of the “control point” not the position.

Hi @torabiarchitect, below should do it:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def nc_filter( rhino_object, geometry, component_index):
    return isinstance(geometry, Rhino.Geometry.NurbsCurve)

def DoSomething():
    crv_id = rs.GetObject("Select a nurbs curve", 4, True, False, nc_filter)
    if not crv_id: return
    
    test_point = rs.GetPoint("Test point")
    if not test_point: return
    
    curve = rs.coercecurve(crv_id, -1, True)
    points = [pt.Location for pt in curve.Points]
    
    # get closest index to picked point
    index = Rhino.Collections.Point3dList.ClosestIndexInList(points, test_point)
    
    print "Closest control point index to test point: {}".format(index)

DoSomething()

_
c.

1 Like

cool! I was not aware of Rhino.Collections.Point3dList