(PY) finding closest point on curve

Hello everyone! I am trying to write a script for finding the closest point of a curve. I try to generate a point that has random coordinates on rhino canvas and then test if it is inside a given area.If it is outside of the given area I want it to be projected on the closest point of the area borders,and then have a random walk towards the center of the area (this part of the code is not solved yet).I have a problem though in converting the GUID to Point coordinates. I know its an easy task but I seem to have stuck. I attach the code(the area is given and the curve is the border of the area)

Thanking everyone in advance

The script is the following

import rhinoscriptsyntax as rs
import random as r
from Rhino import Geometry


class Walker:
    
    def __init__(self):
        self.x=r.randint(-20,20)
        self.y=r.randint(-20,20)
        self.z=0
    def point(self):
        shape=rs.AddPoint(self.x,self.y,self.z)
        return shape
    def update(self):
        choice=r.randint(0,3)
        if (choice==0):
            self.x+=1
        elif(choice==1):
            self.x-=1
        elif(choice==2) :
            self.y+=1
        else:
            self.y-=1
            
    def project(self,area,curve):
        point=(self.x,self.y,self.z)
        
        
        check=rs.IsPointInSurface(area,point)
        if check:
            self.x=self.x
            self.y=self.y
        else:
            closest=rs.CurveClosestPoint(curve,point)
            cp=rs.AddPoint(closest)
            point=point+cd

            
            
            
    def test(self):
        print self.x,self.y


w=Walker()

for t in range(time):
    w.test()
    w.project(area,curve)
    a=w.point()
    w.update()

Some things that might help…

rs.CurveClosestPoint() returns a parameter along the curve, not a 3D point. To get the actual 3d point, you need to run something like this:

param=rs.CurveClosestPoint(curve, point)
pt_on_crv=rs.EvaluateCurve(curve,param)

pt_on_crv will be a Point3d object, from which you can get its coordinates of you need with

pt_on_crv.X
pt_on_crv.Y
pt_on_crv.Z
etc.

However you also need to understand that all the Add… methods in Rhinoscriptsyntax ADD an object to the document and return a GUID. Thus rs.AddPoint() will return a GUID of the point object just added to the document, it will not return the coordinates of the point.

BTW, I’m not sure why your cp=rs.AddPoint(closest) did not error out when you fed it a single number - the parameter from CurveClosestPoint(). In theory, it should have.

HTH, --Mitch

1 Like

Actually it did error,but thanks to your reply I managed to get it working. On more question: the point is supposed to be generated randomly,and only if it is outside of the boundaries of the given surface(area) get projected on the bounding line. For some reason the boolean check keeps giving me false results, as if all the points are constructed outside of the given surface. Any ideas on that?

Otherwise it works like a charm!Thanks again!

I think maybe you need to use rs.IsPointOnSurface() not rs.IsPointInSurface(). --Mitch