Compare point and open curve [resolved]

Hello everybody again!

Someone knows how I can find out if a point is contained in a open Curve.

When I have a closed curve I use the following code:

curve.Contains(AnotherCurve.PointAtStart) == PointContainment.Coincident;

Thank you in advance.

Hi Miguel,

I needed something similar some time ago. Actually I needed to check an open curve for Inside containment, but the principle is similar with Coincident one.
There might be a better way, but I succeeded by basically closing the open curve, and then checking for Containment.
That said, I found curve.MakeClosed method to be really tricky in working with curves that are not polylines (curve degree 1).
A workaround may be to recreate your open curve as an interpolated closed curve. Then check for point containment.
Sorry for not being able to help you with C#, this is python example instead:

import Rhino

openCrvDegree = openCrv.Degree

if openCrvDegree == 1:
    openCrvNurbsCrv = openCrv.ToNurbsCurve()
    pts = list(openCrvNurbsCrv.GrevillePoints())
elif openCrvDegree >= 2:
    divisionTs = openCrv.DivideByCount(10, True)
    pts = [openCrv.PointAt(t) for t in divisionTs]
pts.append(pts[0])

if (openCrvDegree % 2 == 0):
    openCrvDegree += 1

knotStyle = Rhino.Geometry.CurveKnotStyle.ChordPeriodic
closedCrv = Rhino.Geometry.Curve.CreateInterpolatedCurve(pts, openCrvDegree, knotStyle)

contain = closedCrv.Contains(anotherCurve.PointAtStart) == Rhino.Geometry.PointContainment.Coincident
print "contain: ", contain

Thank you very very much for your reply.

This is a good idea, however my curves are lines. Are there some function to find out if on point is contained in a line? For example from the equation of the line.

I do not know, sorry, will leave that to more math knowledgeable guys around here.

If you decide to go with the rhinocommon methods, you can use line.DistanceTo(anotherCurve.PointAtStart, True) with limitToFiniteSegment input parameter set to True to check for the finite line segment only.

Sorry, I am not sure what it means for a point to be contained “in” an line. Are you looking to see if a point is “on” a line?

djordje, Thank you it works.

Dale, Yes, “on” a line. Excuse my bad English.

For curves in general, you can do something like this:

static bool IsPointOnCurve(Curve curve, Point3d point, double tolerance)
{
  var rc = false;
  if (null != curve && curve.IsValid && point.IsValid)
  {
    if (tolerance < Rhino.RhinoMath.ZeroTolerance)
      tolerance = Rhino.RhinoMath.ZeroTolerance;

    double t;
    if (curve.ClosestPoint(point, out t))
    {
      if (point.DistanceTo(curve.PointAt(t)) <= tolerance)
        rc = true;
    }
  }
  return rc;
}

Does this help?

Thanks Dale, I found it very useful.

A post was split to a new topic: Find on which side of a curve a point exists